As my learning, I am trying to use c++ ifstream and its operator>> to read data from a text file using code below. The text file outdummy.txt has following contents:
just dummy
Hello ofstream
555
My questions is how to read char data present in the file into a char array or string. How to do this using the ifstream::operator>> in code below.
#include <iostream>
#include <fstream>
int main()
{
int a;
string s;
char buf[100];
ifstream in("outdummy.txt",ios_base::in);
in.operator>>(a); //How to read integer? How to read the string data.??
cout << a;
in.close();
getchar();
return 0;
}
Since you have elected to use C-strings, you can use the getline method of your
ifstream
object (notstd::getline()
which works withstd::string
s), which will allow you to specify the C-string and a maximum size for the buffer.Based on what you had, and adding an additional buffer for the second line:
However, as the other poster has proposed, try using the
std::string
and its methods, it will make your life easier.ifstream
hasios_base::in
by default. You don't need to specify it.operator>>
can be invoked directly as an operator:in >> a
.in >> s
, but the caveat is that it is whitespace-delimited, so it will read "just" by itself, without "dummy".std::getline(in, s)
.If you want to use formatted input, you have to know in advance what data to expect and read it into variables of the according data type. For example, if you know that the number is always the fifth token, as in your example, you could do this:
On the other hand, if you know that the number is always on the third line, by itself:
As you can see, to read a token as a string, you just stream it into a
std::string
. It's important to remember that the formatted input operator works token by token, and tokens are separated by whitespace (spaces, tabs, newlines). The usual fundamental choice to make is whether you process a file entirely in tokens (first version), or line by line (second version). For line-by-line processing, you usegetline
first to read one line into a string, and then use a string stream to tokenize the string.A word about validation: You cannot know whether a formatted extraction will actually succeed, because that depends on the input data. Therefore, you should always check whether an input operation succeeded, and abort parsing if it doesn't, because in case of a failure your variables won't contain the correct data, but you have no way of knowing that later. So always say it like this:
The latter construction is usually used in a
while
loop, to read an entire file line by line:You can read file contents and use a Finite State Machine for parsing.
Example:
Alternatively, you can use a tool like Flex to write your parser.