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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading/storing a table file

Status
Not open for further replies.

TheButcher

Programmer
Jul 6, 2001
11
US
I have to read in this file. It consists of a name, size, type, value, and address. I want to read in this file and store the different values into a structure so i can search it later in the program.

ex)
MAX 2 bytes hex 10 0000


What I am doing is reading in a line and tokenizing it by white space. I use a bunch of if else statements to determine if it is a number, type or name. Unfortunately this is not working b/c the number can be the size, value, or address. Is there any other way to read in this line. I was thinking maybe a scan b/c I know what order I will be reading in, but not all sections have to be there. Ex) They might not have an address. Ex)type could be a label, so it has no value, etc.

Thanks in advance for you help!
 
how about
#include<fstream>
#include<iostream>
using namespace std;
int maine()
{
char aa[100];
int as;
ifstream iff(&quot;xx.txt&quot;);
iff>>as;
iff>>aa;
cout<<aa<<endl;
return 0;
} John Fill
1c.bmp


ivfmd@mail.md
 
Hi
Can something like this work?

fgets(line, MAXLINE, in);

if(sscanf(line, &quot;%s %i bytes %s %d %d&quot;, name, &size, type, &value, &addr) = 5)
{
/* read successfully */
}
else if(sscanf(line,&quot;%s %i bytes %s %d&quot;, name, &size, type, &value, &addr) = 4)
{
/* read successfully without address */
}

... and so on such that all possibilities are included.

Please do inform what you did.

Bye.
Ankan

Could you please help me... Thread205-112554.
Please do correct me if I am wrong. :)
 
Ankan,

Well I only really needed to search by the 1st word. So I read in all the lines of the file (the entire line) into an array of structures with a single entry. Then if I came across a variable in the code section I used strncmp() to find the word in the structure. Then when I found that word, I stored the line into a temp[] and tokenized it then.

Unfortunately, I am new at C and have no clue what JohnFill did in his program. I never even seen half of that stuff before. Your code seems to make sense, wish I thought of that before wasting all that memory!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top