Problems with assignin values to an array of structs...

This is a discussion on Problems with assignin values to an array of structs... within the C Programming forums, part of the General Programming Boards category; Hello World! I am trying to read the contents of a file, that stores information on incoming messages. Each message ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    19

    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 10:44 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    19

    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...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 04-12-2009, 06:49 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 01:56 AM
  3. better way to scanf of a array of structs member?
    By stabu in forum C Programming
    Replies: 3
    Last Post: 07-17-2008, 05:51 PM
  4. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 07:30 AM
  5. Duplicate values in Array
    By TONYMX3 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2002, 03:57 PM

Tags for this Thread


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