Thread: Storing information in a file

  1. #1
    Unregistered
    Guest

    Question Storing information in a file

    I need to modify my existing program to read its data from a file. Each data line should have the following separated by a space:
    Firstname, Lastname, ID number, 3 test scores.
    I am not sure how to do this, any examples or clues would be appreciated.




    include <stdio.h>
    #include <string.h> // a string will be used for name input
    void main () {

    char firstname [40];
    char lastname [40];
    char fullname [80];
    char grade; // declaration of varibles
    int score1, score2, score3,idnum;
    float average;

    printf("Enter your first name: ");
    scanf("%s", firstname);

    printf("Enter your last name: ");
    scanf("%s", lastname);

    printf("Enter the last 4 of your SS #: ");
    scanf("%d", &idnum);

    printf("Enter first score ");
    scanf ("%d",&score1);

    printf("Enter second score ");
    scanf("%d", &score2);

    printf("Enter third score ");
    scanf("%d", &score3);

    average = (score1 + score2 + score3) / 3.0; //calculates average of scores


    if (average <= 60) grade ='F';
    if (average >= 60 && average < 70) grade = 'D'; // assigns letter grade to numerical value if (average >= 70 && average < 80) grade = 'C';
    if (average >= 70 && average < 80) grade = 'C';
    if (average >= 80 && average < 90) grade = 'B';
    if (average >= 90 && average < 100) grade = 'A';

    strcpy(fullname, lastname);
    strcat(fullname,", ");
    strcat(fullname, firstname); // string to store name first-last and display last name first


    printf("%s \t %d \t %d \t %d \t %d \t %.2f \t %c\n\n",fullname, idnum, score1, score2, score3, average, grade); // output to screen




    } //end main

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void main () {
    This should be
    int main ( ) {

    > I need to modify my existing program to read its data from a file.
    Do this
    FILE *fp = fopen ( "file.txt", "r" );

    Then change all your scanf calls like so
    scanf("%s", firstname);
    to
    fscanf( fp, "%s", firstname);


    Then you probably need to figure out how to make it loop until the end of file....

  3. #3
    Unregistered
    Guest

    stream does not equal NULL

    I have tried the suggestions you posted. When I run the program I get the error "stream!=NULL." I do not know how to make it loop to the end. Could this be the cause?

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Well, to test for the end of the file, you use the EOF. For instance:
    Code:
    if (iNum == EOF)
    {
        printf("End of file\n");
        break;
    }
    
    fclose(fp);
    Understand?

    --Garfield
    1978 Silver Anniversary Corvette

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > When I run the program I get the error "stream!=NULL."
    This probably means that you didn't open the file

    You did this
    FILE *fp = fopen ( "file.txt", "r" );

    But you didn't do this
    if ( fp == NULL ) {
    // error
    }

    Before you tried to do this
    fscanf( fp, "%s", firstname);

    Post your code

  6. #6
    Unregistered
    Guest

    Question

    Salem,
    I tried the things you recommended but I have had no luck. I have a few holes in my understanding of the C language and I am sure it is obvious. Can you see what I am doing wrong?



    // Purpose:user inputs firstname, lastname, three test scores then calculate numerical average, assign lettergrade and output lastname, firstname three testscores, and lettergrade

    #include <stdio.h>
    #include <string.h> // a string will be used for name input
    int main () {

    char firstname [40];
    char lastname [40];
    char fullname [80];
    char grade; // declaration of varibles
    int score1, score2, score3,idnum;
    float average;


    FILE *fp = fopen("file.txt","r");

    if ( fp == NULL ) {



    printf("Enter your first name: ");
    fscanf(fp, "%s", firstname);

    printf("Enter your last name: ");
    fscanf(fp, "%s", lastname);

    printf("Enter the last 4 of your SS #: ");
    fscanf(fp, "%d", &idnum);

    printf("Enter first score ");
    fscanf (fp, "%d",&score1);

    printf("Enter second score ");
    fscanf(fp, "%d", &score2);

    printf("Enter third score ");
    fscanf(fp, "%d", &score3);

    average = (score1 + score2 + score3) / 3.0; //calculates average of scores






    if (average <= 60) grade ='F';
    if (average >= 60 && average < 70) grade = 'D'; // assigns letter grade to numerical value if (average >= 70 && average < 80) grade = 'C';
    if (average >= 70 && average < 80) grade = 'C';
    if (average >= 80 && average < 90) grade = 'B';
    if (average >= 90 && average < 100) grade = 'A';


    strcpy(fullname, lastname);
    strcat(fullname,", ");
    strcat(fullname, firstname); // string to store name first-last and display last name first



    printf("%s \t %d \t %d \t %d \t %d \t %.2f \t %c\n\n",fullname, idnum, score1, score2, score3, average, grade); // output to screen


    }

    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try
    if ( fp != NULL ) {

    > FILE *fp = fopen("file.txt","r");
    file.txt was just my example
    change the name to be the actual name of the file you're trying to read from

    > printf("Enter your first name: ");
    Since you're reading from a file, you can remove these prompts.
    It will still work with them in, you'll just see a lot of text being printed as the file is read though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM