ifstream help

This is a discussion on ifstream help within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I'm trying to learn c++, but i think i may need a little help at this point. I ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    4

    Cool ifstream help

    Hey guys,

    I'm trying to learn c++, but i think i may need a little help at this point.
    I want to read some data from a file "test.dat", but for some reason it won't work.
    I don't see where i mess up, maybe you can help me.

    thanks


    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main() {
    
    // Open the file 
     ifstream infile("test.dat", ios::in | ios::nocreate);
    
    // Check
     if (!infile.good()) {
      cout << "Error" << endl << endl;
      system("PAUSE");
      exit(1);
     }
    
     double value, type1, type2, type3, type4, type5;
    
    // Read the data
       infile >> value >> type1 >> type2 >> type3 >> type4 >> type5;
       while (value!=-1) {
        infile >> value >> type1 >> type2 >> type3 >> type4 >> type5;
        cout << value << "\t" << type1 << "\t" << type2 << "\t" 
             << type3 << "\t" << type4 << "\t" << type5 << endl;
       }
    
     system("PAUSE");
     return(0);
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Mmkay, "won't work" doesn't tell us anything.

    What's the actual problem? infile.good() returns false? You don't get the data in the right order?

    Try to be a little more specific as to what you expect the program to do and what it is actually doing instead.

    And lastly, welcome to cboard.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    I'm sorry, forgot a bit i see

    The first problem is the bit

    Code:
    ifstream infile("test.dat", ios::in | ios::nocreate);
    It won't compile until i delete the "ios::nocreate" part. It says:
    'nocreate' is not a member of 'std::ios'
    When i delete this part, it will compile without complaining.

    The main problem is indeed that infile.good() returns false and thus displays the error message

    I'm using Bloodshed Dev-c++ 4.9.9.2

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I do not believe ios::nocreate is standard, so you should probably leave it out.

    As far as it failing, are you sure that test.dat is located in the working directory of your program? By default that will be the same folder as your program (ie. the exe file).

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    Yes, test.dat is located in the same directory as my *.cpp file and thus the executable.

    Did i forget any headers or something?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Does something like this behave as expected?
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cstdio>
    
    using namespace std;
    
    int main()
    {
       static const char filename[] = "test.dat";
    
       // Open the file 
       ifstream infile(filename);
    
       // Check
       if ( !infile.good() )
       {
          perror(filename);
          system("PAUSE");
          exit(EXIT_FAILURE);
       }
    
       double value, type1, type2, type3, type4, type5;
    
       // Read the data
       while ( (infile >> value >> type1 >> type2 >> type3 >> type4 >> type5) &&
               value!=-1 )
       {
          cout << value << "\t" << type1 << "\t" << type2 << "\t" 
               << type3 << "\t" << type4 << "\t" << type5 << endl;
       }
    
       system("PAUSE");
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by JustMe View Post
    Yes, test.dat is located in the same directory as my *.cpp file and thus the executable.

    Did i forget any headers or something?
    Nope. Your headers are fine I believe.

    Code:
    system("dir");
    Try adding that just for now in your program, and see if test.dat is located in the output.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    4
    Hey guys,

    Thanks for the help, the last post gave me the problem.
    I'm also running a fresh windows install and windows hides the file extensions by default.

    I thought i wrote a file "test.dat" in notepad, but actually wrote a file "test.dat.txt"

    thanks again for helping out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple ifstream Question
    By Paul22000 in forum C++ Programming
    Replies: 8
    Last Post: 12-05-2008, 05:34 PM
  2. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 09:50 AM
  3. ifstream
    By Wraithan in forum Tech Board
    Replies: 3
    Last Post: 09-24-2006, 02:26 AM
  4. ofstream and ifstream for searching and writing
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-17-2003, 08:34 AM
  5. Ifstream Problems
    By IlUomo in forum Windows Programming
    Replies: 1
    Last Post: 04-24-2002, 01:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21