| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Some strange problem about operator >>
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; } } |
|
#2
|
|||
|
|||
|
Re: Some strange problem about operator >>
You could use an std::istream_iterator to read the data into an std::vector<int>. That is proven, tested code, and is much less likely to cause errors. Then you can do your operations using the data in the vector. Tell me if you need help with doing this.
|
|
#3
|
|||
|
|||
|
Re: Some strange problem about operator >>
Hi, HighCommander:
I am sorry I reply so later. i am now in germany, and i post it yesterday night,so untill now i see your post i only have two problem about your advice: the first one are: my data file is very large, there are 100,000 lines in my file , if you read them all into the std::vector<int>, that will cause memory problem or not ? If it not happen , please tell me how to handle it, Thank you first! the second is that , you don't answer why the problem is coming, ? can you give me some hints_ |
|
#4
|
|||
|
|||
|
Re: Some strange problem about operator >>
Verify that the input file exixts (if on Linux ... filename are case sensitive).
(is the extansion .dat or .data ?) Code:
ifstream input("test.data");
// verify file opened ...
if (!input)
{
cout << "could not open file\n";
return 0;
}
|
|
#5
|
|||
|
|||
|
Re: Philip Nicoletti I am sure it opens
the following is my test function in the programme.
assure(input,"test.data"); void assure(std::ifstream& in, const std::string& filename = "") { using namespace std; if(!in) { fprintf(stderr, "Could not open file %s\n", filename.c_str()); exit(1); } } |
|
#6
|
|||
|
|||
|
Re: Some strange problem about operator >>
I don't see anything wrong with your code. Maybe something is wrong with the file, such as it does not contain the right data or something. If you send me your entire code (including the file, or at least the first part of the file), I can test it and I might be able to figure out what is happening.
As for your concern about memory, std::vector uses memory allocated from the heap so it should have no problem storing 100,000 integers. |
|
#7
|
|||
|
|||
|
Re: Some strange problem about operator >>
Also, if you know you will be storing a lot of items in the vector, you should call std::vector::reserve() to allocate enough memory so that adding items to the vector will not cause too much of a bottleneck:
Code:
#include <vector>
int main()
{
// Slow
{
std::vector<int> V;
for (int i = 1; i <= 100000; ++i )
V.push_back(i);
}
// Faster
{
std::vector<int> V;
V.reserve(100000);
for (int i = 1; i <= 100000; ++i )
V.push_back(i);
}
}
Paul McKenzie |
|
#8
|
|||
|
|||
|
Re: Some strange problem about operator >>
The one where you have
Code:
while (!input.eof()) Code:
bool done_reading_file = false;
while (not done_reading_file)
{ input >> x >> y >> e1 >> e2 >> ts;
if (input.fail())
{ // input stream is in a failed state, no need to continue reading
done_reading_file = true;
if (not input.eof())
cerr << "Error reading file, got some bad input" << endl;
}
else
{ double xc,yc;
xc = ((double)x/e2);
yc = ((double)y/e2);
output << xc << " " << yc << endl;
}
}
The alternative to introducing a boolean variable to terminate the loop is to just to read the data in the loop declaration: Code:
while (input >> x >> y >> e1 >> e2 >> ts)
{
double xc,yc;
xc = ((double)x/e2);
yc = ((double)y/e2);
output<<xc<<" "<<yc<<endl;
}
__________________
Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html Last edited by cma; December 1st, 2004 at 10:39 PM. Reason: added alternative to having extra bool value |
|
#9
|
|||
|
|||
|
thanks for your help!
the problem which predict by cma does happen !!! But there are precondition The precondition is that the number of the data stored in the "test.data" is not exactly five per line, it will go to infinitly loop! i have already past the full source code and "test.data" into an email box "studing_data_cpuls_transfer@yahoo.com " password "walkinginwater" if you have intersting, you can go to check! |
|
#10
|
|||
|
|||
|
i got the solution
I am sorry to say that : i made a very silly mistake
HighCommander4 is right, i made somemistakes about the data file, the datas in the files are not all integers, the five one is a double one, when "temporal" is declared type "double", everything works now! Thank you everybody! |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|