简体   繁体   中英

Why is fgets() not reading the entire file?

I'm reading from a file to save the text into an array. The array the text is ultimately going to be saved into is plainText[ ]. Oddly, though, fgets() is not reading the entire file. It only reads half of the file before stopping.

This is the full contents of the file:

bbbbbbbbb
bbbbbbbbb
bbbbbbbbb
bbbbbbbbb
bbbbbbbbb
bbbbbbbbb
bbbbbbbbb
bbbbbbbbb

There are 8 rows of b's with 9 per column for a total of 72 b's. The output I'm getting, though, only contains 36 b's when printed.

Output when plainText[ ] is printed:

bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

This is the code where the file is open and read.

int main(int argc, char **argv) {
    //send an error code if the correct number of arguments have not been met to run the program
    if (argc < 3) {
        printf("Please enter the encryption file and the file to be encrypted.\n");
        return ERROR;
    }

    //open file to be encrypted (aka, plaintext)
    char *fileToEncrypt = argv[2];
    FILE *plainFile = fopen(fileToEncrypt, "r");
    //return an error code if the file to encrypt cannot be found
    if (plainFile == 0) {
        printf("The file you wish to encrypt cannot be found on this machine.\n");
        printf("Please ensure that you are in the correct directory and that there are no typos.\n");
        return ERROR;
    }

This is where I use fgets() to read the contents of the file and transfer it to plainText[ ].

//read contents of file to encrypt
char plainText[STRING_LENGTH + 1], temp2[STRING_LENGTH + 1];


//fill array with null terminators so we don't receive
//memory errors from strcat(plainText, temp2)
for (int i = 0; i < STRING_LENGTH; i++)
    plainText[i] = '\0';

//pointer to end of string in order to remove \n
char *bptr = plainText;

//read the file again if it is found there are multiple lines
while (fgets(temp2, STRING_LENGTH, plainFile)) {
    fgets(temp2, STRING_LENGTH, plainFile);
    strcat(plainText, temp2);
    bptr[strlen(bptr) - 1] = '\0'; //remove \n at end of string at a result of fgets()
}

//close file
fclose(plainFile);

The loop is telling fgets() to continue to read as long as there's content, but fgets() stops inputting content halfway through the file. Why is that?

fgets reads one line into the buffer each time it is called in the condition

while (fgets(temp2, STRING_LENGTH, plainFile)) {

And then you directly call it again, making it read a second line:

fgets(temp2, STRING_LENGTH, plainFile);

If you remove this second line, your code should work as intended. Your code currently does not work as intended because it only copies every second line using strcat .

In the loop:

while (fgets(temp2, STRING_LENGTH, plainFile)) {
    fgets(temp2, STRING_LENGTH, plainFile);
    strcat(plainText, temp2);
    bptr[strlen(bptr) - 1] = '\0'; //remove \n at end of string at a result of fgets()
}

As you call fgets() twice every other line is discarded. You don't see this because all your lines are equal, if you test it with different lines in the file this will become apparent.

To correct this you can try:

while (fgets(temp2, STRING_LENGTH, plainFile)) {
    strcat(plainText, temp2);
    bptr[strlen(bptr) - 1] = '\0'; //remove \n at end of string at a result of fgets()
}

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