Thread: Write a C program to write even and odd integers into different files.

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    8

    Write a C program to write even and odd integers into different files.

    Create three files namely all_numbers, odd_number and even_number.
    Get the number of records and the numbers to store from the user.
    Save all the numbers in “all_numbers” file. Read each number and check whether it is odd or even.
    Store the odd numbers in odd_number file and even numbers in even_number file.
    Finally, print the numbers stored in odd_number and even_number files.
    The output of the program will look like this.

    How many records that you want to store :41
    2
    3
    4
    The data are written too the respective files.

    The even numbers are 2 4
    The odd numbers are 1 3
    Please help me on this.Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What approaches have you considered? What is your current code and how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    8
    i don't have any code.i just want a c program and i want a c source code for Borland c++.Please tell me a program.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to the homework policy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Yoges View Post
    i don't have any code.i just want a c program and i want a c source code for Borland c++.Please tell me a program.
    So you want us to make you a C program for you from scratch and then you also want us to make sure that it works on Borland C++ compiler. This board is not a free code developing service. If you show effort we can help, if not maybe offer to pay someone to do this??

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    8

    I wrote the following C program.

    Code:
    #include <stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    main()
    {
       int n=0,i,n1;
       char ch;
       FILE *fptr,*fptr_e,*fptr_o;
       clrscr();
        fptr=fopen("C:/TCWIN45/BIN/all_num.txt","w");
        fptr_e=fopen("C:/TCWIN45/BIN/even.txt","w");
       fptr_o=fopen("C:/TCWIN45/BIN/odd.txt","w");
       if(fptr==NULL){
          printf("Error!");
            exit(1);
       }
       printf("How many record that you want to store : ");
       scanf("%d",&n);
       for(i=1;i<=n;i++)
        {
        scanf("%d",&n1);
        fprintf(fptr,"%d\n",n1);
        if(n1%2!=0)
        {
           fprintf(fptr_o,"%d\n",n1);
        }
        else
        {
           fprintf(fptr_e,"%d\n",n1);
        }
        }
    printf("The data are written to the respective file\n\n");
    printf("The Even Number are: \t");
    fptr_e=fopen("C:/turboc++/even.txt","r");
    while(ch=getc(fptr_e)!=EOF)
        {
        printf("%c\t",ch);
        }
    fptr_o=fopen("c:/turboc++/odd.txt","r");
    while(ch=getc(fptr_o)!=EOF)
        {
        printf("%c\t",ch);
        }
    getch();
       fclose(fptr);
    
    
       return 0;
        }
    And the output which i want is as follows:

    How many records that you want to store :4
    1
    2
    3
    4
    The data are written too the respective files.

    The even numbers are 2 4
    The odd numbers are 1 3


    But it gives some error after entering 1 2 3 4 .Error i got is something "General Protection Exception Processor Fault".Pls tell me why i got this error & how to eliminate it.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent your code properly, e.g.,
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    main()
    {
        int n = 0, i, n1;
        char ch;
        FILE *fptr, *fptr_e, *fptr_o;
        clrscr();
        fptr = fopen("C:/TCWIN45/BIN/all_num.txt", "w");
        fptr_e = fopen("C:/TCWIN45/BIN/even.txt", "w");
        fptr_o = fopen("C:/TCWIN45/BIN/odd.txt", "w");
        if (fptr == NULL) {
            printf("Error!");
            exit(1);
        }
    
        printf("How many record that you want to store : ");
        scanf("%d", &n);
        for (i = 1; i <= n; i++)
        {
            scanf("%d", &n1);
            fprintf(fptr, "%d\n", n1);
            if (n1 % 2 != 0)
            {
                fprintf(fptr_o, "%d\n", n1);
            }
            else
            {
                fprintf(fptr_e, "%d\n", n1);
            }
        }
        printf("The data are written to the respective file\n\n");
    
        printf("The Even Number are: \t");
        fptr_e = fopen("C:/turboc++/even.txt", "r");
        while (ch = getc(fptr_e) != EOF)
        {
            printf("%c\t", ch);
        }
        fptr_o = fopen("c:/turboc++/odd.txt", "r");
        while (ch = getc(fptr_o) != EOF)
        {
            printf("%c\t",ch);
        }
        getch();
        fclose(fptr);
    
        return 0;
    }
    A few things to note:
    • <conio.h>, clrscr and getch are non-standard
    • Explicitly declare the main function as returning an int, i.e., write int main(void). The void here explicitly states that the main function takes no arguments.
    • Check that all the files were opened, not just one.
    • Check the return value of scanf.
    • When done, close the files.
    • You appear to be writing to files with one file path and then reading from files of a different file path, e.g., "C:/TCWIN45/BIN/even.txt" versus "C:/turboc++/even.txt". What you can do is to declare a named constant for the filename, then use it in both cases.
    • getc returns an int, so ch should be declared to be an int. Furthermore, (ch = getc(fptr_e) != EOF) is equivalent to (ch = (getc(fptr_e) != EOF)), but what you want is ((ch = getc(fptr_e)) != EOF)
    • Actually, since you are supposed to print the numbers, you should be using scanf and printf with %d, not getc and printf with %c. Alternatively, you can use fgets, sscanf and printf with %d, but that may be overkill here.
    Last edited by laserlight; 09-09-2014 at 01:37 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    8

    Output is not formatted accordingly.

    Code:
    #include <stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    main()
    {
        int n=0,i,n1;
        char ch;
        FILE *fptr,*fptr_e,*fptr_o;
        clrscr();
        fptr=fopen("C:\\all_num.txt","w");
        fptr_e=fopen("C:\\even.txt","w");
        fptr_o=fopen("C:\\odd.txt","w");
        if(fptr==NULL){
            printf("Error!");
            exit(1);
        }
        printf("How many record that you want to store : ");
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
        scanf("%d",&n1);
        fprintf(fptr,"%d\n",n1);
        if(n1%2!=0)
        {
            fprintf(fptr_o,"%d\n",n1);
        }
        else
        {
            fprintf(fptr_e,"%d\n",n1);
        }
        }
        fclose(fptr_e);
        fclose(fptr_o);
    printf("The data are written to the respective file\n\n");
    printf("The Even Number are:\t");
    
    
    
    
    fptr_e=fopen("c://even.txt","r");
    while((ch=getc(fptr_e)) !=EOF)
    printf("%c",ch);
    fclose(fptr_e);
    printf("\nThe Odd Number are:\t");
    fptr_o=fopen("c://odd.txt","r");
    while((ch=getc(fptr_o)) !=EOF)
        printf("%c",ch);
    fclose(fptr_o);
    getch();
    fclose(fptr);
    
    
    return 0;
    }
    ok i wrote this final i even got the output but Output is not formatted accordingly.i want the following output:

    The even numbers are 2 4
    The odd numbers are 1 3

    only these two lines i didn't get correctly.All others are fine.even i tried so many things but not getting the output as above for those two lines.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I would strongly recommend you actually listen to the advice laserlight gave you, and apply it - this would not only improve your code, but it would fix the problem you're asking about.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    So you asked this questions elsewhere, took the crappy code that was handed to you, and claimed it as your own on yet another cross-post.

    Then I'm guessing you tried to modify that crappy code you stole, and it still didn't working, leading to yet more cross-posting.

    Have you tried sitting down with a book and just learning this stuff on your own?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-01-2012, 08:00 AM
  2. Replies: 2
    Last Post: 09-25-2012, 10:54 PM
  3. Replies: 3
    Last Post: 02-23-2012, 11:35 PM
  4. Drag and Drop files/Read and write files
    By ScoutDavid in forum C Programming
    Replies: 2
    Last Post: 01-13-2011, 12:14 PM
  5. problem with fout.write: cannot write 0x0A (10 dec)
    By yvesdefrenne in forum C++ Programming
    Replies: 7
    Last Post: 05-23-2005, 12:53 PM

Tags for this Thread