Differences in read file using ifstream in g ++ and msvc

When using the ifstream class to read words from an input file, I used the following expression:

 #include <iostream>
 #include <fstream>

 int main(int argc, char *argv[])
 {
   std::ifstream inputStream(myFile.txt);
   std::string myString;
   myFile.open()
   while(myFile.good())
   {
      myFile >> myString;
      printf("%s \n", myString);
   }
   return 0;
 }

MyFile.txt content: "This is a simple program."

It is compiled and executed, as expected, using the g ++ compiler.

However, the same code compiled using msvc 2008 returns an error in the extraction operator (→), requiring me to replace std :: string with either an initialized array of characters, or any of the supported types. This threw me away as I expected that using the standard library would be the same for all implementations.
I understand the compilation error and know how to fix it using c_str ().

, , - , .
  starndard!!

EDIT: . myFile.txt.

+3
2

, #include <string>. Microsoft <iostream> ( ) std::string , , , .

- std::string, , .

, while (myfile.good()) ... - , , :

while (myfile>>myString)
    std::cout << myString << " \n";

:

#include <string>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <iostream>

int main() { 
    std::ifstream myfile("input.txt");

    std::copy(std::istream_iterator<std::string>(myfile),
              std::istream_iterator<std::string>(),
              std::ostream_iterator<std::string>(std::cout, " \n"));
    return 0;
}
+4

MSVC 2010:

std::ifstream inputStream;
std::string myString;
inputStream.open("myFile.txt", std::ifstream::in);

while(inputStream.good())
{
    inputStream >> myString;
}

: std::ifstream::in , . , .

0

All Articles