Skip to main content
gamedev.net
🔒 Locked

file input ?

Started by AndreTheGiant Aug 9, 2003 at 4:29 PM 2 replies 350+ views
Original Post
AndreTheGiant
AndreTheGiant
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]
C-Junkie
C-Junkie
ifstream("file");
string s;
getline(file,s);
AndreTheGiant
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!
raven_soul
raven_soul
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();

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.