CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    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++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 30th, 2004, 05:21 PM
    walkinginwater walkinginwater is offline
    Member
     
    Join Date: Nov 2004
    Posts: 89
    walkinginwater is on a distinguished road (10+)
    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;
    }


    }
    Reply With Quote
      #2    
    Old November 30th, 2004, 09:04 PM
    HighCommander4 HighCommander4 is offline
    Senior Member
     
    Join Date: Apr 2004
    Location: Canada
    Posts: 1,268
    HighCommander4  is a jewel in the rough (300+)HighCommander4  is a jewel in the rough (300+)HighCommander4  is a jewel in the rough (300+)HighCommander4  is a jewel in the rough (300+)
    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.
    Reply With Quote
      #3    
    Old December 1st, 2004, 07:39 AM
    walkinginwater walkinginwater is offline
    Member
     
    Join Date: Nov 2004
    Posts: 89
    walkinginwater is on a distinguished road (10+)
    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_
    Reply With Quote
      #4    
    Old December 1st, 2004, 08:26 AM
    Philip Nicoletti Philip Nicoletti is offline
    Elite Member
    Power Poster
     
    Join Date: Aug 2000
    Location: West Virginia
    Posts: 6,717
    Philip Nicoletti has a brilliant future (2000+)Philip Nicoletti has a brilliant future (2000+)Philip Nicoletti has a brilliant future (2000+)Philip Nicoletti has a brilliant future (2000+)Philip Nicoletti has a brilliant future (2000+)Philip Nicoletti has a brilliant future (2000+)Philip Nicoletti has a brilliant future (2000+)Philip Nicoletti has a brilliant future (2000+)Philip Nicoletti has a brilliant future (2000+)Philip Nicoletti has a brilliant future (2000+)Philip Nicoletti has a brilliant future (2000+)
    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;
    }
    Reply With Quote
      #5    
    Old December 1st, 2004, 09:07 AM
    walkinginwater walkinginwater is offline
    Member
     
    Join Date: Nov 2004
    Posts: 89
    walkinginwater is on a distinguished road (10+)
    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);
    }
    }
    Reply With Quote
      #6    
    Old December 1st, 2004, 04:46 PM
    HighCommander4 HighCommander4 is offline
    Senior Member
     
    Join Date: Apr 2004
    Location: Canada
    Posts: 1,268
    HighCommander4  is a jewel in the rough (300+)HighCommander4  is a jewel in the rough (300+)HighCommander4  is a jewel in the rough (300+)HighCommander4  is a jewel in the rough (300+)
    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.
    Reply With Quote
      #7    
    Old December 1st, 2004, 04:52 PM
    Paul McKenzie Paul McKenzie is offline
    Elite Member
    Power Poster
     
    Join Date: Apr 1999
    Posts: 20,400
    Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)Paul McKenzie has a reputation beyond repute (3000+)
    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);
        }
    }
    Regards,

    Paul McKenzie
    Reply With Quote
      #8    
    Old December 1st, 2004, 10:31 PM
    cma cma is offline
    Member +
     
    Join Date: Feb 2004
    Location: USA - Florida
    Posts: 729
    cma will become famous soon enough (80+)
    Re: Some strange problem about operator >>

    The one where you have
    Code:
    while (!input.eof())
    is incorrectly detecting EOF. This is a relatively common mistake for beginners (and for the experienced sometimes), you can't detect end of input this way, you have to first check if the stream entered a failed state first and then check for end of input (prefererably after every input):
    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; 
            }    
        }
    How you have it, it goes into an infinite loop because if you read in something that makes the stream go into a failed state (like trying to read strings into int vars. or the file doesn't exist), end of input is never reached, so you then have an infinite loop, the stream is in a failed state but it's not because of EOF.

    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; 
        }
    This will continue reading data until the stream enter's a fail state, either because of EOF or because of bad input.
    __________________
    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
    Reply With Quote
      #9    
    Old December 2nd, 2004, 06:33 AM
    walkinginwater walkinginwater is offline
    Member
     
    Join Date: Nov 2004
    Posts: 89
    walkinginwater is on a distinguished road (10+)
    Smile Thanks Paul McKenzie &&cma&&HighCommander4

    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!
    Reply With Quote
      #10    
    Old December 2nd, 2004, 07:14 AM
    walkinginwater walkinginwater is offline
    Member
     
    Join Date: Nov 2004
    Posts: 89
    walkinginwater is on a distinguished road (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!
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 04:22 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009