Error with fopen function: argument non valid

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CfdMirco
    New Member
    • Mar 2013
    • 2

    Error with fopen function: argument non valid

    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:

    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;
    }
    can anyone help me?? thx =)
    Last edited by Rabbit; Mar 22 '13, 05:11 PM. Reason: Please use code tags when posting code.
  • CfdMirco
    New Member
    • Mar 2013
    • 2

    #2
    The error comes when i pass string as argument to fopen

    Comment

    Working...