how to read this file

I've been working on this all day and am running close to a deadline, would be fantastic if anyone could help.

I have a file formatted as follows:

character 8bit_hexbase_number 8bit_hexbase_number

so an example file would look like:

1 0x00000000
1 0x00000004
0 0x00000800 0x00000000
1 0x00000008
1 0x0000000C
1 0x00000010
0 0x00000804 0x0000000A
0 0x00000808 0xFFFFFFFF
0 0x0000080C 0x5A5A5A5A
1 0x00000014

I don't mind handling them all as strings as they are only used to represent things not actually operate on.

I do need to separate each string though when I'm reading it into my program and I don't know how to as the length of each number may vary.

Can anyone think of a way to read each number/string in and store it so that for example line1 number1=1, number 2=0x00000000 and number3=0?

Is there a function which will read in strings seperated by white space characters as separate strings?
many thanks
Something along these lines would do the job. It's clumsy, but hopefully easy to understand. I don't promise it's exactly correct, but this sort of thing would do it. It ends up with lineStore, a vector of lines structures, each one of which is the three values. I've assumed that it's a text file that literally reads as above.

This method does not rely on every short line starting with 1 (but does rely on every short line beginning with a single digit or character). If you can promise that every short line begins with 1, then it can be made much simpler.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
struct lines
{
  string character;
  string eightbit_hexbase_number1;
  string eightbit_hexbase_number2;
}

ifstream infile("filename");

vector<lines> lineStore;
line tempHoldingLine;
int skip = 0;

while(notYetReachedTheEndOfTheFile) // Work this out yourself :)
{
  if (!skip)
  {  infile >> tempHoldingLine.character;}

  infile >> tempHoldingLine.eightbit_hexbase_number1;
  infile >> tempHoldingLine.eightbit_hexbase_number2;

  // less than elegant way of dealing with short line
  if (tempHoldingLine.eightbit_hexbase_number2.length() == 1)
  {
     string theNextCharacter = tempHoldingLine.eightbit_hexbase_number2;
     tempHoldingLine.eightbit_hexbase_number2 = 0;
     skip = 1;
     lineStore.push_back(tempHoldingLine);
     tempHoldingLine.character = theNextCharacter;
  }
  else
  {
     // It was a full line of three
     lineStore.push_back(tempHoldingLine);
     skip = 0;
   }
}


Last edited on
Hi Solo2wolf,

You could use the ifstream class, and use that to read your lines.

1
2
3
4
5
6
7
8
9
10
11
ifstream file = new ifstream("file_name.txt");
 //try/catch block should be here, blah blah

while(!file.eof())
{
    char * str = file.getline();
    //TODO: split the line into multiple strings, delimited by space
       //do whatever it is you're supposed to do with them
}

file.close();


To split the string, you could use strtok(str, " "), or the boost library's "split" function.


Can anyone think of a way to read each number/string in and store it so that for example line1 number1=1, number 2=0x00000000 and number3=0?


I'm not sure what you mean here.
Last edited on
ifstream file = new ifstream("file_name.txt");
new? Surely not :)

char * str = file.getline();
There is no getline in C++ that returns a char pointer, or that takes no parameters.
Last edited on
Topic archived. No new replies allowed.