reading from file using a function problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: reading from file using a function problem

  1. #1
    Join Date
    Apr 2010
    Posts
    7

    Post reading from file using a function problem

    Code:
    void Lines::load(string filename)
    {
        string lines;
    
        ifstream inFile;
        inFile.open(filename.stx);
        if (!inFile)
        {
            cout<<"error";
        }
        while (!inFile.eof())
        {
            while(inFile.eof() != '/0')
            {
                getline(inFile,lines);
            }
        }
    i have a function : load(string filename)
    which performs the read file function the "string filename" is the name of the file i input thought "cin", but the function can't seem to be working properly in inFile.open(filename.stx).

    any help would be appreciated

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,275

    Re: reading from file using a function problem

    Code:
    filename.c_str();
    1- Function name is c_str, not stx
    2- it is a function, not a member, so remember to add ().

    http://www.cplusplus.com/reference/string/string/
    http://www.cplusplus.com/reference/string/string/c_str/
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Apr 2010
    Posts
    7

    Re: reading from file using a function problem

    thanks for reply monarch, the .stx is the extension of the file that i'm supposed to open...
    i've included the .c_str() but still couldn't get it working :'(

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,275

    Re: reading from file using a function problem

    Don't confuse the string object, and the character array.

    filename is a string. filename.c_str() is a character array.

    filename should already contain "youfilename.ext". If it doesnt contain that, than you can do this:

    Code:
    filename+=".stx";
    ifstream inFile;
        inFile.open(filename.c_str());
    Last edited by monarch_dodra; July 30th, 2010 at 05:37 AM.
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Apr 2010
    Posts
    7

    Re: reading from file using a function problem

    thanks alot monarch, you've been a great help =)

    i've managed to read the whole file and print it out using
    Code:
     cout<<lines;
    now, but i still have a question.

    Assuming my files have the words => "Hi how are you doing."
    i have the 5 string variables:
    string a,b,c,d,e;
    i wanna input from the file Hi to a, how to b are to c and so on, how do i do so?

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,275

    Re: reading from file using a function problem

    your file is a stream, so it works the same ways as cin and cout, so...

    Code:
    myFile >> a;
    myFile >> b;
    myFile >> c;
    myFile >> d;
    myFile >> e;
    if ( !myFile ) {std::cout << "an error occured" << std::endl;}
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,885

    Re: reading from file using a function problem

    in >> a >> b >> c >> d >> e

    Also, your eof-checking can be simplified:
    Code:
    while (getline(inFile,lines))
    {
       ...
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center