By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,659 Members | 1,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,659 IT Pros & Developers. It's quick & easy.

Error with fopen function: argument non valid

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1. int main() {
  2. FILE *fd;
  3. char buf[200];
  4. char *res;
  5. int i;
  6. int num=2;
  7. int j;
  8. float time=0.0;
  9. char string[20];
  10.  
  11. for(i=0;i<40;i++){
  12.  
  13. fd=fopen( "file_list.txt", "r");
  14. if( fd==NULL ) {
  15. perror("Opening file error");
  16. exit(1);
  17. }
  18. /* legge e stampa ogni riga */
  19. res=fgets(buf, 200, fd);
  20. for (j=0;j<19;j++)
  21. stringa[j]=res[j];
  22. fclose(fd);
  23. printf("%s",string);
  24. /* apre il file */
  25. fd=fopen( string, "r");
  26. if( fd==NULL ) {
  27. perror("Opening file error");
  28. exit(1);
  29. }
  30. /* legge e stampa ogni riga */
  31. for(j=0;j<6;j++){
  32. res=fgets(buf, 200, fd); 
  33. printf("%s",res);
  34. /* chiude il file */
  35. fclose(fd);
  36.  
  37. fd=fopen("velocita.txt", "w+");
  38. fprintf(fd, "timestep %d %s \n", time, res);
  39. fclose(fd);
  40. }
  41. return 0;
  42. }
can anyone help me?? thx =)
Mar 22 '13 #1
Share this Question
Share on Google+
1 Reply


P: 2
The error comes when i pass string as argument to fopen
Mar 22 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.