input file error message

This is a discussion on input file error message within the C++ Programming forums, part of the General Programming Boards category; im having some difficulties with fstream and giving some specific error messages: say for instance i have: ifstream in; i ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    1

    input file error message

    im having some difficulties with fstream and giving some specific error messages:

    say for instance i have:

    ifstream in;

    i need two error messages
    1) to test if the file doesnt exist, print 'file does not exist'
    2) if the file exist, but unable to be access (perhaps in read only mode)

    ive been trying to work with in.fail() for if the file does not exist, but it also seems to work for #2.......is there any other ways of getting these two error messages?

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    1. you can use ios::nocreate and ios::noreplace ->

    ios::nocreate -> wont create file, if file doesn't exists
    ios::noreplace -> wont replace file, if file exists


    2. use good/bad/fail -->

    if(!in.good() || in.bad() || in.fail())
    cout<<"something's wrong";

    or

    if(!in)
    cout<<"something's wrong";

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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