Reputation: 3
When I want to run this program it gives me the error "launch failed etc.". Futhermore it says that cout, string and close couldn't be resolved and that it expected ";" before "myfile"
#include <iostream>
#include <fstream>
#include <string>
int main () {
std::string line;
std::ifstream myfile ("C:\Users\Username\Desktop\cas.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
std::cout << line << '\n';
}
std::myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Have tried "Build project"
Upvotes: 0
Views: 92
Reputation: 1679
Try this code. I commented the changes. Basically, everything from the standard library has to be prefixed with std::
.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::string line;
// prefixed with std
std::ifstream myfile ("C:\\Users\\Username\\Desktop\\cas.txt");
// prefixed with std, escaped backslashes
if (myfile.is_open())
{
while (std::getline(myfile, line))
// prefixed with std
{
std::cout << line << '\n';
// prefixed with std
}
myfile.close();
}
else
std::cout << "Unable to open file";
// prefixed with std
return 0;
}
Upvotes: 1