Help picking numbers out of an array/string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 2
Reputation: Trucks is an unknown quantity at this point 
Solved Threads: 0
Trucks Trucks is offline Offline
Newbie Poster

Help picking numbers out of an array/string

 
0
  #1
Nov 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Help picking numbers out of an array/string

 
0
  #2
Nov 15th, 2007
Yes, getline is correct. and use string . It has find and substring functions that will help split the string into values.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2
Reputation: Trucks is an unknown quantity at this point 
Solved Threads: 0
Trucks Trucks is offline Offline
Newbie Poster

Re: Help picking numbers out of an array/string

 
0
  #3
Nov 15th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Help picking numbers out of an array/string

 
0
  #4
Nov 15th, 2007
Use the string as an array. Let's say you just read the string and you're looking for the first 'w':
  1. searchchr = 'w';
  2. i = 0;
  3. while (line[i] != searchchr)
  4. {
  5. i++;
  6. }
Now you can move everything up to the 'w' into another variable with .substr()
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Help picking numbers out of an array/string

 
0
  #5
Nov 15th, 2007
>>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.
Last edited by Lerner; Nov 15th, 2007 at 7:07 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC