ios errors

This is a discussion on ios errors within the C++ Programming forums, part of the General Programming Boards category; Hello, I am writing a small app that checks for the existence of some files. The problem is that when ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    16

    Question ios errors

    Hello,

    I am writing a small app that checks for the existence of some files. The problem is that when I create the stream, and associate a file with the stream, the fille will be created if it didnt exist. I did some reading and it said to use ios::nocreate on the open() function. Here is a code snippet below:

    #include <fstream>
    //#include <ios> //I even tried include this to help

    fin.open("student.tab", ios::in | ios::nocreate);

    When I compile this code with the ios::nocreate in it, it generates error messages saying that nocreate is not a member of 'basic_ios<char,struct std::char_traits<char> >', but yet the documentation says to do it that way. What am I doing worng?

    - Terrell

  2. #2
    bharford
    Guest
    sorry, i can't really help you, but its nice to see someone else from indiana too

  3. #3
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    I don't know about fstreams, but this is how it goes in old-fashioned style:
    Code:
    FILE* fHandle;
    fHandle = fopen(Filename, "r");
    
    if (fHandle == NULL)  printf("File not found");
    Making error is human, but for messing things thoroughly it takes a computer

  4. #4
    Registered User kitten's Avatar
    Join Date
    Aug 2001
    Posts
    109
    Oh and btw... in case the file exists it is opened by your operating system. In this case you must call fclose(fHandle);
    Making error is human, but for messing things thoroughly it takes a computer

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    25
    fstream fin(fileName);

    Try initilizing the fstream in the constructor. I believe that the nocreate and in flags can only be set at creation. But I could be wrong, initilizing at creation has always worked for me.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    16
    Thanks for your suggestions. I just started reading through more header files, and there is a header file call ios.h, so I just included that, and my errors went away. Evidentally the documentation that I was reading (from Microsoft) was wrong (imagine that)!

    Anyways, thanks for your suggestions.

    - Brice

  7. #7
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    ios::nocreate is part of the old style iostream libraries (if you use <fstream.h> it will work) but is not part of the newer libraries. As the old libraries are depreciated you should use the newer ones.

    The default behavior for the ifstream object is to only open a file if it exists, so you shouldn't need to set the ios::nocreate flag. You can test whether a file has been opened (exists) by calling the ifstream::is_open() member function after an attempt has been made to open a file.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Winsock compilation errors
    By jmd15 in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-03-2005, 08:00 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  5. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM

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