Results 1 to 4 of 4

Thread: c ++ loading a file to string to send via socket

  1. #1
    Join Date
    Jul 2013
    Beans
    82

    c ++ loading a file to string to send via socket

    Ok so I have a client server program that I wrote in c++. Took me a while! As I am not really an expert.
    The problem that I encountered was following:
    When I read from a file in to a string it only loads the first line into a string and that that is what is will send.
    If need be I can post the whole code but I figured it would be more efficient if I posted the part of the code where the problem is.

    Code:
    string readFile()
    {
        ifstream read("LOG.TXT");
        string str;
        int index = 0;
        if (read.is_open())
        {
            while (!read.eof()) //Tried using read.good()
            {
                getline(read, str);
                cout << str << endl;
                return str;
            }
        }
        else
            cout << "\nEror file is closed\n";
    }
    Any help any advice is welcomed.

  2. #2
    Join Date
    Jul 2013
    Beans
    82

    Re: c ++ loading a file to string to send via socket

    Solved it like this if anybody needs it.

    [CODE]string readFile()
    {
    ifstream read("LOG.TXT");
    string str, str1;

    int index = 0;
    if (read.is_open())
    {
    getline(read, str);
    while (read)
    {
    str1 += str;
    getline(read, str);
    }
    //cout << str1 << endl;
    return str1;
    }
    else
    cout << "\nEror file is closed\n";
    }[\CODE]

  3. #3
    Join Date
    Nov 2012
    Location
    Halloween Town
    Beans
    Hidden!
    Distro
    Xubuntu Development Release

    Re: c ++ loading a file to string to send via socket

    Quote Originally Posted by brick2 View Post
    Solved it like this if anybody needs it.
    <...snip...>
    Since you solved it, please mark your thread as solved. Scroll to the top of your thread and look for the Thread Tools menu item on the right of the toolbar, click on this menu item to produce a dropdown menu and then click "Mark this thread as solved".

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,245
    Distro
    Kubuntu

    Re: c ++ loading a file to string to send via socket

    Quote Originally Posted by brick2 View Post
    Solved it like this if anybody needs it.

    Code:
    string readFile()
    {
        ifstream read("LOG.TXT");
        string str, str1;
    
        int index = 0;
        if (read.is_open())
        {
            getline(read, str);
            while (read)
            {
                str1 += str;
                getline(read, str);
            }
            //cout << str1 << endl;
            return str1;
        }
        else
            cout << "\nEror file is closed\n";
    }
    Remember that getline() returns the stream being used, which then can be used directly as your conditional statement (the stream's operator bool() method is implicitly called for the conversion to a bool value) in your while-loop.

    Code:
    string readFile()
    {
        ifstream read("LOG.TXT");
        string str, str1;
    
        while (getline(read, str))
        {
            str1 += str;
        }
    
        return str1;
    }
    P.S. I would recommend that you pass the file name as a parameter to the function, and that you take action (such as throw an exception) if you are unable to open the file.

Bookmarks

Posting Permissions

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