ifstream problems.

This is a discussion on ifstream problems. within the C++ Programming forums, part of the General Programming Boards category; Could someone please tell me the correct method of locating a file through ifstream. This it my attempt, which doesn't ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    10

    ifstream problems.

    Could someone please tell me the correct method of locating a file through ifstream. This it my attempt, which doesn't seem to be working.

    Code:
      //Opens for reading the file
      ifstream b_file ( "C:\Program Files\FAH\unitinfo.txt" );
      //Reads one string from the file
      b_file>> str;
      //Should output 'this'
      cout<< str <<"\n";
    Thanks,
    Gareth.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Use forward slashes instead of back slashes.
    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.*

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Alternatively, to represent backslashes in a string, you'll need two of them together.

    Code:
    ifstream b_file ( "C:\\Program Files\\FAH\\unitinfo.txt" );

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    10
    Thanks for the quick reply.

    Isn't this convention a little strange, bearing in mind I copy/pasted the file address straight from a shortcut property box?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    It's more proper in programming to put files into a default folder, so you don't need to type the full address when declaring the ifstream.
    Sent from my iPadŽ

  6. #6
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Isn't this convention a little strange, bearing in mind I copy/pasted the file address straight from a shortcut property box?
    No.
    The '\' is a operator in c++. So to tell the compiler to use a real \ and not the \ operator you have to use two \'s.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    For instance if you wanted to type quotes inside of a literal constant you'd have to write:

    Code:
    cout << "Hello \"Mr President\". ";
    It's also where you get operators like \n, \b, \0... etc etc.
    Sent from my iPadŽ

  8. #8
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Technically, they're called escape sequences, not operators.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Well, thank you for that correction.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  2. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 01:17 PM
  3. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  4. Read problems
    By xddxogm3 in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2003, 01:17 AM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM

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