简体   繁体   中英

How do I read a file in C and store the characters in a variable

I am completely new to C and need help with this badly. Im reading a file with fopen(), then obtaining the contents of it using fgetc(). What I want to know is how I can access the line fgetc() returns so if I can put the 4th - 8th characters into a char array. Below is an example I found online but am having a hard time parsing the data returns, I still don't have a firm understanding of C and don't get how an int can be used to store a line of characters.

    FILE *fr;
fr = fopen("elapsed.txt", "r");
int n = fgetc(fr);
    while(n!= EOF){
        printf("%c", n);
        n = fgetc(fr);
    }   printf("\n");

Here 1 first open the file 2 get size of file 3 allocated size to character pointer 4 and read data from file

FILE *fr;
char *message;
fr = fopen("elapsed.txt", "r");
/*create variable of stat*/
struct stat stp = { 0 };
/*These functions return information about a file. No permissions are required on the file itself*/
stat("elapsed.txt", &stp);
/*determine the size of data which is in file*/
int filesize = stp.st_size;
/*allocates the address to the message pointer and allocates memory*/
message = (char *) malloc(sizeof(char) * filesize);
if (fread(message, 1, filesize - 1, fr) == -1) {
    printf("\nerror in reading\n");
    /**close the read file*/
    fclose(fr);
    /*free input string*/
    free(message);
}
printf("\n\tEntered Message for Encode is = %s", message);

PS Dont Forget to Add #include <sys/stat.h> .

You're not retrieving a line with fgetc. You are retrieving one character at a time from the file. That sample keeps retrieving characters until the EOF character is encountred (end of file). Look at this description of fgetc.

http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/

On each iteration of the while loop, fgetc will retrieve a single character and place it into the variable "n". Something that can help you with "characters" in C is to just think of it as one byte, instead of an actual character. What you're not understanding here is that an int is 4 bytes and the character is 1 byte, but both can store the same bit pattern for the same ASCII character. The only different is the size of the variable internally.

The sample you have above shows a printf with "%c", which means to take the value in "n" and treat it like an ASCII character.

http://www.cplusplus.com/reference/clibrary/cstdio/printf/

You can use a counter in the while loop to keep track of your position to find the 4th and 8th value from the file. You should also think about what happens if the input file is smaller than your maximum size.

Hope that helps.

Ok look at it as box sizes I could have a 30cm x 30cm box that can hold 1 foam letter that I have. Now the function I am calling a function that 'could' return a 60cm x 60cm letter but it 99% likely to return a 30cm x 30cm letter because I know what its reading - I know if I give it a 60cm x 60cm box the result will always fit without surprises.

But if I am sure that the result will always be a 30cm x 30cm box then I know I can convert the result of a function that returns aa 60cm x 60cm box without losing anything

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM