Thread: Reading an input file into a struct.

  1. #1
    C Newbie
    Join Date
    Oct 2011
    Posts
    59

    Question Reading an input file into a struct.

    Code:
    #include <stdio.h>
    
    
    typedef struct
    {
    	char name[20];
    	short score;
    }Student_Record;
    
    
    int main()
    {
    	FILE * ptr;
    
    
    	Student_Record test;
    	int count, k;
    	test.name;
    	test.score;
    
    
    	ptr = fopen("input.txt", "r");
    
    
    	if(ptr == NULL)
    	{
    		printf("Open operation failed.\n\n\n");
    		return 1;
    	}
    
    
    	fscanf(ptr, "%d", &count);
    
    
    	for(k = 0; k < count; k++)
    	{
    		fscanf(ptr, "%s%d", &test.name, &test.score);
    	}
    
    
    	fclose(ptr);
    
    
    	printf("|   Test Results   |\n");
    	printf("|------------------|\n");
    	
    	for(k = 0; k < count; k++)
    	{
    		printf("|%s       |%d      |\n", test.name, test.score);
    	}
    
    
    	printf("|------------------|\n");
    
    
    	getch();
    	return 0;
    }
    I am pretty sure that I have my variables wrong, or something along those lines. I can compile+run without any errors, yet it evidently skips my for loop that is supposed to print the test.name's and test.score's.

    Can somebody point me in the right direction without blatantly giving me the answer? (Since this is the basis on what object oriented programming is going to be about, I would like to at least have a detailed explanation of what needs done if you can't lead me in the right direction for solving it myself.)

    Thanks in advance,
    ~Alan

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    $ gcc -Wall -g -std=c99  struct.c   -o struct
    struct.c: In function ‘main’:
    struct.c:18: warning: statement with no effect
    struct.c:19: warning: statement with no effect
    struct.c:37: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[20]’
    struct.c:37: warning: format ‘%d’ expects type ‘int *’, but argument 4 has type ‘short int *’
    Lines 18 and 19 can be removed. They don't actually do anything.
    Line 37: You don't need the & with strings (char arrays). The name of the array is already a pointer to the first element. Also, a short and an int are not the same to scanf. Read the scanf documentation to find out how to specify short.

    Then, look at your definition of test. It's a single Student_Record. You can't store 5 test scores in a single record, so it only remembers the last one you read. If you want to read all count entries, then print them later, you need an array of at least count elements.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    it helps if you post the contents of the input file and exactly what is printed for output.

  4. #4
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Quote Originally Posted by dmh2000 View Post
    it helps if you post the contents of the input file and exactly what is printed for output.
    Oops, I meant to do this.

    input.txt
    Code:
    4
    Bob     90
    Eric     88
    Sam     94
    Lara     83
    http://i.imgur.com/U1LD2.png

    And I think I made those corrections @anduril462

    Code:
    #include <stdio.h>
    
    
    typedef struct
    {
        char name[20];
        short score;
    }Student_Record;
    
    
    int main()
    {
        FILE * ptr;
    
    
        Student_Record test[20];
        int count, k;
    
    
        ptr = fopen("input.txt", "r");
    
    
        if(ptr == NULL)
        {
            printf("Open operation failed.\n\n\n");
            return 1;
        }
    
    
        fscanf(ptr, "%d", &count);
    
    
        for(k = 0; k < count; k++)
        {
            fscanf(ptr, "%s%h", test[k].name, &test[k].score);
        }
    
    
        fclose(ptr);
    
    
        printf("|   Test Results   |\n");
        printf("|------------------|\n");
        
        for(k = 0; k < count; k++)
        {
            printf("|%s       |%d      |\n", test[k].name, test[k].score);
        }
    
    
        printf("|------------------|\n");
    
    
        getch();
        return 0;
    }
    EDIT: Updated the input.txt and the image.
    Last edited by Alan Gott; 11-29-2011 at 08:31 PM.

  5. #5
    C Newbie
    Join Date
    Oct 2011
    Posts
    59
    Oops, didn't fully update the txt.
    Code:
    4
    Alan    90
    Eric    88
    Dann    94
    Lara    83
    EDIT #2: ... /FACEPALM... I obviously don't know the identifier for SHORT, so I changed them all to INTs and it worked... FML.
    Last edited by Alan Gott; 11-29-2011 at 09:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading struct from file
    By seandil666 in forum C Programming
    Replies: 6
    Last Post: 12-07-2009, 10:32 PM
  2. Reading a txt file into a struct problem
    By Swerve in forum C++ Programming
    Replies: 10
    Last Post: 03-19-2008, 12:56 AM
  3. having trouble reading into Struct from File
    By bcianfrocca in forum C++ Programming
    Replies: 9
    Last Post: 09-06-2005, 10:54 PM
  4. reading into Struct from file
    By bcianfrocca in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2005, 07:38 PM
  5. Poblem reading struct in a file
    By The_Kingpin in forum C Programming
    Replies: 1
    Last Post: 10-24-2004, 07:02 PM