繁体   English   中英

while循环在应该到达C之前终止

[英]while loop terminating before it is supposed to C

该程序应该将现有的txt文件复制到新的txt代码文件。 但是,这是行不通的。 由于某种原因,它总是在第三次迭代后停止。 有什么建议吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
    char fileNameIn[100];
    char fileNameOut[100];
    FILE *ptrIn = NULL; //____ File Pointers
    FILE *ptrOut = NULL;
    char str[1000]; //this is used at fgets and it obtains sentences
    /*_________________Counter variables)*/
    char *token;
    int ctr = 0;
    int ndel = -1;
    char wordA[10];
    char sentence[101];

    char del[10] = " !-,.";
    ;
    int temp = 0;
    printf("Enter the input filename: \n");
    //  gets(fileNameIn);
    scanf("%s", fileNameIn);
    //printf("You entered: %s\n",fileNameIn);
    printf("Enter the output filename: \n");
    scanf("%s", fileNameOut);

    ptrIn = fopen(fileNameIn, "r"); // r is to read
    ptrOut = fopen(fileNameOut, "w"); //w is to write on file
    if (ptrIn == NULL || ptrOut == NULL) {
        printf("Unable to open file\n");
        exit(1);
    }

    //while(fgets (str,sizeof(str), ptrIn) )
    while (fgets(str, sizeof(str), ptrIn)) { // while we are not at the end of the file
        puts(str);
//      if(temp==0)
//      {
        token = strtok(str, del);
        temp = -1;

        printf(
                "Enter position of word to delete (Start counting at 0). Enter -1 to skip deletion:\n");
        scanf("%d", &ndel);
        printf("You selected: %d\n", ndel);

        while (token != NULL)   // while loop inside a sentence
        {
            if (ctr != ndel) {
                strcpy(wordA, token);

            }
            token = strtok(NULL, del);
            if (ctr != ndel) {
                strcat(sentence, wordA);
                strcat(sentence, " ");
                printf("halfway?");
            }
            ctr++;
        }   // endof sentence loop
        fprintf(ptrOut, "%s", sentence);
        printf("the sentence is now:\n%s", sentence);
        printf("___________________________________________");
        printf("\n");
        strcpy(sentence, "");
        ctr = 0;
        ndel = -1;
    }   //end of while loop eof

    printf("Finish the main: ");
    fflush(ptrOut);
    fclose(ptrIn);
    fclose(ptrOut);

    return EXIT_SUCCESS;
}

这是现有文件的示例:

test.txt:

hello my name is john.
this is a test.
after the third line the while
loop stops
this does the get copied

strcat()senetence wihtout初始化它, strcat()将搜索终止nul字节的它的第一个参数,并开始复制字符从它从该位置第二个参数开始,所以一个简单

sentence[0] = '\0';

在外部while循环之后将对其进行修复,但是您的代码需要重新格式化,并且应该通过检查每个潜在的未定义行为原因来使其更加安全。

这是代码,现在可以正常工作

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char fileNameIn[100]  = {0};
    char fileNameOut[100] = {0};
    FILE *ptrIn           = NULL;
    FILE *ptrOut          = NULL;
    char str[1024]        = {0};
    char *token           = NULL;
    int   ctr             = 0;
    int   ndel            = -1;
    char  wordA[1024]     = {0};
    char  sentence[1024]  = {0};
    char  del[]           = " !-,.";

    int temp = 0;
    printf("Enter the input filename  > ");
    fflush(stdout);
    scanf("%99s", fileNameIn);

    printf("Enter the output filename > ");
    fflush(stdout);
    scanf("%99s", fileNameOut);

    ptrIn = fopen(fileNameIn, "r"); // r is to read
    if (ptrIn == NULL)
    {
        printf("Unable to open file %s\n", fileNameIn);
        return -1;
    }

    ptrOut = fopen(fileNameOut, "w"); // w is to write on file
    if (ptrOut == NULL)
    {
        fclose(ptrIn);
        printf("Unable to open file %s\n", fileNameOut);
        return -1;
    }

    while (fgets(str, sizeof(str), ptrIn)) // while we are not at the end of the file
    {
        puts(str);

        token = strtok(str, del);
        temp  = -1;

        printf("Enter position of word to delete (Start counting at 0) `-1 to skip deletion' > ");
        if (scanf("%d", &ndel) != 1)
            continue;
        printf("You selected: %d\n", ndel);

        sentence[0] = '\0';
        while (token != NULL)
        {
            if (ctr != ndel)
                strcpy(wordA, token);
            token = strtok(NULL, del);
            if (ctr != ndel)
            {
                strcat(sentence, wordA);
                strcat(sentence, " ");
            }
            ctr++;
        }
        fprintf(ptrOut, "%s", sentence);

        printf("the sentence is now:\n%s", sentence);
        printf("\n");

        ctr  = 0;
        ndel = -1;
    }
    printf("Finish the main: ");

    fflush(ptrOut);
    fclose(ptrIn);
    fclose(ptrOut);

    return EXIT_SUCCESS;
}

暂无
暂无

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

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