how to read and process text file ..

Jan 1, 2011 at 5:07pm
hello guys ....
i'm having a problem here . I have this kind of data ...

2 3 4 1 -1
1 2 3 -1
3 4 5 6 -1

what i want to do is to read this data , line by line .
Everytime the loop found -1 , it will be go to next process which is I want to arrange the number before -1 .

I have code to read file but I do not know how to give a command to the code , to stop the read line process and go to the arrangement step ...

here is my code .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

void main()
{
	ifstream cop;
	cop.open("try.txt");

	string data;

	while(!cop.eof())
	{
	getline(cop,data);

	cout << data << endl;
	}
}



I hope anyone can help me .. thank you very much!!
Last edited on Jan 1, 2011 at 5:08pm
Jan 1, 2011 at 6:18pm
Everytime the loop found -1 , it will be go to next process which is I want to arrange the number before -1.

Can you explain this line a bit better? What do you mean by arrange? Are all the data you plan on reading going to be integers?

If so you may not need strings (unless that's part of the arrange step?) but instead read your data into a vector or array and when you catch a -1 run your arrange.

1
2
3
4
5
6
7
8
9
10
11
12
13
int num;
while (cop >> num) // (cop >> num) will ignore white spaces and
                   // will stop when it hits eof() or bad input.
{
   if (num != -1) 
   {
      // code to place into array or convert to char and add to string
   }
   else
   {
      // Run arrange code
   }
}

Topic archived. No new replies allowed.