c++文件的读取和写入

#include
#include
#include
#include
int main(int args, char**argv)
{
std::ifstream fin("E://data.txt", std::ios::in);
char line[1024]={0};
std::string x = "";
std::string y = "";
std::string z = "";
while(fin.getline(line, sizeof(line)))
{
std::stringstream word(line);
word >> x;
word >> y;
word >> z;
std::cout << "x: " << x << std::endl;
std::cout << "y: " << y << std::endl;
std::cout << "z: " << z << std::endl;
}
fin.clear();
fin.close();
return 0;
}


方法二:按行读取
#include
#include
using namespace std;
char s[80];
int main()
{
string a[100]; //采用 string 类型,存100行的文本,不要用数组
int i=0;
ifstream infile;
infile.open("E:\\data.txt",ios::in);
while(!infile.eof()) // 若未到文件结束一直循环
{
infile.getline(infile, a[i], '\n');//读取一行,以换行符结束,存入 a[] 中
i++; //下一行
}
for(int ii=0;ii{
cout<}
infile.close();
}
方法三:成功读取文件
#include
#include
using namespace std;
char s[80];
void main(void)
{
char a;
char s[80];
ifstream infile;
infile.open("e:\\data.txt",ios::in);
while(!infile.eof())//文件没有结束则继续读取.
{
infile.getline(s,80);
cout<}
infile.close();
a=getchar();
}

相关主题
相关文档
最新文档