0
#include <stream>  
#include <iostream>
#include <stream>
#include <iosfwd>
#include <string>

using namespace std;

int main(int argc, const char * argv[]) {
    string op = "original";
    ifstream file;
    file.open("sample.txt");
    if(!file) {
        cout << "Error: unable to open file." << endl;
    }
    else {
        while(!file.eof()) {
            file >> op;
            cout << op << endl;
        }
    }

    file.close();
    return 0;
}

sample.txt only contains the word five

I run this code in Visual Studio and the output is

five

I run the exact same code(direct copy and paste, and data.txt is added for sure) in Xcode and what gets printed in the command line is

original

The bug I see is that

file >> op    

never really worked in Xcode. WHY?

Kid_Learning_C
  • 2,605
  • 4
  • 39
  • 71

2 Answers2

0

Here is example of how can you get the string from text file

#include <fstream>

std::string getDataFromFile(std::string filepath)
{
   ifstream fin;


   std::ifstream file(filepath);
   std::string temp;
   stringstream fileData("");
   while(std::getline(file, temp)) {
       fileData << temp << "\r\n";
   }
   file.close();

   return fileData.str();

}

Ron Fridman
  • 304
  • 1
  • 6
-2

In the cout statement on line 20 (cout << op << "haha" << endl;) the text saying << "haha" must be deleted. Or if your intended output should equal this:

five
haha

Try doing this:

cout << op << endl <<"haha";

I do find this a simple to answer question though. I am so sorry my answer did not output correctly :(.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190