简体   繁体   English

为什么 fgets() 不读取整个文件?

[英]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[ ].文本最终要保存到的数组是plainText[]。 Oddly, though, fgets() is not reading the entire file.但奇怪的是, fgets() 并没有读取整个文件。 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. b 有 8 行,每列 9 个,总共 72 个 b。 The output I'm getting, though, only contains 36 b's when printed.但是,我得到的输出在打印时仅包含 36 个 b。

Output when plainText[ ] is printed:打印 plainText[ ] 时的输出:

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[ ].这是我使用 fgets() 读取文件内容并将其传输到纯文本 [] 的地方。

//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.循环告诉 fgets() 只要有内容就继续读取,但 fgets() 在文件中途停止输入内容。 Why is that?这是为什么?

fgets reads one line into the buffer each time it is called in the condition fgets每次在条件中调用时都会将一行读入缓冲区

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 .您的代码目前无法按预期工作,因为它仅使用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.当您调用fgets() ,每隔一行就会被丢弃两次。 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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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