walkinginwater
November 30th, 2004, 05:21 PM
I want to write an programme to read the datas from a file;
The data is stored as the following form
273 834 454 728 4774
138 520 303 479 44875
309 322 186 372 64181
..... .... ....
I only need the 1st,2nd, 4th number in one row, each line I will produce two new numbers
My first problem is that the wrong version of my program can compile, can build, but it cannot do anything, the Output.txt is zero size, that means the program is terminate at while(input>>temporal), but I am sure even in the wrong version the “test.data” is opened
My second problem is that in the wrong version, when I substitute statement “while(input>>temporal)”, by while(!input.eof()) and the data acquire statement “input>>temporal” inside the loop, the program can compile, can build, but when I run it, it is in a horrible situation, it cannot stop the loop, it cannot end,
//wrong version
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main(int argc, char* argv[])
{
ifstream input("test.data");
ofstream output("Output.txt");
const int HISTO2DSZ=256;
int temporal,x,y,e1,e2;
int Num_input=0;//this take counts of the input number
while(input>>temporal)//run-time error, the condition “input>>temporal” is not satisfied at the beginning
{
Num_input++;
if ((Num_input%5)==1)
x=temporal;
if((Num_input%5)==2)
y=temporal;
if((Num_input%5)==3)
e1=temporal;
if((Num_input%5)==4)
{
e2=temporal;
double xc,yc;
xc = ((double)x/e2);
yc = ((double)y/e2);
output<<xc<<" "<<yc<<endl;
}
}
}
//correct one
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main(int argc, char* argv[])
{
ofstream output("Output.txt");
ifstream input("test.data");
int x,y,e1,e2;
double ts;
while (!input.eof())
{
input >> x >> y >> e1 >> e2 >> ts;
double xc,yc;
xc = ((double)x/e2);
yc = ((double)y/e2);
output<<xc<<" "<<yc<<endl;
}
}
The data is stored as the following form
273 834 454 728 4774
138 520 303 479 44875
309 322 186 372 64181
..... .... ....
I only need the 1st,2nd, 4th number in one row, each line I will produce two new numbers
My first problem is that the wrong version of my program can compile, can build, but it cannot do anything, the Output.txt is zero size, that means the program is terminate at while(input>>temporal), but I am sure even in the wrong version the “test.data” is opened
My second problem is that in the wrong version, when I substitute statement “while(input>>temporal)”, by while(!input.eof()) and the data acquire statement “input>>temporal” inside the loop, the program can compile, can build, but when I run it, it is in a horrible situation, it cannot stop the loop, it cannot end,
//wrong version
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main(int argc, char* argv[])
{
ifstream input("test.data");
ofstream output("Output.txt");
const int HISTO2DSZ=256;
int temporal,x,y,e1,e2;
int Num_input=0;//this take counts of the input number
while(input>>temporal)//run-time error, the condition “input>>temporal” is not satisfied at the beginning
{
Num_input++;
if ((Num_input%5)==1)
x=temporal;
if((Num_input%5)==2)
y=temporal;
if((Num_input%5)==3)
e1=temporal;
if((Num_input%5)==4)
{
e2=temporal;
double xc,yc;
xc = ((double)x/e2);
yc = ((double)y/e2);
output<<xc<<" "<<yc<<endl;
}
}
}
//correct one
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main(int argc, char* argv[])
{
ofstream output("Output.txt");
ifstream input("test.data");
int x,y,e1,e2;
double ts;
while (!input.eof())
{
input >> x >> y >> e1 >> e2 >> ts;
double xc,yc;
xc = ((double)x/e2);
yc = ((double)y/e2);
output<<xc<<" "<<yc<<endl;
}
}