简体   繁体   English

在 C 中跳过了循环

[英]While loop got skipped in C

This is my code, I wanted to display all of the lines stored inside of a text file and at the same time replace that particular line by getting input from user of what line to replace.这是我的代码,我想显示存储在文本文件中的所有行,同时通过从用户那里获取要替换的行的输入来替换该特定行。

///Get Customer ID
char c_id[100];
printf("Enter Customer ID: ");
scanf("%s", &c_id);

FILE* fPtr;
FILE* fTemp;
char temp_line[100];
char buffer[100];
char newline[100];
int i, arr_n, line, count;

/* Remove extra new line character from stdin */
fflush(stdin);
fflush(stdout);

/// Open all required files
fPtr = fopen(c_id, "r");
fTemp = fopen("replace.txt", "w");

/// Print the content
i = 1;
arr_n = 0;

printf("\nOptions:\n");

while (fgets(buffer, BUFFER_SIZE, fPtr))
{
    printf("%d. %s\n", i, buffer);
    i = i + 1;
}

printf("Enter your options: ");
scanf("%d", &line);

printf("Enter the updated information: ");
scanf("%s", &newline);

count = 0;

while (fgets(temp_line, BUFFER_SIZE, fPtr))
{
    count++;
    if (count == line)
    {
        switch (line)
        {
        case 1:
            fprintf(fTemp, "Name: %s", newline);
            break;
        case 2:
            fprintf(fTemp, "Date of birth: %s", newline);
            break;
        case 3:
            printf("It did got here");
            fprintf(fTemp, "Address: %s", newline);
            break;
        case 4:
            fprintf(fTemp, "Contact 1: %s", newline);
            break;
        case 5:
            fprintf(fTemp, "Contact 2: %s", newline);
            break;
        default:
            printf("Invalid answer given");
        }
    }
    else
    {
        fputs(temp_line, fTemp);
    }
}

/// Close all files to release resource
fclose(fPtr);
fclose(fTemp);


/// Delete original source file 
remove(c_id);

/// Rename temporary file as original file 
rename("replace.txt", c_id);

printf("\nSuccessfully updated the information");

It's the last while loop that got skipped.这是最后一个被跳过的while循环。 Why would it skip that while loop, I can't seem to find the reason it skips.为什么它会跳过那个while循环,我似乎找不到它跳过的原因。 I tried to change to another buffer to temp_line, and it's still the same.我尝试将temp_line更改为另一个缓冲区,它仍然是一样的。

I tried to remove few unnecessary line that's not related to it first and try to resolve the problem but still can't find the exact reason the while loop got skip.我尝试先删除一些与它无关的不必要行并尝试解决问题,但仍然找不到 while 循环被跳过的确切原因。

Is the because the switch case?是因为开关盒吗?

Or is there something wrong with buffer?还是缓冲区有问题?

You first loop reaches end of file.您的第一个循环到达文件末尾。 Function fgets returns a pointer to the buffer or a null pointer if end of file is reached. Function fgets返回指向缓冲区的指针,如果到达文件末尾,则返回 null 指针。 Since you are using the pointer value as a boolean in the while loop second loop is skipped because your file is already exhausted and fgets returns NULL.由于您在while循环中将指针值用作boolean,因此跳过第二个循环,因为您的文件已经用尽并且fgets返回NULL。 while(NULL) {... } never enters the while loop code block. while(NULL) {... }永远不会进入 while 循环代码块。

Use function fseek or rewind to reset the file pointer to the top of the file before the second loop.在第二次循环之前,使用 function fseekrewind将文件指针重置到文件顶部。

i said that the while loop is skipped because whenever I put input of line for example 3, it should've run into case 3 and print "It did got here".我说 while 循环被跳过了,因为每当我输入第 3 行的输入时,它应该会遇到案例 3 并打印“它确实到了这里”。 However it didn't even print those out and then I tried to re run again by putting another printf at the start of while and nothing got printed out again.然而,它甚至没有打印出来,然后我尝试通过在 while 开始时放置另一个 printf 再次重新运行,但没有再次打印出来。

while (fgets(temp_line, BUFFER_SIZE, fPtr))
{
    printf("successfully got into while loop");
    count++;
    if (count == line)
    {
        switch (line)
        {
        case 1:
            fprintf(fTemp, "Name: %s", newline);
            break;
        case 2:
            fprintf(fTemp, "Date of birth: %s", newline);
            break;
        case 3:
            printf("It did got here");
            fprintf(fTemp, "Address: %s", newline);
            break;
        case 4:
            fprintf(fTemp, "Contact 1: %s", newline);
            break;
        case 5:
            fprintf(fTemp, "Contact 2: %s", newline);
            break;
        default:
            printf("Invalid answer given");
        }
    }
    else
    {
        fputs(temp_line, fTemp);
    }
}

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

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