Read numbers from text file
hello, i'm trying to read a text files into string and to output that strings
this's my text file
0b10100 0d478 0x7 0xff 11110000
and this is my 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
|
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
ifstream is("Numeri.txt");
int count=0;
char *str;
int position=0;
int tmp_pos = 0;
while(!is.eof()){
while(is.peek() !=' '){
tmp_pos++;
is.seekg(tmp_pos);
}
is.seekg(position);
count = tmp_pos-position+1; //+1 for '\0'
str = new char[count+1];
is.getline(str,count);
str[count] = '\0';
cout << str << endl;
position = ++tmp_pos;
is.seekg(position);
delete [] str;
}
return 0;
}
|
I can print the first number, but then the program enter in loop?
what's wrong?
thanks
line 8 should read 'ifstream infile (" inputfile ").......i think assuming no path is needed.
while(is.peek() !=' ')
peek will always return the same character if you don't remove it, try with is.get()
Topic archived. No new replies allowed.