fileIO.cpp,RUN
#include <iostream>
#include <fstream>
#include <string>
void FileIn()
{
std::ifstream fin("data.txt"); //construct ifstream object and open the file
if (!fin.is_open())
{//fail, return
return;
}
std::string name;
int age, score;
while (fin >> name >> age >> score) //without blanks
{
std::cout << name << "\t" << age << "\t" << score << std::endl;
}
fin.close(); //fin析构时会自动调用,此处可不写
}
void FileOut()
{
std::ifstream fin;
fin.open("student.txt");//open file
if (fin.fail())
{//fail, return
return;
}
std::string name;
int age, score;
std::getline(fin, name); //with blanks
fin >> age >> score;
fin.close();
std::ofstream fout;
fout.open("studentOut.txt");//open file
fout << name << std::endl << age << std::endl << score;
fout.close();
}
int main()
{
FileIn();
FileOut();
return 0;
}