C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-09-2009, 09:26 AM   #1
Registered User
 
Join Date: Dec 2008
Posts: 13
Question [SOLVED] Problems with assignin values to an array of structs...

Hello World!

I am trying to read the contents of a file, that stores information on incoming messages. Each message consists of two lines: the first containing information about the reception time, and the sender, and the second line containing the message itself. I am trying to store the contents of the file in an array of structures. My structure definiton looks as follows:

Code:
int i,j;
struct msg{
	int h;
	int m;
	int phone;
	char content[101];
};
char current_line[100];
FILE *src_file;
My attempt at getting the information from the file into the array looks like this:

Code:
i=1;
j=0;
while(fgets(current_line,100,src_file)){
	if(i%2==1){    /*because every unpaired row contains information about when did the message arrive and from where, I only want to work with those rows...*/
		sscanf(current_line,"%d%d%d",data[j].h,data[j].m,data[j].phone);
                j++;
	}
}
Here is how my text file looks...

Code:
9 11 123456789

Szia, mikor jossz?

9 13 434324223

Nem kerek ebedet!

9 14 434324223

Hova menjek erted?

9 16 343567452

Nem erek oda idoben. Hivd fel a fonokot es ments ki!
The first number is the hour, the second is the minute and the third is the telephone number. Every second line contains the message. The program compiles, but when run, it gives a segmentation fault. I found some related threads, but couldn't get the right conclusion on what should be the problem here... any help, would be appreciated! Thanks!

Last edited by laczfinador; 05-09-2009 at 09:44 AM.
laczfinador is offline   Reply With Quote
Old 05-09-2009, 09:37 AM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
You should use the "address of" operator (&) with int inputs in scanf type functions:
Code:
sscanf(current_line,"%d%d%d",&data[j].h,&data[j].m,&data[j].phone);
Also: notice that stuff in "code" tags does not get line wrapped, so please please please NEVER AGAIN use long, unbroken comment lines in code tags.

Plus: in the example you posted that data is every 5th line...
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 05-09-2009, 09:42 AM   #3
Registered User
 
Join Date: Dec 2008
Posts: 13
Thumbs up

Ouch! I can't believe I missed that - again! I always forget that ampersand character, when working with arrays... anyways, thanks for the quick reply! Have a nice day man! I'll be on a lookout for long comment lines too...
laczfinador is offline   Reply With Quote
Reply

Tags
array, array of structures, structure

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
displaying user input values of an array by calling functions... PLEASE HELP ME!!! hiddenprophecy C Programming 12 04-12-2009 05:49 PM
Have problems with copying my array! AvaGodess C Programming 11 09-25-2008 12:56 AM
better way to scanf of a array of structs member? stabu C Programming 3 07-17-2008 04:51 PM
can't assign proper values to an array of string Duo C Programming 1 04-04-2005 06:30 AM
Duplicate values in Array TONYMX3 C++ Programming 2 01-30-2002 03:57 PM


All times are GMT -6. The time now is 10:32 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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