Hello,
i'w writing a program that read a text file (file_list) that contain a list of names of other txt that i have to open.
with a for cycle it reads each line of file_list and save it in a string, than it has to pass that string (the name of the file that i have to open) to an fopen function.
I can compile the program but when i run it , the program says that can't open the file because invalid argument.
here is the code:
can anyone help me?? thx =)
i'w writing a program that read a text file (file_list) that contain a list of names of other txt that i have to open.
with a for cycle it reads each line of file_list and save it in a string, than it has to pass that string (the name of the file that i have to open) to an fopen function.
I can compile the program but when i run it , the program says that can't open the file because invalid argument.
here is the code:
Code:
int main() {
FILE *fd;
char buf[200];
char *res;
int i;
int num=2;
int j;
float time=0.0;
char string[20];
for(i=0;i<40;i++){
fd=fopen( "file_list.txt", "r");
if( fd==NULL ) {
perror("Opening file error");
exit(1);
}
/* legge e stampa ogni riga */
res=fgets(buf, 200, fd);
for (j=0;j<19;j++)
stringa[j]=res[j];
fclose(fd);
printf("%s",string);
/* apre il file */
fd=fopen( string, "r");
if( fd==NULL ) {
perror("Opening file error");
exit(1);
}
/* legge e stampa ogni riga */
for(j=0;j<6;j++){
res=fgets(buf, 200, fd);
}
printf("%s",res);
/* chiude il file */
fclose(fd);
fd=fopen("velocita.txt", "w+");
fprintf(fd, "timestep %d %s \n", time, res);
fclose(fd);
}
return 0;
}
Comment