I'm not understanding why i'm able to read from the file but i can't write to it. The fstream is open in both modes. Once the program finishes i haven't changed the content of the file.

Thank you in advance.

#include <fstream>
#include <string>
#include <iostream>

int main()
{
    std::string line;
    std::fstream m_File("/home/user/file.txt", std::ios::in | std::ios::out);

    if(m_File.fail())
        return -1;

    // read file
    std::getline(m_File, line);
    while(m_File.good())
    {
        std::getline(m_File, line);
        std::cout << line << std::endl;
    }

    // write to file
    std::string line2 = "test to write";
    m_File.seekp(std::ios_base::beg);
    m_File << line2;

    m_File.close();

    return 0;
}

I think you're problem is opening the file in two modes at the same time. Try opening the stream one mode at a time and closing it before you want to switch modes.

well, of course it works that way, but i want (and fstream allows) to open the stream in both modes and choose what to do.
Thanks for the reply.

Had a chance to play with your code a bit. Setting both the put pointer and the get pointer, will allow you to write to the file at the beginning, overwriting what is already there:

#include <fstream>
#include <string>
#include <iostream>
int main()
{
    std::string line;
    std::fstream m_File("text.txt", std::ios::in | std::ios::out);
    if (m_File.fail())
        return -1;
    // read file
    std::getline(m_File, line);
    while (m_File.good())
    {
        std::getline(m_File, line);
        std::cout << line << "\n";
    }
    // write to file
    std::string line2 = "test to write";
    m_File.seekp(std::ios::beg);
    m_File.seekg(std::ios::beg);
    m_File << line2 << "\n";
    m_File.close();
    return 0;
}

Thank you, but still is not working. I've changed the code a little bit, I will continue working on it.

#include <fstream>
#include <string>
#include <iostream>

int main()
{
    std::string line;
    std::fstream m_File("/home/user/file.txt", std::ios::in | std::ios::out);

    if(m_File.fail())
        return -1;

    // read file
    std::getline(m_File, line);
    while(m_File.good())
    {
        std::cout << line << std::endl;
        std::getline(m_File, line);
    }

    // write to file
    m_File.seekp(std::ios::beg);
    m_File.seekg(std::ios::beg);
    m_File << "2" << std::endl;

    m_File.close();

    return 0;
}

I did a little more investigating for you. It looks like the problem wasn't so much trying to read and write from the same stream, as it was about trying any I/O operation after you've read to the end of the stream. This set the failbit, which prevented any further I/O operations. Calling the clear() method of the stream, after the while loop allows your original code to work.

#include <fstream>
#include <string>
#include <iostream>
int main()
{
    std::string line;
    std::fstream m_File("/home/user/file.txt", std::ios::in | std::ios::out);
    if (m_File.fail())
        return -1;
    // read file
    std::getline(m_File, line);
    while (m_File.good())
    {
        std::getline(m_File, line);
        std::cout << line << std::endl;
    }
    m_File.clear();
    // write to file
    std::string line2 = "test to write";
    m_File.seekp(std::ios::beg);
    m_File << line2;
    m_File.close();
    return 0;
}

Sorry about my earlier code. I only tested it on 1 compiler and got undocumented results. This code should be good I tested it on 2 different compilers with the same result.

commented: Good catch. +12

Thank you very much, now it works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.