Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Can you write to a file using this compiler?

+3 votes
asked Mar 18 by Mary Jacobsen (150 points)
I am very new to C++ and I am taking a class where my editor and compiler,  Visual Studio, is not working on my laptop.  Can I write to a file using this online compiler and if so how?  I have the header using <fstream> and I created a file in notepad.  What else do I need to do?

1 Answer

0 votes
answered Mar 18 by Peter Minarik (22,320 points)

Note

I would fix Visual Studio on your laptop.

Nevertheless, you can use OnlineGDB to write and test some simple projects.

The Problem

Let me see if I understood your problem correctly.

You have a file, let's call it myfile.txt. You would like to (read? and) write to this file.

The Solution

Yes, you can use fstream, however, the program compile and run by OnlineGDB can only access files that are on the server.

Fear not though, as you can add any files to your project. There are a "New File" and an "Upload File" icons next to the "Run" button. With the help of these, you can upload and download files as part of your projects. You can also download files if you click on the tab representing your file.

I've made a small sample project for you. You will see that the program reads the content of myfile.txt then adds an extra line to it which you can see change in the editor after the program ran.

Here's the source below (copied from the project linked above):

main.cpp

#include <fstream>
#include <iostream>

static void ReadFile()
{
    std::ifstream file("myfile.txt");
    std::string line;
    if (!file.is_open())
        return;

    while (std::getline(file, line))
        std::cout << line << std::endl;

    file.close();
}

static void WriteFile()
{
    std::ofstream file("myfile.txt", std::ios_base::app);
    if (!file.is_open())
        return;

    file << "Another line added" << std::endl;
    file.close();
}

int main()
{
    ReadFile();
    WriteFile();
    return 0;
}

myfile.txt (before running the program)

Hello world!
This is a test.

myfile.txt (after running the program)

Hello world!
This is a test.
Another line added

commented Mar 18 by Mary Jacobsen (150 points)
I have had several people walk me through trying to fix Visual Studio on my laptop to no avail.  
 I have even done a factory reset, no luck. (What a pain with no positive outcome!)  Problem is I am taking a college class that ends May 7.  I need this class to get my CS teaching endorsement added to my other endorsements to continue teaching CS , so I don't think I need Visual Studio long term...    IDK if it would be worth the effort?
commented Mar 19 by Peter Minarik (22,320 points)
If you don't think you'll need Visual Studio after May, maybe there's no point trying to struggle with it, as there are various alternatives (see online compilers XD).

However, I have been using Visual Studio since 2005 or so. I never had problem with installing it.

Also, you mentioned CS (CSharp) but in your original post you were asking for C++ -- hence I provided my solution in C++. If you'd need CSharp (C#) instead, you could still write the files, but of course the code would be different.
commented Mar 19 by Mary Jacobsen (150 points)
Naw... CS in my world is short for Computer Science as in getting my Computer Science endorsement.  I have been teaching high school Business for 20 years but now there are requirements to be endorsed to teach computer science in the state of Wyoming.  As part of my job, I have to get this endorsement so that I can teach a class in computer science.  I am not looking as CSharp,  I am C++ all this semester.   Thanks for your help.  Can I look you up if I have more questions?  LOL
commented Mar 22 by Peter Minarik (22,320 points)
You can drop a message here or post a new question on OnlineGDB. I look at new questions every workday.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...