std::getline for an ifstream but using a char* ins

2019-07-22 03:47发布

问题:

I want to use the getline function with a char*.

I don't want to use std::string because I have a function that takes char* as parameters and writes to them and I don't want to write a whole new one just for strings.

回答1:

Simple answer for a simple question: use the stream's member function getline instead of the free function.

#include <fstream>

...

std::fstream my_stream;
char buffer[ 1000 ];

my_stream.getline( buffer, sizeof buffer );


回答2:

you can use getline from istream

istream& getline (char* s, streamsize n );


回答3:

Simple enough

char buffer[200];
cin.getline(buffer, sizeof buffer);

But there is no such thing as the string library, so your attempts not to include it are bound to be successful!