0

Why does my code give extra output while files that are created are perfect?

This is a code that aims to creates a file that stores any 5 integer values entered by the user and then makes 2 more files (even.txt and odd.txt) that contains even and odd integers from those 5 integers given by the user respectively.

Output of the program:

Enter the file name : numbers.txt
Enter numbers you want to enter inside the file : 4 5 6 7 8
Contents of even.txt are : 4    6       8       8
Contents of odd.txt are : 5     7       7

The last values i.e. 8 in even.txt and 7 in odd.txt are repeated while the files made by the code were perfect.

Code for the program:

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

int main(){
    FILE *fp;
    char name[20];
    printf("Enter the file name : ");
    scanf("%s", name);
    fp = fopen(name, "w"); //creating file
    if(fp == NULL){
        printf("Can't open the file ");
        exit(0);
    }
    int num;
    printf("Enter 5 numbers you want to enter inside the file : ");
    for(int i=0 ; i<5 ; i++){
        scanf("%d", &num);
        fprintf(fp, "%d\n", num);
    }
    fclose(fp);
    FILE *eve, *odd; 
    eve = fopen("even.txt", "w");
    odd = fopen("odd.txt", "w");
    fp = fopen(name, "r");
    if(fp == NULL || eve == NULL || odd == NULL){
        printf("Can't open the file ");
        exit(0);
    }
    while(fscanf(fp, "%d", &num)!= EOF){ //creating even and odd numbers file
        if(num%2==0){
            fprintf(eve, "%d\n", num);
        }
        else{
            fprintf(odd, "%d\n", num);
        }
    }
    fclose(eve);
    fclose(odd);
    fclose(fp);
    eve = fopen("even.txt", "r");
    if(eve == NULL){
        printf("Couldn't open file");
        exit(0);
    }
    printf("Contents of even.txt are : "); //even.txt output
    while(!feof(eve)){
        fscanf(eve, "%d", &num);
        printf("%d\t", num);
    }
    fclose(eve);
    odd = fopen("odd.txt", "r");
    if(odd == NULL){
        printf("Couldn't open file");
        exit(0);
    }
    printf("\nContents of odd.txt are : "); //odd.txt output
    while(!feof(odd)){
        fscanf(odd, "%d", &num);
        printf("%d\t", num);
    }
    fclose(odd);
    return 0;
}

Why is this happening and How do I resolve it? Thank you for your help.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • `while(fscanf(fp, "%d", &num)!= EOF)` ==> `while (fscanf(fp, "%d", &num) == 1)` ... `0 != EOF`. – pmg Mar 01 '21 at 12:22
  • 1
    Also `while (!feof(file))` is always wrong: https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong?r=SearchResults&s=1|412.8845 – pmg Mar 01 '21 at 12:25
  • @pmg Tried it but the output is still not right : Output after your suggestion: Enter the file name : numbers.txt Enter numbers you want to enter inside the file : 4 5 6 7 8 Contents of even.txt are : 4 Contents of odd.txt are : 4 – Samit Kapoor Mar 01 '21 at 12:25
  • @pmg Then should i use while( fscanf(odd, "%d", &num) == 1) in the odd.txt output and similarly in even.txt output? – Samit Kapoor Mar 01 '21 at 12:26
  • That looks better, yes. – pmg Mar 01 '21 at 12:27

0 Answers0