Thread: Questions relating to FILE I/O

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    23

    Questions relating to FILE I/O

    Trying to open file and get it calculate and print gpa and class.

    1) Should I make getinfo process both print to file and print to screen?

    2) How do I open the file, read the file and put data onto a variable?

    3) how exactly would I use the putchar and fputc in this code?

    4) How do to check for error opening the file?


    Code:
    #include <stdio.h>
    #include <stdlib.h> //Remove if not in use
    #include <ctype.h> //Remove if not in use
    #include <iostream>
    #define   FLUSH   while ( getchar() != '\n' );
    
    void getchoice (void);
    //void tofile (void);
    //void toscreen (void);
    void getinfo (void);
    
    int main (void)
    {
        int oneortwo;
        getchoice();
    
        system ("pause");
        return 0;
    }
    
    void getchoice (void)
    {
        int oneortwo;
    
        do
        {
            printf ("Please select where to print the transcript: [1] To file. [2] To screen.\nPlease only select between choices \"1\" or \"2\"\n");
            scanf_s ("%d", &oneortwo);
            if (oneortwo == 1)
            {
                printf ("%d: You have chosen to print to file.\n", oneortwo);
                // Call function
            }
            else if (oneortwo == 2) 
            {
                printf ("%d: You have chosen to print to screen.\n", oneortwo);
                // Call function
            }
            else
            {
                printf ("Invalid entry.\n");
                FLUSH;
            }
        } while (oneortwo != 1 && oneortwo != 2);
    }
    
    
    /*
    void tofile (void)
    {
        FILE* spOut;
        float units;
        float gpa;
    
        spOut = fopen("record.txt", "r");
        
        if ((spOut = fopen("record.txt", "r")) == NULL) // Or if(!spIn)?
        {
            printf ("Error opening 'record.txt'\n");
        }
    
        while ((fscanf_s(spOut, "%*15c%*15c%*15c:%f:%f\n", &units, &gpa)) != EOF)
        {
            fprintf (spOut, "Units: %f\n GPA: %f\n", units, gpa);
        }
        
        fclose (spOut);
    }
    */
    
    void getinfo (void)
    {
        FILE* spIn;
        FILE* spOut;
        float units;
        float gpa;
    
        spIn = fopen("record.txt", "r");
    
        if ((spIn = fopen("record.txt", "r")) == NULL) // Or if(!spIn)?
        {
            printf ("Error opening 'record.txt'\n");
        }
    
        while ((fscanf_s(spIn, "%*15c%*15c%*15c:%f:%f\n", &units, &gpa)) != EOF)
        {
            printf ("Units: %f\n GPA: %f\n", units, gpa);
        }
        
        fclose (spIn);
    }
        /*
        if (gpa >= 3.99 && gpa <= 4.01)
            // = 'A';
        else if (gpa >= 3.66 && gpa <= 3.68)
            // = 'A-';
        else if (gpa >= 3.32 && gpa <= 3.34)
            // = 'B+';
        else if (gpa >= 2.99 && gpa <= 3.01)
            // = 'B';
        else if (gpa >= 2.66 && gpa <= 2.68)
            // = 'B-';
        else if (gpa >= 2.32 && gpa <= 3.34)
            // = 'C+';
        else if (gpa >= 1.99 && gpa <= 2.01)
            // = 'C';
        else if (gpa >= 1.66 && gpa <= 1.68)
            // = 'D+';
        else if (gpa >= 1.32 && gpa <= 1.34)
            // = 'D';
        else if (gpa >= 0.99 && gpa <= 1.01)
            // = 'D-';
        else
            // = 'F';
        */
    
        /*
        sumgpa = gpa1 + gpa2 + gpa3 + gpa4 + gpa5 + gpa6 + gpa7 + gpa8 + gpa9 + gpa10;
        sumunits = units1 + units2 + units3 + units4 + units5 + units6 + units7 + units8 + units9 + units10; 
        averagegpa = sumgpa / sumunits;
        
        printf("You've attempted %.1f units and received an overall GPA of %.2f.\n", sumunits, gpa);
        */

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Magic staple View Post
    Trying to open file and get it calculate and print gpa and class.

    1) Should I make getinfo process both print to file and print to screen?
    That is entirely up to you. It's obvious the data will have to be presented to the user somehow.

    2) How do I open the file, read the file and put data onto a variable?
    You were half way there with fscanf() ... look it up in the C Libarary Documentation for your specific compiler and pay attention to the formatting symbols. (If you don't have this documentation, get it.)

    3) how exactly would I use the putchar and fputc in this code?
    You may not need to. In most cases printf() and fprintf() are far better candidates. Again, look these up in your Library Docs.


    4) How do to check for error opening the file?
    You test the FILE pointer returned by fopen(). If it is NULL the file didn't open and you should abort the process. If it is not NULL you can assume it's open.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More questions (relating to global doubts)
    By Jorl17 in forum C++ Programming
    Replies: 29
    Last Post: 12-03-2008, 11:40 AM
  2. Relating classes using composition
    By clegs in forum C++ Programming
    Replies: 12
    Last Post: 11-06-2007, 12:56 AM
  3. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  4. Replies: 26
    Last Post: 03-20-2004, 02:59 PM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM