Archived

This topic is now archived and is closed to further replies.

file input ?

This topic is 5278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Ive been looking for a way to do some file input in c++. If i google it, i end up with about a million different ways to do it, but none of them are what i want. Most of them use c-style strings ( char* ), and most of them read one character from the file at a time using getch() or something. I want a method that uses std::string instead of an array of characters,and i want to be able to read a whole line at a time. Ive been looking at fstream , and within that, ifstream. It provides a getline() function that reads a whole line, but it stores it in a c-style string, not an std::string. can someone post the 3 or 4 lines of code needed to acheive what im trying to do? Im sure it exists but i just cant find an example. tanks! [edited by - AndreTheGiant on August 9, 2003 5:30:10 PM] [edited by - AndreTheGiant on August 9, 2003 5:30:37 PM]

Share on other sites
ifstream("file");
string s;
getline(file,s);

Share on other sites
ok sure make me look stoopid

so what headers did you include for that to work? and what about the using declaration?
I dont like to say

using namespace std;

I like to specifiy each thing separately like this:

include
using std::cout;
using std::endl;

thanks!

Share on other sites
quote:
Original post by AndreTheGiant
ok sure make me look stoopid

so what headers did you include for that to work? and what about the using declaration?
I dont like to say

using namespace std;

I like to specifiy each thing separately like this:

include
using std::cout;
using std::endl;

thanks!

#include <fstream>using namespace std;ostream fout;fout.open("file");fout << data;fout.close();