Search code examples
cfileerrnofeofftell

Why ftell prints -1 as value of file pointer? And why errno prints out "INVALID ARGUMENT"?


I have these 2 functions in a project which loads and saves user's information into a file. Each user is saved in a new line of the file. My problem is that the program crashes when I try to use ftell(f). When I print ftell(f) it prints -1 after opening the file with fopen(). I tried to see in errno the error, but it prints "NO ERROR" after fopen() but "INVALID ARGUMENT" once I use fseek to modify the file pointer f position.

My problem is in my Load_File function, but I show the Save_File function too for checking I write correctly in the file.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

LIST Load_File(LIST L){
    //PRE: receive a void list (L==NULL)
    //POST: returns an user's loaded from file
    USER user; //node of list

    char str[150]; 

    //User's structure
    char name[30];
    char CI[10];
    char email[30];
    char city[30];
    char password[30];

    errno=0;

    FILE *f;
    if(f=fopen("DATOS.txt","r")==NULL){
        printf("File not found. \n");
        return L;
    }


    //Code for verify what's happening
    printf("FTELL: %d/n", ftell(f)); //prints -1
    printf("ERRNO: %s\n", strerror(errno)); //prints NO ERROR
    fseek(f, 0, SEEK_CUR);
    printf("FTELL: %d\n", ftell(f)); //still prints -1
    printf("ERRNO: %s\n", strerror(errno)); //prints INVALID ARGUMENT
    printf("FEOF: %d\n",feof(f)); //CRASHES! (a)

    while(feof(f)==0){ //CRASHES when (a) line is deleted

        //Load line of file in user's structure
        fgets(str, 150, f);
        sscanf(str,"%s %s %s %s %s ",name, CI, email, city, password);
        //Copy into structure's parts
        strcpy(user->name, name);
        strcpy(user->CI, CI);
        strcpy(user->email, email);
        strcpy(user->city, city);
        strcpy(user->password, password);

        Add_user_to_list(L, user);
    }

    if(fclose(f)!=0) printf("\n\n FILE NOT PROPERLY ClOSED \n\n");
}

void Save_File(LIST L){
    //PRE: receive an user's list
    //POST: saves a new list in file

    FILE *f;
    int flag=0;

    f=fopen("DATOS.txt","w");
    if(f==NULL){
        printf("Error opening file f\n");
    }
    if(!L_Void(L)){
        L=L_First(L);
        do{
            if(flag) L=L_Next(L);
            flag=1;
            fprintf(f,"%s %s %s %s %s \n",L_InfoM(L)->name,L_InfoM(L)->CI, L_InfoM(L)->email, L_InfoM(L)->city, L_InfoM(L)->password);
        }while(!L_Final(L));
    }else printf("List is void, then nothing was saved.\n");

    if(fclose(f)!=0) printf("\n\n FILE NOT PROPERLY COSED \n\n");
}

Solution

  • This code is wrong:

    if(f=fopen("DATOS.txt","r")==NULL){
    

    Binary operators - such as == - have higher precedence than assignment operators - such a =.

    So your code is parsed as:

    if(f=( fopen("DATOS.txt","r")==NULL ) ){
    

    The result of the logical == comparison is assigned to f.

    Why are you stuffing the assignment into the if statement? This is much clearer, as well as being a lot less bug-prone:

    FILE *f = fopen( "DATOS.txt", "r" );
    if ( NULL == f ) {
    ...
    

    The more you do on one line, the more likely you'll make a mistake. Programming correctly is hard enough. Don't do things that make it harder - like try to see how much code you can stuff into one line.