Read fields

Hi,

I am trying to read a file and store the fields in different arrays. I have tried using fgetc, fgetw but I am not able to separte the fields.

Eg:
Following is the file that i want to read

Name      Salary     ID
-------     --------    -----'
john        1000       1234
karen      2000        2345
Mary       3000        1112

I create a array of struc called data.

typedef struct data{
string name;
double salary;
int ID;
}data_obj;

then I create an array of this struct
data array[5];

I know how to open, close , read the file but can't figure out how to correctly store each of the field in the array.
Thank you.
icysmartyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
waelothmanConnect With a Mentor Commented:
pleas don't forget & as i made
 fscanf(input_file,"%s%f%d",household[i].id_number,&household[i].annual_income,&household[i].members_of_household);
0
 
waelothmanCommented:
simply use fget(f,array[i], sizeof(data))
0
 
jhshuklaCommented:
ifstream file("foo.dat");
data_obj Data[5];

for(int i=0;i<5;i++)
  file >> Data[i].name >> Data[i].salary >> Data[i].ID;

file.close();
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
icysmartyAuthor Commented:
If I am using C programming, can I use fscanf?
Do you care to explain why declare data_obj Data[5].
This is how i read the file

FILE* input_file;
input_file = fopen("foo.dat", "r");
//check the error opening ..
  ...
 ...
...
call the function to read the data
0
 
waelothmanCommented:
if the are using FILE * there is 2 way to  open fopen("foo.dat", "r"); for text file or fopen("foo.dat", "rb"); for binary
if you using text file format then you can user fsacnf("%s%d%d",Data[i].name,Data[i].salary,Data[i].id)

provided that name contain no space

you can user data Data[5].


0
 
icysmartyAuthor Commented:
Should fscanf takes the FILE* as parameter too? fscanf takes 3 parameters right?
All I get is zeros... so, I think the pointer of the file does not traverse through the data.

can I use fread to read how many rows of data do I have?
0
 
jhshuklaCommented:
struct data array[5];
same as
data_obj Data[5];
just different variable name.

>> If I am using C programming, can I use fscanf?
sure.
fscanf(input_file, "%s %f %d ", array[i].name, &array[i].salary, &array[i].ID);
I used %f because I don't remember the format string for double. replace with appropriate code.

>> provided that name contain no space
Yeah. that's important!
If name can contain spaces then, try to read in a float. if it fails, append the string. retry until salary is successfully read. post a comment if help needed.

You asked the question in C++; so I replied in C++.
0
 
waelothmanCommented:
sorry i messed the file paramter as well as i messed & before the integers
0
 
waelothmanCommented:
if you made the application to write and read from the file then you can put the length of the string in 1st colmn so you can user fgets to return it agian with its size
0
 
icysmartyAuthor Commented:
This is what I have... but it doesn't work ...
I have a question.
How does the pointer of the file traverse through the data?
From here, I see that fscanf is a reading a filed at a time... and how does it know what is theh next field?
Also, how to read end of the line?

int store_data(FILE *input_file){
      
                int count = 0;
      for ( i = 0; i< count; i++)
      {
      fscanf(input_file,"%s%f%d",array[i].name,array[i].salary,array[i].id);
                                count++;
            
      }

      return count;
}
0
 
icysmartyAuthor Commented:
The function that I post above shows nothing when I run it...
This is because i think the pointer is not placed at the first byte of the input_file.
Can I use read?
0
 
waelothmanCommented:
of corese loop not work

while (! foef(input_file)  )
   {
     fscanf(input_file,"%s%f%d",array[i].name,array[i].salary,array[i].id);
                                count++;
         
     }

0
 
waelothmanCommented:
the loop say for i =0  and whiel i < 0 and this not happend
0
 
icysmartyAuthor Commented:
okay.. so, now I remove the for loop ...

size_t i=0;
      
      while(!feof(input_file))
      {
            fscanf(input_file,"%s%f%d",household[i].id_number,household[i].annual_income,household[i].members_of_household);
            i++;
      }

      return i;

This function returns an error when executing.. ( i believe it is pointer problem).
0
 
icysmartyAuthor Commented:
You are right, i should have include the &
Weird though.. it reads the header and store them as an integer. ...
It suppose to read "name" then "salary" and "id"
I step through it, and it is actually reading 'na', 'a', 'm', 'e'...
so my household[i].id_number is somehow equal to (int)n
0
 
icysmartyAuthor Commented:
Say I have

name             salary                   id
-----------------------------------------
can I discard this when reading the data?
or I should store then in a char array?
0
 
waelothmanCommented:
you shold discard this first in string like

fgets(s,80,input_fil);
wehere s is char array and 80 is the length of line +1 (for the new line) count it by your self
0
 
waelothmanCommented:
or
while( fgetc(input_file) != '\n'); //to skip the 1st line
while( fgetc(input_file) != '\n'); //to skip the 2st line
0
 
icysmartyAuthor Commented:
I want to read the header and line as well, so I can print the exact data I read out to the screen.
I try to read the header and line inside the while loop but it somehow does not work.
The error "assertion fail" is displayed.
0
 
waelothmanCommented:
use
fgets(s,80,input_fil);
 where 80 is the line width
0
 
icysmartyAuthor Commented:
Perfect, it is working now.
Can I ask you last question.
This function works when the data are organized row by row.
What if I have something like

No.1
John
         address     xxabc     go.get  

In c++, i believe I can use >>  to read the next string, but not sure if it can read the next word in a new line.
what can we use in C?
0
 
waelothmanCommented:
you can fascanf many string and neglict what you don't want and get what you want like
fscanf(file,"%s%s%s%s%s",No,Name,Addressword,Address,Second);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.