Hello every body
i have the following problem :
i have different directries .In each directry there are files .I want my program to
1.scan these directries
2.then take these files
3. then open file named result.txt where each line in this file contain two points :
filename1 contents of filename1
filename2 contents of filename2
filename3 contents of filename3
....etc
where fileame , filename2 , filename3 are files names available in directries and in the same line the contents for the same fle apperar .
i try the follwoing :
#include <sys/types.h>
#include <sdtlib.h>
#include <stdio.h>
#include <ctype.h>
main() {
int i ;
FILE *d , *t , *f ;
char fileName[20] ;
char c ;
// because i work in unix in the follwoing three lines i list each
// directry and takes its file and put all of them in dir.txt file
system("ls dir1 > dir.txt") ;
system("ls dir2 > dir.txt") ;
system("ls dir3 > dir.txt") ;
// now file dir.txt contains all files names .Therefore, the next step is to back dir.txt and read each line in it where each line contain one of the files name then put this name in new file name result.txt and also open file to read its contents and put it in the same line
t = fopen("result.txt","w");
d=fopen("dir.txt","r");
while((c=getc(d) != OEF) {
if(c != '\n') {
// if character not new line this mean you still read file name
putc(c,t);
fileName[i]=c ; i++ ;
}
else {
i = 0 ;
// when i need to start read new file name and save in fileName then i value must back to zero
putc(c,t) ; // c here has new line value
}
// after i get the file name i need to open it and read its contents
f=fopen(fileName,"r") ;!!!!!!!
while((c=getc(f)) != EOF) {
......
}
fclose(f) ;
}
fclose(t);
fclose(d) ;
}
the prevoius code doesn't work because of line
f=fopen(fileName,"r") ;!!!!!!!
because fopenaccept file name as char *f and in my code fileName is array .As a result when i run the program i get segmantation fault
the question now :
how i can decide the files want to be open and read at run time where these files names read from text file ???????
or
how i can convert fileName array to char *fileName
i hoooooope i get support
Thanks a lot
Nada