| | |
Help picking numbers out of an array/string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Nov 2007
Posts: 2
Reputation:
Solved Threads: 0
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
// 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
Use the string as an array. Let's say you just read the string and you're looking for the first 'w':
Now you can move everything up to the 'w' into another variable with .substr()
c Syntax (Toggle Plain Text)
searchchr = 'w'; i = 0; while (line[i] != searchchr) { i++; }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jul 2005
Posts: 1,678
Reputation:
Solved Threads: 264
>>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.
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.
![]() |
Similar Threads
- Using Returned Values and other questions... (C)
- Conversion of Long Array to String (Java)
- Problems from string to int array (C)
- how do u find prime numbers in an array (C)
- Array/String intersect and Array/Array intersect code (C#)
- generating random numbers into an array.. (Java)
- Conver int Array into a String (Java)
- Array to String (PHP)
Other Threads in the C++ Forum
- Previous Thread: Shuffle with Random Generator
- Next Thread: Nugget? reading different int from a file
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






