Thread: fileIn question

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    77

    fileIn question

    how do i get my program to check if the file is actually there or not

    i.e

    fileIn("text.txt")
    but text.txt doesnt exist

    thanks for any help
    Hooked On Phonics Didn't Work For Me!

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Or, for C++ stream objects...

    Code:
    ifstream infile ("/home/user/file.txt", ios::in);
    
    if (!infile) {
    
       cerr << "File does not exist!\n";
       exit (1);
    
    }
    
    //Do stuff

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    now it tells me that every file that i enter doesnt exist
    Hooked On Phonics Didn't Work For Me!

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    OK...

    First, are you using vVv's file pointer method or the stream menthod I described?

    Second, if possible, post the control statement that you use to determine if a file exists.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    i tried using vvv's but i couldnt get it to work, now im using yours but every file in try it tells me that it doesnt exist
    Hooked On Phonics Didn't Work For Me!

  6. #6
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Erm, you problem lies in your use of globals.

    globals in this context are bad.

    Your problem is probably that the global fileIn (exists at all) is an int, not an ifstream.

    If you really must use globals (which you really mustn't) You need to make the global fileIn an ifstream object and use fileIn.open("filename", ios::in) to open it.

    But you really ought to get those globals squared away. They cause nothing but trouble in that context, as you problem here so aptly illustrates.
    Last edited by Imperito; 11-03-2002 at 12:47 PM.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    i dont understand
    Hooked On Phonics Didn't Work For Me!

  8. #8
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    The way you declare your variables globally (outside of any function) is what is causing this problem.

    The quick fix would be to chance the global declaration of "int fileIn" to "ifstream fileIn". But this isn't a good solution.

    Not by a long shot.

    Your use of global variable is only going to make more problems as the program gets bigger, you really need to switch over to parameter passing and return values for your functions.

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    The quick fix would be to chance the global declaration of "int fileIn" to "ifstream fileIn". But this isn't a good solution.
    the program is most likely not going to get any bigger so how would i do what you mentioned here.
    Hooked On Phonics Didn't Work For Me!

  10. #10
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    Just that.

    At the top of the file, in your global variable declarations (Which regardless of program size you really ought not to use, its a bad habit to get into and it keeps you from developing proper parameter passing) at the very bottom you have "int fileIn" change that to "ifstream fileIn"

    Then later on, when you have ifstream fileIn("whatever", ios::in), change it to fileIn.open("whatever", ios::in);

    But I'll say it again, and once more for clarity, don't use global variables like that, its bad.

  11. #11
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    i just cant figure it out. i changed the stuff you said to change to fix it but i just cant get it right for some reason.

    the problem seems to be the changing the int filein to the ifstream filein.
    Hooked On Phonics Didn't Work For Me!

  12. #12
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    ok, ive been researching on this subject all day and ive finally narrowed it down to 2 errors which are..

    when i first used ifstream fileIn(/*file here*/)
    i used fileIn >> array[i] during a loop to open the contents into an array.

    now that i switched to using the method vVv described above what would i use to put the contents of the file into the array?
    Hooked On Phonics Didn't Work For Me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM