Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Confused Newbie...File IO...please help

michaeltmichaelt Member Posts: 4
Hi... I found the following code whilst browsing alt.comp.lang.learn.c++

It gets a user to enter a filename, opens the file and then writes hello world to it.

string s;
cout << "enter filename: ";
cin >> s;
ofstream f(s.c_str());
f << "hello world
";

Now I want to get the user to enter a filename, open the file and then read from it.
I wrote the following but no luck. It did compile but it didn't read the file

string s;
cout << "enter filename: ";
cin >> s;
ifstream f(s.c_str()); // Does this line make sense?

while(!f.eof())
{
f.get(ch);
cout << ch << endl;
}

Have I got the approach to opening and reading files entirely wrong by using
ifstream f(s.c_str()) and get(ch)

Please remember I'm a newbie (six weeks old) and my world is full of confusion :)

Many thanks for your time

Kind Regards

Robert

Comments

  • spurs01spurs01 Member Posts: 44
    while(!feof()) /*i think it should be feof() not f.eof() */
    {
    f.get(ch);
    cout << ch << endl;
    }


    travis
    : Hi... I found the following code whilst browsing alt.comp.lang.learn.c++
    :
    : It gets a user to enter a filename, opens the file and then writes hello world to it.
    :
    : string s;
    : cout << "enter filename: ";
    : cin >> s;
    : ofstream f(s.c_str());
    : f << "hello world
    ";
    :
    : Now I want to get the user to enter a filename, open the file and then read from it.
    : I wrote the following but no luck. It did compile but it didn't read the file
    :
    : string s;
    : cout << "enter filename: ";
    : cin >> s;
    : ifstream f(s.c_str()); // Does this line make sense?
    :
    : while(!f.eof())
    : {
    : f.get(ch);
    : cout << ch << endl;
    : }
    :
    : Have I got the approach to opening and reading files entirely wrong by using
    : ifstream f(s.c_str()) and get(ch)
    :
    : Please remember I'm a newbie (six weeks old) and my world is full of confusion :)
    :
    : Many thanks for your time
    :
    : Kind Regards
    :
    : Robert
    :

  • terranterran Member Posts: 2
    : while(!feof()) /*i think it should be feof() not f.eof() */
    : {
    : f.get(ch);
    : cout << ch << endl;
    : }

    feof() is the function in stdio (C's standard IO library) that checks for end of file for a given file descriptor. You NEVER mix stdio stuff with iostream unless you want problems (or unless you call ios::sync_with_stdio(), which you shouldn't do unless you have a good reason). Wo, f.eof() is the correct call.


    : : string s;
    : : cout << "enter filename: ";
    : : cin >> s;
    : : ifstream f(s.c_str()); // Does this line make sense?

    Yes, this line makes sense. Iostream is kind of a braindead library (personal opinion showing here). The constructor for iostream doesn't know how to take STL style string. It does know how to take C style strings though (char*). So basically, you need to get the c-style string from the STL string, so you call s.c_str() which returns a "const char*" representing the STL string.

    : : Have I got the approach to opening and reading files entirely wrong by using
    : : ifstream f(s.c_str()) and get(ch)

    Yep, this is correct. The program should work just fine. The problem is probably actually in you not typing in the right filename. After opening a file, *ALWAYS* check if the stream opened succesfully.

    Add this right after ifstream f(s.c_str());

    if( !f ) {
    cerr << "Unable to open file: " << s << << endl;
    return 1;
    }


    This just tests if there are ANY errors in the ifstream (not just if you've reached the end of file). My guess is something is wrong with its attempt to open the file.

    I'm kind of bad at estimating what is confusing and what is not. So if something is not clear, let me know and I'll try to clear it up.

    -Albert
  • michaeltmichaelt Member Posts: 4
  • Shawn CarterShawn Carter Member Posts: 0

    ________ \ http://forcoder.org \ free video tutorials and ebooks about [ Delphi, Visual Basic, Objective-C, C, C#, MATLAB, Scratch, Go, Perl, Python, Java, Visual Basic .NET, Ruby, Swift, Assembly, R, JavaScript, PHP, PL/SQL, C++ Scheme, LabVIEW, Fortran, Transact-SQL, Hack, Dart, Ada, Alice, D, ML, ABAP, COBOL, Clojure, Lisp, Bash, Crystal, F#, Rust, Kotlin, Prolog, Awk, Apex, Julia, Scala, VBScript, FoxPro, SAS, Erlang, Lua, Logo ] ____________

Sign In or Register to comment.