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.

I am trying to read the strings from the text file, but It's not working. Where am I wrong?

+2 votes
asked Sep 15, 2022 by Nirali Patel (750 points)
#include <iostream>
#include <fstream>
#include  <string>

using namespace std;

int main()
{
    
    char user_input;
    string str;
    fstream newfile;

    //checks if the user wants to enter a string
    cout<<"Do you want to enter a string?"<<endl;
    cin>>user_input;
    
    //if the user enters n, the program terminates
    if(user_input == 'n')
    {
        cout<<"Your done"<<endl;
    }
    
    //if the user enters y, it asks to enter a string over the alphabet
    if(user_input == 'y')
    {
        cout<<"Please enter a sting over a alphabet:"<<endl;
        //opens the file to read the string
        newfile.open("in.txt", ios::in);
        
        //checks whether file is open
        if (newfile. is_open())
        {
            string str;
            //reads data from file object and puts it into string
            while (getline(newfile, str))
            {
                //prints the data of the string
                cout<< str << "\n";
            }
            //closes the file object
            newfile.close();
        }
    
    }

    return 0;
}

2 Answers

0 votes
answered Sep 15, 2022 by xDELLx (10,500 points)
        newfile.open("in.txt", ios::in);


The above line can be modified to use the absolute path(instead of the relative path,right now).

eg newfile.open("./in.txt", ios::in); or newfile.open("/home/user/check/in.txt", ios::in);

Or you can place the in.txt file in same directory as the binary.

Or make sure the file in.txt exists !!

for reference:https://onlinegdb.com/PQ86gLPO8

0 votes
answered Sep 16, 2022 by Vedant Hiwarde Patil (150 points)

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

using namespace std;

int main()
{
    
    char user_input;
    string str;
    fstream newfile;

    //checks if the user wants to enter a string
    cout<<"Do you want to enter a string?"<<endl;
    cin>>user_input;
    
    //if the user enters n, the program terminates
    if(user_input == 'n')
    {
        cout<<"Your done"<<endl;
    }
    
    //if the user enters y, it asks to enter a string over the alphabet
    if(user_input == 'y')
    {
        cout<<"Please enter a sting over a alphabet:"<<endl;
        //opens the file to read the string
        newfile.open("in.txt", ios::in);
        
        //checks whether file is open
        if (newfile. is_open())
        {
            string str;
            //reads data from file object and puts it into string
            while (getline(newfile, str))
            {
                //prints the data of the string
                cout<< str << "\n";
            }
            //closes the file object
            newfile.close();
        }
    
    }

    return 0;
}

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.
...