![]() |
| | #1 |
| Registered User Join Date: Dec 2008
Posts: 13
| 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;
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++;
}
}
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! Last edited by laczfinador; 05-09-2009 at 09:44 AM. |
| laczfinador is offline | |
| | #2 |
| subminimalist 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); 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 | |
| | #3 |
| Registered User Join Date: Dec 2008
Posts: 13
| 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 | |
![]() |
| Tags |
| array, array of structures, structure |
| Thread Tools | |
| Display Modes | |
|
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 |