how do i read this data from a file instead of this array

This is a discussion on how do i read this data from a file instead of this array within the C Programming forums, part of the General Programming Boards category; I want to read this data from a file and perform the same task that it is performing right now. ...

  1. #1
    Unregistered
    Guest

    how do i read this data from a file instead of this array

    I want to read this data from a file and perform the same task that it is performing right now. I just want to know how i can do it from a file. Here is the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define NUM_STU 5
    
    	typedef struct
    	{
    	 char name[26];
    	 int  midterm[3];
    	 int  final;
    	} STUDENT;
    
    
    
    int main (void)
    {
    	STUDENT *pStuPtr;
    	STUDENT stuAry[NUM_STU] =
    	{
    		{"Charles, George",{85, 94, 79}, 93},
    		{"Adams, Karin",   {75, 91, 89}, 89},
    		{"Nguyen, Tuan",   {87, 88, 89}, 90},
    		{"Oh, Bill",       {78, 96, 88}, 91},
    		{"Chavez, Maria",  {83, 79, 93}, 91},
    	};
    
    	printf("Unsorted data:\n");
    	for (pStuPtr = stuAry;
    	     pStuPtr < stuAry + NUM_STU;
    		 pStuPtr++)
    		printf("%-26s %4d %4d %4d %4d\n",
    		pStuPtr->name,
    		pStuPtr->midterm[0],
    		pStuPtr->midterm[1],
    		pStuPtr->midterm[2],
    		pStuPtr->final);
    		printf("\n");
    
    return 0;
    }
    thanx for any advice.

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    Depends on what way you wrote the data into the file, which is to be read. If you wrote it with fwrite() then use fread() to read it. If you wrote it with fprintf() then use fscanf() to read it.

    Code:
    fread(stuAry, sizeof(stuAry), 1, fp);
    fscanf(fp, "%s %d %d %d %d", ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  3. Replies: 1
    Last Post: 09-10-2005, 07:02 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. Read data from file !!!
    By frankiepoon in forum C Programming
    Replies: 2
    Last Post: 10-15-2002, 12:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21