Good day guys.

Im just new in c++. I just want a little help/guide/sample code or steps on how to get files from a certain txt files(notepad) and display it on screen. Vice Versa.

Thank you for giving time.

God bless.

Recommended Answers

All 4 Replies

Depends on the file's contents. As a general rule of thumb, just open the file as a normal text file and read its contents one line at a time. Beyond that, you will have to tell us what's in the file.

The text file has 5 lines of text data(ex. name of employee). I want to pull that text data and display it on c++ screen.

A matter of correction. i am using visual c++ 6.0.

Thank you for giving time.

Here is one way of reading from a text file and displaying the contents:

string sentence;
    string readSentence;
    
    //open file to read
    ifstream inData;
    inData.open("YourFileName.txt");
    
    //read first line from the file
    getline(inData, readSentence);

    //show error if file not found
    if(!inData){
          cout<< "Can't open input file.\n";
          getchar();
    }
    
    //while there are more lines to read from file
    while(inData){     
          sentence = readSentence; 

          //write data to screen     
          cout << sentence << endl;
          
          //read another line
          getline(inData, readSentence);
    }
    
    //close file
    inData.close();

Here is one way of reading from a text file and displaying the contents:

string sentence;
    string readSentence;
    
    //open file to read
    ifstream inData;
    inData.open("YourFileName.txt");
    
    //read first line from the file
    getline(inData, readSentence);

    //show error if file not found
    if(!inData){
          cout<< "Can't open input file.\n";
          getchar();
    }
    
    //while there are more lines to read from file
    while(inData){     
          sentence = readSentence; 

          //write data to screen     
          cout << sentence << endl;
          
          //read another line
          getline(inData, readSentence);
    }
    
    //close file
    inData.close();

thank you very much. i know this works. Il try this tonight. God bless.

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.