hey,
I need to read from a file but withput using string...
I tried this...
// reading a text file

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
char line[120];
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}

It's still asking for string.. how can I make it work?

Recommended Answers

If you really don't want to use a string object, you can read the file into your aray of char. It would be much better to use a string.

http://www.cplusplus.com/reference/istream/istream/getline/

myfile.getline(line, 120);

Using it as the condition in a while loop isn't …

Jump to Post

All 2 Replies

If you really don't want to use a string object, you can read the file into your aray of char. It would be much better to use a string.

http://www.cplusplus.com/reference/istream/istream/getline/

myfile.getline(line, 120);

Using it as the condition in a while loop isn't going to work.

thanks it works!!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.