reading a line of unsigned integers with spaces

I want to read a line of unsigned integers from a file seperated by spaces and put the values into a vector
You should give that a go then. Post your code when you get stuck with something in particular.

I want an Alfa Romeo MiTo QV but it ain't going to happen just like that.
So the code reads but if the numbers become bigger it fails like in the third number:
2252 3853 10688

while (iTabPos < iwend)
{


fread(piv,1, 1*sizeof( char), filehandle);
printf("read characters of the file:\n%s\n", piv);
hr.ssignal[iTabPos]=iv;

*istring = getc(handle1);
if (*istring == ' ')
{
iTabPos++;

}
else if (*istring == 10) {break;}


}
*istring = getc(handle1);
Last edited on
I have written a small function for myself recently that seperates a string into smaller ones. However, I think that it could be muuuuuuuch easier with the correct method (that i don' know :/). Hope it'll help. You have to modify my code ofc, and there is only 1 seperation char allowed. Needs to be improved. Heres the code:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream> 
#include <string>   
#include <vector>  
#include <cstdlib>

using namespace std;

int main()
{
    string s;            // String s
    string zs;           // Zwischenspeicher
    char tz = ' ';     // Trennzeichen (seperation char)
    vector<string> c(10); // "cut" --> Stringsegmente im Vektor

cout << "\nTrennzeichen: Leerzeichen " << endl <<endl;

s = "1-22-333-4444-55555";

cout << s << endl;

int i = 0;

while (s.find(tz)<s.length()+1){ 
/*find gab bei Tests (strtest3) immer 4294967295 zurück, wenn das gesuchte Zeichen nicht enthalten war*/

zs=s;
cout << "\nzs vorher  " << zs << endl;
cout << "\nzs.find - : " << zs.find(tz) <<endl;
cout << "\nzs.length: " << zs.length() <<endl;
cout << "\nzs.length - zs.find: " << zs.length()-zs.find(tz) <<endl;
zs=zs.erase(zs.find(tz),zs.length()-zs.find(tz));
c[i]=zs;

s.erase(0,s.find(tz)+1);

cout << "\ns nachher " << s << endl;

cout << "\n\nc["<<i<<"]: "<<c[i]<<"\n\n";

i=i+1;
}

c[i]=s;

for (int x=0;x<=i;x++){
cout << "\n"<<x+1<<". Zahl: " << c[x] << "\n";}




}


I'm sorry,that the annotaions are german, but I hope u can find your way through !

Greetings, Lukas

EDIT: You have to convert the string to int if you want to use this solution (there are very easy methods to do that (e.g. atoi etc)
Last edited on
Danke Lukas, ich verstehe embischen Deutsch,
but in my case i read online from a file so am no sure the condition in the while loop will work s.find?
sorry am a beginner
:)
You only need to get the string from the internet and save it into a variable, so the string gets processed in your program. (Does your file contain any wordwraps, or is it a single line of hundreds of unsigned integers ?)

I program for some years now, but I would still consider myself as a beginner due to my low level in programming aswell...
The s.find in the while condition gives some huge integer, if there is no specified char left in the string, that's how it works. But this seems to be very unelegant, so I hope some pr0 will look at this topic and will comment my code.

BTW, "Zubehör" is a german word ;)

Lukas
sorry i meant online from a sensor and the file is massive so i cannot really load it at once perhaps i divided into blocks
Ok, please tell me, what a sensor is?
I have no idea about how to divide a massive file into smaller pieces without downloading them.

BTW, i found the function u need:

http://www.cplusplus.com/reference/cstring/strtok/

i think this might be *much* (!) better than my "solution"...

I hope, it helps

Lukas
The following example reads from a stringstream (can be replaced with a file stream). The default terminator for getline is CR/LF, the second one I use a space to break up each line. So this deals with multiple lines. My sample data has a '\n' between the 3rd and 4th numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	vector<string> v;

	stringstream ssData( "2252 3853 10688\n111 222 333" );

	string strLine;

	while( getline( ssData, strLine ) )
	{
		stringstream ssLine( strLine );
		string strNum;

		while( getline( ssLine, strNum, _T(' ') ) )
			v.push_back( strNum );
	}

	for( size_t i=0; i < v.size(); i++ )
		cout << v[i] << endl;


2252
3853
10688
111
222
333
Not the same, but similar in one-two thing:
http://www.cplusplus.com/forum/beginner/95136/
Topic archived. No new replies allowed.