Link to home
Start Free TrialLog in
Avatar of icysmarty
icysmarty

asked on

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.
Avatar of waelothman
waelothman

simply use fget(f,array[i], sizeof(data))
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();
Avatar of icysmarty

ASKER

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


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?
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++.
sorry i messed the file paramter as well as i messed & before the integers
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
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;
}
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?
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++;
         
     }

the loop say for i =0  and whiel i < 0 and this not happend
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).
ASKER CERTIFIED SOLUTION
Avatar of waelothman
waelothman

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Say I have

name             salary                   id
-----------------------------------------
can I discard this when reading the data?
or I should store then in a char array?
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
or
while( fgetc(input_file) != '\n'); //to skip the 1st line
while( fgetc(input_file) != '\n'); //to skip the 2st line
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.
use
fgets(s,80,input_fil);
 where 80 is the line width
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?
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);