Thread: extra character at end!

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

    extra character at end!

    hi guys,

    i'm just trying to read in a file and output it chracter by character, the problem is i get an extra output of the last character, why does it still go through my loop when the condition has been met, also is there any way i can make this code more efficient:

    #include <iostream>
    #include <fstream>
    using namespace std ;

    int main(void)
    {
    ifstream inFile ;
    char strFileName[10] ;
    char chFileCharacter ;

    cout << "Please enter filename: " ;
    cin >> strFileName;

    inFile.open(strFileName);

    if (!inFile)
    {
    cout << "File not found" << endl ;
    }

    while (!inFile.eof())
    {
    inFile >> chFileCharacter ;
    cout << chFileCharacter ;
    }

    inFile.close();

    return 0 ;
    }

    oh yeah, one other thing, why can't i use a string variable rather than a char array to open a file, it gave me a funny error.

    Thanks guys.

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You're not testing for eof until after it's been read/written. Try something like -

    while (inFile >> chFileCharacter)


    >why can't i use a string variable rather than a char array to open a file, it gave me a funny error.

    Do -

    inFile.open(strFileName.c_str());

  3. #3
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Originally posted by Sorensen
    inFile.open(strFileName.c_str());
    Ya, if you dont do the ".c_str()" it doesnt know what do do with the string you inputed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. about wide character and multiple byte character
    By George2 in forum C Programming
    Replies: 3
    Last Post: 05-22-2006, 08:11 PM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM