user4557512
user4557512

Reputation: 49

while loop terminating before it is supposed to C

This program is supposed to copy an existing txt file to a new txt code file. However is isn't working right. For some reason it always stops after the third iteration. Suggestions?

#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;
}

This is an example of the existing file:

test.txt:

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

Upvotes: 0

Views: 104

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

You strcat() to senetence wihtout initializing it, strcat() will search for the terminating nul byte of it's first argument and start copying characters from it's second argument start from that position, so a simple

sentence[0] = '\0';

right after the outer while loop will fix it, but your code needs reformatting and you should make it safer by checking every single potential undefined behavior cause.

This is the code and it now works correctly

#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;
}

Upvotes: 2

Related Questions