c++关于getline的小问题
现在有一个文件abc.txt其内容为: abc bcd asd asd asd asd asd asd 就两行 打开ifstream流用getline(str,40) //str是一string类型 读取第一行,之后怎样从str内分别将abc bcd asd提取出来,存入其他三个字符串中,不想用getchar,还有别的方法吗,请赐教?
现在有一个文件abc.txt其内容为: abc bcd asd asd asd asd asd asd 就两行 打开ifstream流用getline(str,40) //str是一string类型 读取第一行,之后怎样从str内分别将abc bcd asd提取出来,存入其他三个字符串中,不想用getchar,还有别的方法吗,请赐教?
答案:用文件流
定义文本变量ifstream file("abc.txt");
file>>s1>>s2>>s3>>s4;
其他:istringstream 使用strtok函数。
char* p = strtok(str.c_str()," ");
上一个:关于C语言递归与系统栈的问题。
下一个:C语言问题 求大神