Quantcast
Channel: Tools
Viewing all articles
Browse latest Browse all 91752

Forum Post: File I/O on CCS v5. File pointer pointing to EOF by default

$
0
0

I am trying to use fgetc using code composer.  After opening a .csv file using fopen, I try to get the characters in the file via fgetc.  However, the file pointer points to EOF by default when I go into the do-while loop into the checklen function below. We tried to exact same code on MS Visual Studio and it works fine on there.  Here's a snippet of the relevant code:

arrayprop chartoint(char * input, char * mode){
    int length=0; //for length of char array
    FILE *file;
    file=fopen(input, mode); //The location of the txt file. Note the FORWARD slash. In read-only mode        

   length=checklen(file); //Find the length of the file so enough memory can be allocated
}

The program finds the file but messes up in checklen(file) when I try to find the number of chars within the file

int checklen(FILE *pfile){//Checks how many chars there are in the csv file
    int c=0; //just so the fgetc function has somewhere to return values to, though fgetc returns them as chars
    int length=0; //this is what this function will return at the end
    do{
        c=fgetc(pfile); //just use the fgetc function and pass in the file pointer.  pfile is incremented implicitly
        length++; //increment the length counter for every char (byte) found
    } while(c!=EOF);//do this until the end of file, at which point length will be the size of the file
    return length; //return the size of the file
}

c gets -1 on its first pass in the do-while loop, which means the file pointer is pointing to EOF by default.  Why is this happening and what can I do to fix it?


Viewing all articles
Browse latest Browse all 91752

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>