LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-25-2012, 02:34 AM   #1
Anuradha12345
LQ Newbie
 
Registered: May 2012
Posts: 10

Rep: Reputation: Disabled
c program to check all the subdirectories and read each directory files


i want a c program which scan all the subdirectories of a directory and read all the files and store the contents of the file at one place. Thanking in advance.
 
Old 07-25-2012, 03:12 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Write one then. Homework or not, people aren't really here to do things for you. If you're stuck with something, post what you've done and ask for help.
 
1 members found this post helpful.
Old 07-25-2012, 05:21 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
int main (void) {
    system ("(find . -type f -print0 | xargs -0 cat) > outputfile");
return 0; }
 
Old 07-25-2012, 06:00 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Read up on the functions stat() and lstat() as well as fread(), fwrite(), fopen() and fclose().
 
Old 07-25-2012, 07:34 AM   #5
jack.sully
Member
 
Registered: Jul 2012
Posts: 38

Rep: Reputation: Disabled
Hi,
Few days back i wrote a code to scan the directory and display all the files.
u can modify the code to store the contents at one place. To study in detail u can follow Begginning Linux Programming by Neil matthews and Richard Stones, and C programming by Dennis Rittchie.
This should help.


#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>

void dirwalk(char *,int);

main()
{
printf("Directory scan of /home/surendra/Linuxcodes:\n");
dirwalk("/home/surendra/Linuxcodes",0);
printf("done.\n");
return(0);
}

void dirwalk(char *dir,int depth)
{
DIR *dp;
char str[30];
char name[30];
struct dirent *entry;
struct stat statbuf;

if((dp = opendir(dir)) == NULL)
{
fprintf(stderr,"Cannot open directory: %s\n",dir);
return;
}

chdir(dir);

while((entry = readdir(dp)) != NULL)
{
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode))
{
/* Found a directory ,but ignore . and .. */
if(strcmp(".",entry->d_name) ==0 || strcmp("..",entry->d_name) == 0)
continue;
printf("%*s %s/\n",depth,"",entry->d_name);

/*funtion is called recursively at a new indent level */
dirwalk(entry->d_name,depth + 4);
}
else
if(S_ISREG(statbuf.st_mode))
printf("%*s %s \n",depth,"",entry->d_name); /*Here you need to add u'r code*/
strcpy(str,entry->d_name);
strcpy(name,str);

printf("\nFile name is %s\n",name);
}

chdir("..");
closedir(dp);
}
 
1 members found this post helpful.
Old 07-25-2012, 10:26 AM   #6
rstewart
Member
 
Registered: Feb 2005
Location: Sunnyvale, CA
Distribution: Ubuntu
Posts: 205

Rep: Reputation: 38
man ftw (file tree walk), the code already exists in a library.
 
2 members found this post helpful.
Old 07-26-2012, 05:46 AM   #7
Anuradha12345
LQ Newbie
 
Registered: May 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Unhappy listing subdirectories

Here is my code which lists all the subdirectories with its id instead of name now i m stuck how to read each file in each directory by usind its id(catid) and collect all files data at one place line by line...



#define MAX_CATEGORIES 100

int main()
{
DIR *d;
char *dir_name="/home/anuradha/ufilter/blacklists";
struct category{
short catid;
char catname[100];
};
struct category cat[MAX_CATEGORIES];
int counter=0;

/*open a current directory*/
d=opendir(dir_name);
if(!d)
{
fprintf(stderr, "cannot open '%s' :%s",dir_name,strerror(errno));
exit(EXIT_FAILURE);

}

struct dirent * entry;
while(1)
{
entry=readdir(d);
if(entry==NULL)
{
break;
}
counter++;
cat[counter].catname[0] = '\0';
strncpy (cat[counter].catname, entry->d_name, sizeof(cat[counter].catname));
cat[counter].catid = counter;

}

/*Close the directory.*/
if(closedir(d))
{
fprintf(stderr, "could not close '%s' : %s\n",dir_name,strerror(errno));
exit(EXIT_FAILURE);
}
return 0;
}
 
Old 07-26-2012, 07:45 AM   #8
jack.sully
Member
 
Registered: Jul 2012
Posts: 38

Rep: Reputation: Disabled
I think u'r task will be much easier if u use shell script.
 
Old 07-26-2012, 10:36 AM   #9
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
The index into the array is basically the same as catid (if I see it correctly both are based on counter). So you can iterate through the array by using the counter.

Do you want one line of file 1 followed by one line of file 2 followed by one line of file 3; and next do the second line of each file? Or do you want file 1 followed by file 2 followed by file 3 etc.

As you're talking about lines, I assume that those files are text files. In which case you can use fopen(), fgets(), fputs() and fclose().
 
1 members found this post helpful.
Old 07-29-2012, 05:58 PM   #10
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Quote:
Originally Posted by NevemTeve View Post
Code:
int main (void) {
    system ("(find . -type f -print0 | xargs -0 cat) > outputfile");
return 0; }
hahaha!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Rsync exclude a directory with all subdirectories and files Slackovado Slackware 1 12-26-2010 11:40 PM
Find files in current directory, not subdirectories SmurfGGM Linux - Newbie 1 10-09-2008 12:14 PM
copy 3 files to a directory and all subdirectories? snip128 Linux - Newbie 9 08-12-2005 09:18 PM
Copying all files from subdirectories into one directory Hegemon Linux - General 3 01-17-2005 11:25 AM
read directory and subdirectories skora Programming 3 10-27-2003 10:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration