std::getline for an ifstream but using a char* instead of a string [closed]

北慕城南 提交于 2019-12-11 00:26:06

问题


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!



来源:https://stackoverflow.com/questions/7119854/stdgetline-for-an-ifstream-but-using-a-char-instead-of-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!