而循环在假定为C之前终止

而循环在假定为C之前终止,c,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 Po

该程序应该将现有的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;
}
如果不初始化它,
strcat()
senetence
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;
}
#包括
#包括
#包括
内部主(空)
{
char fileNameIn[100]={0};
char fileNameOut[100]={0};
FILE*ptrIn=NULL;
文件*ptrOut=NULL;
char str[1024]={0};
char*token=NULL;
int ctr=0;
int ndel=-1;
char-wordA[1024]={0};
字符句子[1024]={0};
字符del[]=“!-,.”;
内部温度=0;
printf(“输入文件名>”;
fflush(stdout);
scanf(“%99s”,fileNameIn);
printf(“输入输出文件名>”;
fflush(stdout);
scanf(“%99s”,fileNameOut);
ptrIn=fopen(fileNameIn,“r”);//要读取的是r
if(ptrIn==NULL)
{
printf(“无法打开文件%s\n”,fileNameIn);
返回-1;
}
ptrOut=fopen(fileNameOut,“w”);//w将写入文件
如果(ptrOut==NULL)
{
fclose(ptrIn);
printf(“无法打开文件%s\n”,fileNameOut);
返回-1;
}
while(fgets(str,sizeof(str,ptrIn))//当我们不在文件末尾时
{
put(str);
令牌=strtok(str,del);
温度=-1;
printf(“输入要删除的单词的位置(从0开始计数)`-1跳过删除'>”;
如果(扫描频率(“%d”,&ndel)!=1)
继续;
printf(“您选择了:%d\n”,ndel);
句子[0]='\0';
while(令牌!=NULL)
{
如果(ctr!=ndel)
strcpy(wordA,token);
令牌=strtok(空,del);
如果(ctr!=ndel)
{
strcat(句子,单词a);
strcat(句子“”);
}
ctr++;
}
fprintf(ptrOut,“%s”,句子);
printf(“现在的句子是:\n%s”,句子);
printf(“\n”);
ctr=0;
ndel=-1;
}
printf(“完成主体:”);
fflush(ptrOut);
fclose(ptrIn);
fclose(ptrOut);
返回退出成功;
}

我无法重现您描述的问题,虽然我看到您的代码中有许多错误,但我也没有看到任何会导致该问题的东西。我知道会导致该问题的原因->未定义的行为。证据是它对@zwol有效,你
strcpy()
到一个非常小的数组,所以我猜可能是这样,如果你将
wordA
重新声明为
char wrodA[200]
,问题还会继续发生吗?现在看看你的代码,更容易阅读。。。不是吗?当你构建一个不适合
字符句子[101]的句子时会发生什么?程序中的任何地方都没有
strlen()
。它对我来说工作得很好。我建议他在
fgets()
之后在循环中这样做,并从循环底部删除
strcpy(句子“”
。@jschultz410我没有检查完整的代码,但这是出现问题的最明显原因,所以我添加了这个答案。您肯定是对的。实际上,更改应该在外部“while”循环语句之后。不幸的是,添加了:句子[0]='\n\;不会解决问题。while循环只运行了三次,然后它就运行了quits@user4557512现在我仍然很忙,虽然我从早上5点开始工作,现在已经是晚上7点了,但是当我觉得今天我不想再做我的项目时,我会看看。