Using inFile to read from TXT yields gibberish

This is a discussion on Using inFile to read from TXT yields gibberish within the C++ Programming forums, part of the General Programming Boards category; Hello. I'm using the following code to try to just read a few names from a text file (I'm learning ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    Using inFile to read from TXT yields gibberish

    Hello. I'm using the following code to try to just read a few names from a text file (I'm learning C++ from a college book). However, when I run it, the outputFile program makes the names right, but when I read it in, I get gibberish.. like ASCII characters.

    I've tried re-starting the computer, and making sure NOTEPAD was the default .TXT document program.

    Thanks for any help.


    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      ifstream inFile;
      const int SIZE = 81; //Size limit
      char name[SIZE];
      
      inFile.open("demofile.txt");
      cout << "Reading data from the file.\n\n";
      
      inFile >> name;
      cout << name << endl;
      inFile >> name;
      cout << name << endl;
      inFile >> name;
      cout << name << endl;
      inFile >> name;
      cout << name << endl;
      
      inFile.close();
      cout << "\nDone.\n";
      
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How do you know that the line
    Code:
    inFile.open("demofile.txt");
    succeeds?

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    26

    No error checking..

    Quote Originally Posted by tabstop View Post
    How do you know that the line
    Code:
    inFile.open("demofile.txt");
    succeeds?
    oh, crap.. I just wrote this long message, then realized that you're right.. I forgot to add the frigging path.. it should be inFile.open("c:\\demofile.txt");

    Ok, thanks. I feel dumb.
    Last edited by Zzaacchh; 08-04-2008 at 10:55 PM.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    A simple:

    Code:
    if (!inFile)
    {
    }
    Will help a lot.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    Quote Originally Posted by Bubba View Post
    A simple:

    Code:
    if (!inFile)
    {
    }
    Will help a lot.
    1) There is little reason not to simply do
    Code:
    if ( !inFile.open("demofile.txt") )
    {
    }
    2) Failing to open a file is only one of the places an error can occur. Every line that deals with the file should be error checked. The best way to do this is to call inFile.exception(ios::failbit|ios::badbit) and put the whole thing into a try block. The alternative solution is to check each call to inFile for failure, and deal with it there.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read values from a txt file
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 08-30-2002, 01:37 PM
  2. Read Array pro!!Plz help!!
    By Supra in forum C Programming
    Replies: 2
    Last Post: 03-04-2002, 02:49 PM
  3. data read problem
    By Supra in forum C Programming
    Replies: 0
    Last Post: 02-03-2002, 06:02 PM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

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