
C++里文件读入的getline问题
C++里面读入文件时有个getline函数如#include<fstream>ifsteamindata;对象有一个函数indata.getline(char*,int,...
C++里面读入文件时有个getline函数
如
#include<fstream>
ifsteam indata;
对象有一个函数indata.getline(char*,int,char)
其中的getline只能将一行的数据存入char数组中
有没有类似于这个函数功能的,但是把文件中的一行数据存入string型里面的函数?
或者高速的把char数组转化为string型的方法,别说用for一个个转
那样速度太慢 展开
如
#include<fstream>
ifsteam indata;
对象有一个函数indata.getline(char*,int,char)
其中的getline只能将一行的数据存入char数组中
有没有类似于这个函数功能的,但是把文件中的一行数据存入string型里面的函数?
或者高速的把char数组转化为string型的方法,别说用for一个个转
那样速度太慢 展开
展开全部
string类,比方说存了一个private char *型数据,但由于string类并没有封装“调用数据头指针”这样的函数,所以无法直接调用string类中字符串数据的char *型地址,无法将其作为参数放在getline这样的函数中。因为getline的第一个参数要求是char *型。
可以先将被get的line存在一个字符串里,再用string类的"="操作符赋给string类。
可以先将被get的line存在一个字符串里,再用string类的"="操作符赋给string类。
展开全部
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string path("D:\\a.txt");
string str1;
ifstream infile(path.c_str(),ifstream::in);
if (!infile)
{
cout<<"open file error"<<endl;
return 0;
}
while (!infile.eof())
{
//将一行数据存放到str1中
getline(infile,str1);
cout<<str1<<endl;
}
char ch[]="abcde";
string str2;
str2=ch;
cout<<str2<<endl;
infile.close();
return 0;
}
#include <fstream>
#include <string>
using namespace std;
int main()
{
string path("D:\\a.txt");
string str1;
ifstream infile(path.c_str(),ifstream::in);
if (!infile)
{
cout<<"open file error"<<endl;
return 0;
}
while (!infile.eof())
{
//将一行数据存放到str1中
getline(infile,str1);
cout<<str1<<endl;
}
char ch[]="abcde";
string str2;
str2=ch;
cout<<str2<<endl;
infile.close();
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
高速转化
string s;
char *t="kkk";
s=t;
cout<<s<<endl;
string s;
char *t="kkk";
s=t;
cout<<s<<endl;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询