getting file's text with ',' delimit

hi, i have a file which is like this
16A9B,15A,4,6,6,8,156A,19B...
it may be as long as 200mb in size file , and all of them are in hex values

what i need to do is get the value before the first comma (16A9B), then ill do something with it, then i need to get the next value(15A) and PLUS it to the first value (16A9B+15A=0x16BF5) and then ill do something with it(16BF5) and then continue to the next one ( 16BF5 + 4 = ....... until the end

how can i do this? any help is appreciated
You need to use the hex input manipulator. You will also want to be careful with those commas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iomanip>
#include <iostream>

unsigned long get_csv_hex_value( std::istream& ins )
  {
  unsigned long result = 0;
  ins >> std::ws;  // skip any leading whitespace
  if (ins.peek() == ',')  // is there a leaing comma?
    {
    ins.get();  // yes, skip it...
    ins >> std::ws; // ...and any more leading whitespace
    }
  ins >> std::hex >> result;
  return result;
  }

Now each time you want the next value, just use the function.

The function is very general. You can make it more specific if you like. For example, if you know that there is no whitespace in your file, you'll save some time not checking for it.

Likewise, you'll save some time by putting the stream into hex mode once at the beginning, instead of each time you read a value.

Hope this helps.

[edit] Oh yeah, don't forget to check the stream's good() state so you know when to stop reading values...
Last edited on
lemme get this right , i removed the whitespaces cuz theres no space in the file
1
2
3
4
5
6
7
8
9
10
unsigned long get_csv_hex_value( std::istream& ins )
{
  unsigned long result = 0;
  if (ins.peek() == ',')  // is there a leading comma?
    {
    ins.get();  // yes, skip it...
    }
  ins >> std::hex >> result;
  return result;
}


and ill call it by
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
istream hFile;
hFile.open("Values.tmp");

unsigned long Value = 0;
unsigned long nextValue = 0;

...do loop
if ( hFile.good() )
{
  Value = get_csv_hex_value(hFile); // get first value
  //do some stuff with Value
}
else {
  //end of file reached, do nothing
}
while ( hFile.good() )
{
  nextValue = get_csv_hex_value(hFile);
  Value = Value + nextValue;
  // do some stuff
}

im very new to these streams, so it works by finding the next "," using peek then uses get() to get the value before the comma, right?

and does this mean that the next time u call this function, it will start from after the "," .
just want to know whats going on , im gonna go test this now

thanks alot!
Last edited on
Topic archived. No new replies allowed.