while循环在它应该是C之前终止

时间:2015-03-07 20:18:54

标签: 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

1 个答案:

答案 0 :(得分:2)

strcat()senetence无法初始化它,strcat()将搜索其第一个参数的终止nul字节并开始从中复制字符&# 39;第二个参数从那个位置开始,所以很简单

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;
}
相关问题