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.

fstream commands in OnlineGDB?

+3 votes
asked Jan 21, 2022 by Connor Becz (190 points)
edited Jan 22, 2022 by Connor Becz

Hi guys,

I just started using this website recently to help study for my college C++ class.

Today I was trying to write a program that reads input data from a .txt file and prints it to another .txt file named by the user.

I wrote the code to do this, but when I tried to run it, I got a message that the iostream objects are protected.  I actually don't even understand what this means really, is there any way to have a program in Online GDB read or print data files?

Just wondering, as it would be nice actually test my code and see if I did the exercise right.

Thanks,

EDIT: Here is data file I created as well as the portion of the code where I open the files and initialize the istream and ostream objects.  The program reads from a txt file (I will show below) and prints to an output txt file.  The purpose of the assignment is to write a code that will read a file containing only integers and whitespace, and print the number of integers and the number of individual lines.  It doesn't have to print to an output file per se, getting it to print to standard output would be just fine as well.

Main Program (not complete, just the pertinent part):

/******************************************************************************

Problem 5-25

*******************************************************************************/
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
    // Declare and initialize objects
    int count, entries, data, integers{0};
    char space;
    istream fin;
    ostream fout;
    string filename;
    
    // Collect name of file
    cout << "Enter name of input file:\n";
    cin >> filename;
    
    // Open Input File and Check for Errors
    fin.open(filename);
    if(fin.fail())
    {
        cerr << "Error opening input file " << filename << endl;
        exit (1);
    }
    
    // Open Report File 
    fout.open("Report.txt");

Data File (input.txt):

 10
5 3 4
1 2
6
7
8 9
3 2 1
2 9
3 9
46 24
82 100 99

In the data file I created, I currently am using a counter-controlled loop, where the number of entries is read as the first data point.  

Anybody who knows why I am getting the error message when I try to initialize the istream and ostream and call these objects, it would be greatly appreciated.  Here is the error log I get:

main.cpp:18:13: error: ‘std::basic_istream<_CharT, _Traits>::basic_istream() [with _CharT = char; _Traits = std::char_traits]’ is protected within this context
   18 |     istream fin;
      |             ^~~
In file included from /usr/include/c++/9/iostream:40,
                 from main.cpp:6:
/usr/include/c++/9/istream:606:7: note: declared protected here
  606 |       basic_istream()
      |       ^~~~~~~~~~~~~
main.cpp:19:13: error: ‘std::basic_ostream<_CharT, _Traits>::basic_ostream() [with _CharT = char; _Traits = std::char_traits]’ is protected within this context
   19 |     ostream fout;
      |             ^~~~
In file included from /usr/include/c++/9/iostream:39,
                 from main.cpp:6:
/usr/include/c++/9/ostream:390:7: note: declared protected here
  390 |       basic_ostream()
      |       ^~~~~~~~~~~~~
main.cpp:27:9: error: ‘std::istream’ {aka ‘class std::basic_istream’} has no member named ‘open’
   27 |     fin.open(filename);
      |         ^~~~
main.cpp:35:10: error: ‘std::ostream’ {aka ‘class std::basic_ostream’} has no member named ‘open’
   35 |     fout.open("Report.txt");
      |          ^~~~

1 Answer

+1 vote
answered Jan 21, 2022 by Peter Minarik (101,360 points)
selected Jan 27, 2022 by Connor Becz
 
Best answer

Yes, OnlineGDB supports reading/writing files.

Can you share your code so one can investigate what happened in your specific case?

Here's a small example of reading from a file (just the first line) and writing the content to another file. After executing the below code you should see a new file added to your project: "1stLine.txt"

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

using namespace std;

int main()
{
    ifstream inputFile("main.cpp");
    if (!inputFile.is_open())
    {
        cerr << "File not found!" << endl;
        return -1;
    }

    string line;
    getline(inputFile, line);
    inputFile.close();
    
    ofstream outputFile("1stLine.txt");
    if (!outputFile.is_open())
    {
        cerr << "File not found!" << endl;
        return -1;
    }
    
    outputFile << line;
    outputFile.close();

    return 0;
}

I hope this helps.

commented Jan 22, 2022 by Connor Becz (190 points)
Hi Peter, thanks for your response.

I did edit my post to include the portion of the code that is not working, as well as the error log I get and the input data file input.txt.


Thanks
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...