dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Weird IO Issue

  1. #1
    Join Date
    Jun 2008
    Posts
    44

    Weird IO Issue

    Hi all,

    I feel pretty retarded that I need to post this, but I haven't used C++ in a long time. For some reason the following code will not read anythign from the specified file. I made sure, the file is opened.

    #include "stdafx.h"
    #include <fstream>
    #include <string>



    using namespace std;

    int _tmain(int argc, _TCHAR* argv[])
    {
    string strInput;
    ifstream inf("su08_world1.dat");

    while (!(inf.eof()))
    {

    inf >> strInput;
    cout << strInput << endl;
    }
    system("PAUSE");


    return 0;
    }

    Any help will be appreciated.
    Marc

  2. #2
    Join Date
    May 2008
    Posts
    96

    Re: Weird IO Issue

    Are you sure the file is opened? (Because you didn't check in the code you posted.)

    Try
    Code:
      string strInput;
      ifstream inf( "su08_world1.dat" );
    
      if (!inf)
        cout << "fooey, the file didn't open!";
      else
        while (inf >> strInput)
          cout << strInput << endl;
    Hope this helps.

  3. #3
    Join Date
    Jun 2008
    Posts
    44

    Re: Weird IO Issue

    In my original code (which has the same problem), I did check the code. It is definetely open.

    Thanks
    Marc

  4. #4
    Join Date
    Jun 2008
    Posts
    12

    Re: Weird IO Issue

    I commented out the first line and changed the main declaration so that I could compile it without stdafx.

    If you happen to be using dev-cpp, you need to also include iostream for that line cout << strInput << endl;

    When I added in an iostream inclusion and made myself a su08_world1.dat file, it worked fine.

    That leads me to a question, does the program crash or does it just skip to the end with system("PAUSE")?
    You know you're a C++ Programmer when the first thought to come to mind when seeing 'std' is the namespace std, and the second thought is namespaces in general, the third thought being how to improve that program you're working on, and finally the fourth thought being sexually transmitted diseases.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Weird IO Issue

    Quote Originally Posted by std
    If you happen to be using dev-cpp, you need to also include iostream for that line cout << strInput << endl;
    It doesn't matter what compiler you use, you must include <iostream> to guarantee that code will compile that uses std::cout.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jun 2008
    Posts
    12

    Re: Weird IO Issue

    Ahh I had the hierarchy mixed up.
    You know you're a C++ Programmer when the first thought to come to mind when seeing 'std' is the namespace std, and the second thought is namespaces in general, the third thought being how to improve that program you're working on, and finally the fourth thought being sexually transmitted diseases.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)