Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pointing to array of structure and file i/o help/

Status
Not open for further replies.

tru178

Technical User
Dec 8, 2001
6
US

Hi. I'm trying to write this program where i read data from a file to
an array of structure. here are parts of it.
----------------------------------------------------------
struct employee
{
char name[20];
int empnumber;
};
employee file[20];

void file2(employee *c);

main()
{
employee *fileptr;
fileptr=file;

file2(fileptr); // the function file2 is located on a different file
named file2.cpp.
}

---------------------------------------------------------------
file2(employee *c)
{
ifstream input;

input.open("data.txt",ios::in);

if (!input)
cout << &quot;error&quot;;
else
while (!input.eof())
{
for (int i=0; i<=20; i++)
{
input.getline(c->name,20);
input >>(c->empnumber);
}
}
input.close();
}


I'm a beginner. anyone know what's wrong with this?
i get the error in file2 that &quot;error C2227: left of '->name' must
point to class/struct/union.&quot;
the c->name and c->empnumber above has an index of i. for some reason, it didn't show up in the post.
 
The problem seems to be that the function in your second file doesn't know what an Employee structure looks like. You should put the definition of your Employee structure into a header file and then use &quot;#include&quot; to tie the header file into both of your .c (or .cpp) files. So, do something like this:

In a new file named the same as your program, but with the extension of .h, (i.e. &quot;myprogram.h&quot;), put the Employee structure definition:

Code:
struct employee
{
  char name[20];
  int empnumber;
};

Then, add this line at the very top of both of your two current .c (or .cpp) files, but be sure to use the actual name of YOUR header file:

Code:
#include &quot;myprogram.h&quot;


Hopefully that will solve your problem. Now, regarding the problem with the index disappearing from your source code:

> the c->name and c->empnumber above has an index of i.
> for some reason, it didn't show up in the post.

The next time you are creating a Message that you want to post, click on the &quot;Process TGML&quot; link (but leave the checkbox checked) located just below the edit window. A window will pop up giving you some helpful tips. One tip tells you how to enclose your source code with HTML style tags to prevent parts of it from being translated into italics and other special formating. The tags you need to use are:

[
Code:
code]

/* your source code goes here */

[
/code]

Hope that helps.
 
I did what you said and made a header file with my structure in it and include it in my files but still the same error. the error &quot;error C2227: left of '->name' must
point to class/struct/union.&quot; still exists.
However, if i use the dot &quot;.&quot; operator instead of the pointer &quot;->&quot; operator, it works fine. any idea on why case file2 receives a pointer form file1.
 
Hi,

> any idea on why case file2 receives a pointer form file1.

It sounds like you must have changed some other things since posting your original program. So, I really can't comment, other than to mention that the definition of the file2 function above answers your question (employee is a pointer parameter):

file2(employee *c)

I'm glad you got it to work. I just want to mention that your code was very hard to read, because there is a C keyword called, FILE, that is used to access files on your hard disk:

FILE *fileptr;
fileptr = fopen(&quot;file.txt&quot;, &quot;r&quot;);

So, it's good not to use that word in your variables unless you are actually using the variable to access files on your hard drive.
 
oops. my bad. i haven't worked with C before so i didn't know that File was a reserved word.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top