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

  1. #1

    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:
    #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;
    		printf("%-26s %4d %4d %4d %4d\n",
    return 0;
    thanx for any advice.

  2. #2
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Bangalore, India
    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.

    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-21-2008, 11:19 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Read data from file !!!
    By frankiepoon in forum C Programming
    Replies: 2
    Last Post: 10-14-2002, 11:45 PM
Website Security Test