GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Oct-2009, 17:02
iblink iblink is offline
New Member
 
Join Date: Nov 2006
Posts: 23
iblink has a little shameless behaviour in the past
Question

Help reading from a file into a structure array


Hello. I am trying to read info from a file into an array of a created structure. After running the program on visual studio 2008, it gives me no syntax error but this dialog box appears "debug assertion failed". Can anyone look at my code and tell me the problem.. Thanks in advance.

CPP / C++ / C Code:
#include<stdio.h>

//void display();
int main()
{
	struct persalary
	{
		int month;
		long salary;
	};
	struct employee
	{
		int empnr;
		char name[12];
		struct persalary salaries[3];
		long total;
		float average;
	}person[3];
	int i=0;

	FILE *emp_file;
	emp_file = fopen("emp.txt","r");

	do{
	fscanf(emp_file,"%d %s %d %ld %d %ld %d %ld", &person[i].empnr, person[i].name, &person[i].salaries[0].month,
			&person[i].salaries[0].salary,&person[i].salaries[1].month, &person[i].salaries[1].salary,
			&person[i].salaries[2].month, &person[i].salaries[2].salary);
	i++;
	}while(feof(emp_file)==0);
	fclose(emp_file);

	return 0;
}

The content of the file is as follows:
Code:
121 Alex 2 300000 4 400000 12 750000 234 Andy 3 250000 6 800000 7 475000 456 Mart 4 750000 5 880000 10 660000 875 Steven 2 950000 4 660000 9 775000
Last edited by admin : 29-Oct-2009 at 03:39. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 28-Oct-2009, 17:24
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Help reading from a file into a structure array


You are trying to read 4 students into an array that can only hold 3.
  #3  
Old 28-Oct-2009, 17:30
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help reading from a file into a structure array


You declare an array of three structs ant then you try to store data for more than three.

Try something like the following to see how it goes:

CPP / C++ / C Code:
    do {
        int n = 
        fscanf(emp_file, "%d %s %d %ld %d %ld %d %ld", &person[i].empnr,
               person[i].name, &person[i].salaries[0].month,
               &person[i].salaries[0].salary, &person[i].salaries[1].month,
               &person[i].salaries[1].salary, &person[i].salaries[2].month,
               &person[i].salaries[2].salary);
        printf("i = %d, n = %d\n", i, n);
        i++;
    } while (feof(emp_file) == 0);

If you increase you dimension to more than four, it shouldn't crash the way that it did, but I'm thinking that you should see why your loop is not right.

For example, I get the following:
Code:
i = 0, n = 8 i = 1, n = 8 i = 2, n = 8 i = 3, n = 8 i = 4, n = -1

With four lines in the file, it goes through the loop five times. (The eof flag is not set until after it tries to read past the end of the file.)

That topic (improper use of eof as a loop control) has been covered numerous times, but I know it's hard to believe that it simply doesn't work the way that you want it to without seeing it for yourself.

If get past the crash (by increasing the size of the array) but you still can't find how to make the loop work properly, let us know, and maybe someone can give some more clues.


Bottom line: Always (yes, always test the result of a read operation to see that the proper number of items was read!

Regards,

Dave
  #4  
Old 28-Oct-2009, 17:40
iblink iblink is offline
New Member
 
Join Date: Nov 2006
Posts: 23
iblink has a little shameless behaviour in the past

Re: Help reading from a file into a structure array


Thank You very much.. Works great after increasing the size of the array person[] by 1. I do understand well how reading the file works, thanks to your example.
  #5  
Old 28-Oct-2009, 18:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help reading from a file into a structure array


Quote:
Originally Posted by iblink
...I do understand well how reading the file works....

So did you change your loop so that it does not attempt to store five things from your four-line file? I mean, even if it happens not to actually crash the program, you need to read exactly what the file has. Neither more nor less.

Regards,

Dave
  #6  
Old 28-Oct-2009, 18:18
iblink iblink is offline
New Member
 
Join Date: Nov 2006
Posts: 23
iblink has a little shameless behaviour in the past

Re: Help reading from a file into a structure array


exactly what i do.. works fine.. thx
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Power Calibration Error In Nero Fix (hopefully) matt3678 Computer Hardware Forum 60 20-Aug-2009 06:04
contents of .txt file into 2D array anirudhroxrulz C Programming Language 4 10-Apr-2008 23:45
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 21:03
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.