Reputation: 837
There is a file in directory and i'm trying to read a file but i can't. What is wrong with my code. Example is taken from http://www.cplusplus.com/forum/beginner/37208/
#include <iostream>
#include <fstream>
#include <string>
#define MAX_LEN 100
using namespace std;
string inlasning ()
{
string text;
string temp; // Added this line
ifstream file;
file.open ("D:\education\Third course\semestr 2\security\lab1.2\secret_msg.txt");
while (!file.eof())
{
getline (file, temp);
text.append (temp); // Added this line
}
cout << "THE FILE, FOR TESTING:\n" // For testing
<< text << "\n";
file.close();
return text;
}
void main ()
{
inlasning();
}
Upvotes: 1
Views: 6076
Reputation: 593
\
is an escape character, so you need to use \\
to obtain the result you intend.
This is true almost everywhere, even here in stackoverflow where you nee to use it in order to write something like this for example:
*A*
(just put the \
before the *
), otherwise (if you don't use the \
) stackoverflow will interpret the text and will output an italic A, this:
A
The same is true for bold (two asterisks... two slashes):
**A**
instead of
A
:)
... or maybe you cannot read it because it is a "secret_msg" :P (LOL)
Upvotes: 0
Reputation: 96266
In string literals \
is used as an escape character.
You have to write \\
.
Note: you should check the open
call.
On failure, the failbit flag is set (which can be checked with member fail), and depending on the value set with exceptions an exception may be thrown.
Upvotes: 1