Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8656447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:16:38+00:00 2026-06-12T15:16:38+00:00

Possible Duplicate: while( !feof( file ) ) is always wrong I have a strange

  • 0

Possible Duplicate:
“while( !feof( file ) )” is always wrong

I have a strange problem related to a while loop. I have a function which is called at the end of the parent of a process (print_file()) and it doesn’t accept a true condition to go ahead. This is my simple multiprocess code as you can see below.

#include <stdio.h>     /* basic I/O routines.   */
#include <stdlib.h>
#include <unistd.h>    /* define fork(), etc.   */
#include <sys/types.h> /* define pid_t, etc.    */
#include <sys/wait.h>  /* define wait(), etc.   */
#include <signal.h>    /* define signal(), etc. */
#include <pthread.h>
#include <time.h>
#include <ctype.h>

void print_file(char* [], char* []);
void child_process(int,int);
void parent_process();
int counter=0;

int main(int argc, char* argv[]) {

    counter = atoi(argv[1]);
    int i,k;
    pid_t child_pid;
    int child_status;
    char* array[counter];
    srand ( time(NULL) );
    int temp;

    for(i=0; i<counter; i++){
        temp = rand()%4;
        child_pid = fork();

        switch(child_pid) {
            case -1:
                printf("Error occured with fork()\n");
                exit(1);
            case 0: 
                child_process(i,temp); /* Child Process */
                exit(0);
        }
    }

    wait(&child_status);
    parent_process();
    execl("/usr/bin/killall","killall","tail",(char *) 0);
    return 0;
}

void child_process(int i,int temp){

    FILE* fptr;
    fptr = fopen("sample.txt","a+");
    if( temp==0 ) {
        fprintf(fptr,"A %d\n",i);
    }
    else if( temp==1 ) {
        fprintf(fptr,"C %d\n",i);
    }
    else if( temp==2 ) {
        fprintf(fptr,"G %d\n",i);
    }
    else if( temp==3 ) {
        fprintf(fptr,"T %d\n",i);
    }
    fflush(fptr);
    sleep(1);
    fclose(fptr);
}

void parent_process(void){

    FILE* fptr;
    fptr = fopen("sample.txt","r");
    char* str = (char*)malloc(1);
    int temp,i,k;
    char* array_opst[counter];
    char* array[counter];

    i=0;
    while(!feof(fptr)){

        fscanf(fptr,"%s%d",str,&temp);
        if(feof(fptr))
            break;

        if(strcmp(str,"A")==0){
            array[temp]="A";
            array_opst[temp]="T";
            printf("Array[%d] = %s\n",temp,array[temp]);
        }
        else if(strcmp(str,"C")==0){
            array[temp]="C";
            array_opst[temp]="G";
            printf("Array[%d] = %s\n",temp,array[temp]);
        }
        else if(strcmp(str,"G")==0){
            array[temp]="G";
            array_opst[temp]="C";
            printf("Array[%d] = %s\n",temp,array[temp]);
        }
        else if(strcmp(str,"T")==0){
            array[temp]="T";
            array_opst[temp]="A";
            printf("Array[%d] = %s\n",temp,array[temp]);
        }
        i++;
    }
    fclose(fptr);
    free(str);
    print_file(array,array_opst);
}

void print_file(char* array[counter], char* array_opst[counter]) {

    int j=0,i=1;

    while(j<counter){
        printf("%d", i);
        i++;
        j++;
        if(i==10){
            i=0;
        }
    }
    return;
}

In the print_file function it never enters into the while loop even when the condition is satisfied. But whenever I put a printf on the first line of the print_file function to check if it does succesfully enter it prints. (Be carefull that counter is a global variable). What causes this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:16:40+00:00Added an answer on June 12, 2026 at 3:16 pm

    The output from the loop is not appearing due to stdout being buffered (the printf() statement inside the loop contains no \n and stdout is not explicitly fflush()d) and the call to execl() in main(), which replaces the current process image and stdout buffer is not flushed as usual on a normal program exit. Add a new-line character to the printf() or explicitly call fflush(stdout); after the loop.


    There is a buffer overrun, causing undefined behaviour:

    char* str = (char*)malloc(1);
    

    allocates 1 byte, str is then used thus:

    fscanf(fptr,"%s%d",str,&temp);
    

    fscanf() adds a null terminator overruning the end of str even if only one char is read. I can’t see a reason for dynamically allocating this or even when it needs to be an array as the format of the lines seems to be a single char followed by an int:

    fprintf(fptr,"A %d\n",i);
    

    To fix this problem, just use a char and the format specifier %c instead:

    char ch;
    
    if (2 == fscanf(fptr, "%c%d", &ch, &temp))
    {
    }
    

    Always check return values (fopen(), fprintf(), fscanf() etc).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: “while( !feof( file ) )” is always wrong If I write an
Possible Duplicate: Modifying .NET Dictionary while Enumerating through it I have a dictionary object
Possible Duplicate: Runtime exception, recursion too deep I'm getting a problem while developing a
Possible Duplicate: Are iframes considered 'bad practice'? While working with web developers, I always
Possible Duplicate: Parse C# string to DateTime I am facing a problem while converting
Possible Duplicate: Assignment Condition in Python While Loop What's the equivalent of this: while
Possible Duplicate: do-while loop in Python? How do I write a loop in Python
Possible Duplicate: Decreasing for loop in Scala? While working through [Scala For The Impatient][1],
Possible Duplicate: What's the difference between iterating over a file with foreach or while
Possible Duplicate: Some issues trying to read a file with cbc.read.table function in R

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.