954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help picking numbers out of an array/string

Hi everyone, this is my first post and im just plain confused on how to get what i need here. in the past ive only had single lines of data and just used an input >> char;

but now i have this line of code in a txt file, with like 300 rows...

0x3B timestamp=446218 00 00 00 DC 1C 00 00 00


and i need to read in line by line and then save the 5th and 6th rows as one number, such as in this one, i would need to save 00DC as one number to convert it from hexadecimal to decimal, which i have figured out.

I am thinking about using getline function, but how do i pull out the values i need and store them as a number?

also which is better to store the line as a char array, or as a string??

any help is greatly appreciated.

Trucks
Newbie Poster
2 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Yes, getline is correct. and use string . It hasfind and substring functions that will help split the string into values.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Ok, so i got that part to work, here is the basic part of my code and it works really well....


// Going through each line of data in the file
getline(input_file, line);
// Picking out single characters in the data lines
data = line.substr(36,1);
data1 = line.substr(41,2);

cout << data << data1 << endl;

but now the problem is that, the other several data files that the program must work for, have a different number of characters in them, so is there a way to make it read the values based on whitespace or something?

thanks for the help

Trucks
Newbie Poster
2 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Use the string as an array. Let's say you just read the string and you're looking for the first 'w':

searchchr = 'w';
i = 0;
while (line[i] != searchchr)
{
    i++;
}

Now you can move everything up to the 'w' into another variable with.substr()

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

>>is there a way to make it read the values based on whitespace or something?


There are all sorts of search/reading/parsing routines that can be used based on each individual search criteria. Let's say that each line has a minimimum of 6 substrings all separated from one another by spaces. Each substring will have one or more characters, the only restriction to which is the char in the 5th and 6th substring have to represent digits in HEX. You always want the 5th and 6th substring to work with. To extract them you could call >> 4 times and ignore the input. Then call >> twice more to save desired values in desired variables. Then call getline() with a newline delimiter to clear the rest of the line and go to the next.

If you prefer to read in the entire line from the file and parse it from an istringstream object after it's been read in instead of parsing directly from the file using the same routine, so be it.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You