Please help with File IO

B

Brian

Hello,

I have a text file I'm attempting to parse. There are about 50 fixed width
fields in each line / row. For example (shortened for brevity):

W1234Somebody East 101110001111010101
E1235Someone Else West 010111001001010101

I'm having problems pulling these fields into structures, in order to be
able to access each individually. I am currently opening as a sequential
file. Is there a better way?

My structure looks something like:

struct data{
char area[1];
char empNumber[4];
char name[16]
char region[5];
char options[20];
}

int index = 0;
data user[100];

I would like to read the entire file into memory. Please tell me if I'm
going about this the wrong way. So far after reading the file in, I'm
unable to access any individual items (ie. user[index].area) Also should
I stick with a sequential file, or should I consider binary access (seems
like it may be easier to address individual elements)?



(I don't want to include too many details here, but will be happy to provide
whatever is needed).

Thank you in advance,
Brian
 
L

Larry Brasfield

Brian said:
Hello,

I have a text file I'm attempting to parse. There are about 50 fixed width
fields in each line / row. For example (shortened for brevity):

W1234Somebody East 101110001111010101
E1235Someone Else West 010111001001010101

I'm having problems pulling these fields into structures, in order to be
able to access each individually. I am currently opening as a sequential
file. Is there a better way?

My structure looks something like:

struct data{
char area[1];
char empNumber[4];
char name[16]
char region[5];
char options[20];
}

int index = 0;
data user[100];

I would like to read the entire file into memory. Please tell me if I'm
going about this the wrong way.

The information you provide does not permit such a conclusion.
Diagnosing "The wrong way" would have to be based on your
requirements, unstated so far. Your approach is going to present
difficulties if the format changes, but I presume you know that.
So far after reading the file in, I'm
unable to access any individual items (ie. user[index].area)

I cannot translate "I'm unable to access" into a familiar fact.
Do you mean "I can access individual fields but their values
differ from what I expected."? Or is it something else? If
the former: What did you get? What did you expect? And
why did you expect it? If the latter, please elaborate.
Also should
I stick with a sequential file, or should I consider binary access (seems
like it may be easier to address individual elements)?

I have not heard of "sequential file" in connection with the
standard C or C++ libraries. If you are distinguishing that
from files opened in binary mode, then, from the fact that
you claim to be dealing with a "text file", it is clear that you
do not want binary mode. The "not binary" mode is often
known as "text mode". Files are considered a sequential
series of bytes whether binary or not.
(I don't want to include too many details here, but will be happy to provide
whatever is needed).

It would probably help to see the call you use to read in
the file, together with enough data to know what values
the call parameters have.

You have given few clues as to how records are separated
in your input file. Are line-ends used for that? If so, your
attempt to blast in a whole series of records fails to account
for the space taken by the record separators. If not, your
example is misleading.
Thank you in advance,
Good luck.
 
B

Brian

Hi Larry,

I just posted my code and a sample of the data as a follow-up to my
other post: "Best way to access this File?". I do apologize for the
post - I realize I didn't do a good job in posing the question.

Thanks!
Brian

Larry Brasfield said:
Brian said:
Hello,

I have a text file I'm attempting to parse. There are about 50 fixed width
fields in each line / row. For example (shortened for brevity):

W1234Somebody East 101110001111010101
E1235Someone Else West 010111001001010101

I'm having problems pulling these fields into structures, in order to be
able to access each individually. I am currently opening as a sequential
file. Is there a better way?

My structure looks something like:

struct data{
char area[1];
char empNumber[4];
char name[16]
char region[5];
char options[20];
}

int index = 0;
data user[100];

I would like to read the entire file into memory. Please tell me if I'm
going about this the wrong way.

The information you provide does not permit such a conclusion.
Diagnosing "The wrong way" would have to be based on your
requirements, unstated so far. Your approach is going to present
difficulties if the format changes, but I presume you know that.
So far after reading the file in, I'm
unable to access any individual items (ie. user[index].area)

I cannot translate "I'm unable to access" into a familiar fact.
Do you mean "I can access individual fields but their values
differ from what I expected."? Or is it something else? If
the former: What did you get? What did you expect? And
why did you expect it? If the latter, please elaborate.
Also should
I stick with a sequential file, or should I consider binary access (seems
like it may be easier to address individual elements)?

I have not heard of "sequential file" in connection with the
standard C or C++ libraries. If you are distinguishing that
from files opened in binary mode, then, from the fact that
you claim to be dealing with a "text file", it is clear that you
do not want binary mode. The "not binary" mode is often
known as "text mode". Files are considered a sequential
series of bytes whether binary or not.
(I don't want to include too many details here, but will be happy to provide
whatever is needed).

It would probably help to see the call you use to read in
the file, together with enough data to know what values
the call parameters have.

You have given few clues as to how records are separated
in your input file. Are line-ends used for that? If so, your
attempt to blast in a whole series of records fails to account
for the space taken by the record separators. If not, your
example is misleading.
Thank you in advance,
Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top