0

hope you can help me with this, it's kinda tricky... i have txt file which contents names and marks of peple

for an example

Carl,Johnson,123xxx123,9;

like in order: name,surname,certificate no,mark;

struct xxxxx
{
    char name[20];
    char surname[20];
    char certificate No[10];
    int mark;
};

the problem starts when i need to read ten records from txt...

don't have clue how to duplicate struct, do i have to define it like ten times

struct xxxx sk1[ ],sk2[ ]... and so on...?

r_ahlskog
  • 1,916
  • 1
  • 17
  • 26

1 Answers1

3

Once you have defined a struct you can declare several variables of that type.

struct xxxx sk1;
struct xxxx sk2;

or even an array of them

struct xxxx sk[10];

To populate this structure with data from the text file you need to write some code, formatted as the input is I would think fscanf() would not be of much help as is.

The approach I would take is probably reading the entire the line using fscanf() then do some splitting on ',' to get the fields. The first three are plain strcpy, the last one would take use of atoi

My grasp of C input functions are not all that good as I much prefer C++ so you should perhaps look for a second opinion in this matter.

Edit: I found some perfectly good info on how to proceed from your old questions

Community
  • 1
  • 1
r_ahlskog
  • 1,916
  • 1
  • 17
  • 26
  • yes, but i actually intrested in the way how to read information into struct from file... –  Nov 22 '11 at 20:43
  • @edGGars I updated the answer a bit, still maybe a new question is called for. Do some code along the lines of my suggestion, post a question include the txt file format/example, the struct definition and the parsing code as far as you get it. And someone should pick it up while I sleep. – r_ahlskog Nov 22 '11 at 22:35