0

是否有关于阅读、操作编写 C++ 文件流的易于理解和客观的指南?在这一点上,任何事情都会有所帮助。

难道没有一个简单、直接的模型可以用来读/写/操作一个由字符串、整数、双精度数、字符和布尔值组成的 txt 文档中的项目列表吗?

我可以加载文件,读取所有内容并将其转储到结构化数组(或内存),然后操作结构数组(排序、列出、编辑等),然后将其全部转储回文件吗?

4

3 回答 3

0

读取文件:

std::ifstream file("file.txt");
std::string line;
while(std::getline(file, line)) {
    std::cout << line << std::endl;
}

因此,当您从文件中有一行时,您可以将其拆分并按照您想要的方式解析它。也许将字符串(行)放入字符串流并解析整数:

std::stringstream ss(line);
ss >> num1 >> num2 >> num3;

写入文件:

int num1 = 1;
int num2 = 2;
int num3 = 3;
std::ofstream file("file.txt");
file << num1 << " " << num2 << " " << num3 << std::endl;
于 2012-11-07T05:10:34.140 回答
0

如果你的文件被格式化了怎么办?根据格式,您将不得不以不同的方式阅读它。例如,如果您有一个格式如下的文件

John 18
Kelly 19

你可以有一个大致像这样的代码来阅读它

struct Person{
  string name;
  int age;
}

//create a read operator for Person class
ostream& operator>>(ostream& stream, Person& p){
   return stream >> p.name >> p.age;
}

bool mySortFunc(const Person& p1, const Person& p2){
  return p1.name < p2.name; //sort by name
}

int main(){
  ofstream file('names.txt');
  std::vector<Person> people;
  Person aPerson;
  while( file >> aPerson ){ 
    people.push_back(aPerson);
  }

 //now you have a list of person
 std::sort(people.begin(),people.end(), mySortFunc);

 //do more stuff 
}

以上内容均未编译,只是为了给您一个想法。希望能帮助到你..

于 2012-11-07T05:33:11.623 回答
0
    // FileManipulation.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "FileInterface.hpp"
class FileInterface;
int _tmain(int argc, _TCHAR* argv[])
{
    string         inputfile("D:\\input.txt"),outputfile("D:\\output.txt"),emp_name("sukumar"),emp_dept("seclore");
int emp_id(100),n=6;
ifstream infile1(inputfile.c_str());
double emp_sal(9342342.343);
try
{
    FileInterface fintface(inputfile,outputfile);
    if(!infile1.good())
    {cout<<"file does not exist"<<endl;
        while(n--)
        {
            cout<<"enter name"<<endl;fflush(stdin);
            getline(cin,emp_name);
            cout<<"enter dept"<<endl;fflush(stdin);
            getline(cin,emp_dept);
            cout<<"enter emp_id"<<endl;fflush(stdin);
            cin>>emp_id;
            cout<<"enter emp_sal"<<endl;fflush(stdin);
            cin>>emp_sal;
            fintface.write(emp_name,emp_dept,emp_id,emp_sal);
        }
    }
    else
    {
        cout<<"file closed"<<endl;
    }
    fintface.empid_grt(102);
    fintface.empid_less(102);
    fintface.salary_grt(double(2000));
    fintface.salary_less(double(2000));
    fintface.emp_dep("seclore");
    fintface.print_emp("sukumar");
    fintface.print_emp(104);
}
catch(char * s)
{
    cout<<s<<endl;
}
fflush(stdin);
cin.get();
return 0;

}

#ifndef FILE_INTERFACE_HPP
#define FILE_INTERFACE_HPP
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
class FileInterface
{
string inputf;
string outputf;
ifstream infile;
ofstream outfile;
bool empid_cmpare(int emp_id,bool greater);
bool salary_cmp(double salary,bool greater);
public:
FileInterface(string inputfile,string outputfile);
~FileInterface();
bool write(string emp_name,string dept,int emp_id,double salary);
bool empid_grt(int emp_id) ;
bool empid_less(int emp_id);
bool salary_grt(double salary);
bool salary_less(double salary);
bool emp_dep(string dept);
bool print_emp(int emp_id);
bool print_emp(string emp_name);
};
#endif //FILE_INTERFACE_HPP

#include "FileInterface.hpp"
#include <windows.h>
FileInterface::FileInterface(string inputfile,string outputfile)
{
inputf=inputfile;
outputf=outputfile;
infile.open(inputf.c_str(),ifstream::in|ifstream::out|ifstream::app);
if(infile.good())
{
    infile.close();
}
outfile.open(outputf.c_str(),ofstream::in|ofstream::out|ofstream::app);
if(outfile.good())
{
    outfile.close();
    if(remove((char*)outputf.c_str())!=0)
    cout<<"error in deleting output file"<<endl;
}
}
FileInterface::~FileInterface()
{
}
bool FileInterface::write(string emp_name,string dept,int emp_id,double salary)
{
outfile.open(inputf.c_str(),ofstream::in|ofstream::out|ofstream::app);
if(outfile.fail())
{
    cout<<"Error opening FILE in write"<<endl;
    throw "Error in file";
}
else
{
    outfile<<fixed<<showpoint;
    outfile<<left;
    outfile<<setw(20)<<emp_name;
    outfile<<right;
    outfile<<setw(20)<<dept;
    outfile<<setw(10)<<emp_id;
    outfile<<setw(20)<<setprecision(2)<<salary<<endl;
    outfile.close();
}
return true;
}
bool FileInterface::empid_cmpare(int emp_id,bool greater)
{
string emp_name,emp_dept;
int id;
double sal;
fstream infile;
if(infile.is_open())
    infile.close();
infile.open(inputf.c_str(),ifstream::in);
if(infile.fail())
{
    cout<<"Error opening inFILE in empid_cmpare"<<endl;
    throw "Error in file";
}
outfile.open(outputf.c_str(),ofstream::in|ofstream::out|ofstream::app);
if(outfile.fail())
{
    cout<<"Error opening outFILE in empid_cmpare"<<endl;
    throw "Error in file";
}
outfile<<fixed<<showpoint;
while(!infile.eof())
{
    infile>>emp_name;
    infile>>emp_dept;
    infile>>id;
    infile>>sal;
    if(greater&&(id>=emp_id))
    {
        outfile<<left;
        outfile<<setw(20)<<emp_name;
        outfile<<right;
        outfile<<setw(20)<<emp_dept;
        outfile<<setw(10)<<id;
        outfile<<setw(20)<<sal<<endl;
    }
    else if(!greater&&id<emp_id)
    {
        outfile<<left;
        outfile<<setw(20)<<emp_name;
        outfile<<right;
        outfile<<setw(20)<<emp_dept;
        outfile<<setw(10)<<id;
        outfile<<setw(20)<<sal<<endl;
    }
}
infile.close();
if(infile.is_open())
    cout<<"file still open"<<endl;
outfile.close();
if(outfile.is_open())
    cout<<"file still open"<<endl;
return true;
}
bool FileInterface::empid_grt(int emp_id) 
{
return empid_cmpare(emp_id,true);
}
bool FileInterface::empid_less(int emp_id)
{
return empid_cmpare(emp_id,false);
}
bool FileInterface::salary_cmp(double salary,bool greater)
{
string emp_name,emp_dept;
int id;
double sal;
fstream infile;
infile.open(inputf.c_str(),ifstream::in);
if(infile.fail())
{
    cout<<"Error opening inFILE in salary_cmp"<<endl;
    throw "Error in file";
}
outfile.open(outputf.c_str(),ofstream::in|ofstream::out|ofstream::app);
if(outfile.fail())
{
    cout<<"Error opening outFILE in salary_cmp"<<endl;
    throw "Error in file";
}
outfile<<fixed<<showpoint;
while(!infile.eof())
{
    infile>>emp_name;
    infile>>emp_dept;
    infile>>id;
    infile>>sal;
    if(greater&&(sal>=salary))
    {
        outfile<<left;
        outfile<<setw(20)<<emp_name;
        outfile<<right;
        outfile<<setw(20)<<emp_dept;
        outfile<<setw(10)<<id;
        outfile<<setw(20)<<sal<<endl;
    }
    else if(sal<salary)
    {
        outfile<<left;
        outfile<<setw(20)<<emp_name;
        outfile<<right;
        outfile<<setw(20)<<emp_dept;
        outfile<<setw(10)<<id;
        outfile<<setw(20)<<sal<<endl;
    }
}
infile.close();
outfile.close();
return true;
}

bool FileInterface::salary_grt(double salary)
{
return salary_cmp(salary,true);
}
bool FileInterface::salary_less(double salary)
{
return salary_cmp(salary,false);
}
bool FileInterface::emp_dep(string dept)
{
string emp_name,emp_dept;
int id;
double sal;
fstream infile;
infile.open(inputf.c_str(),ifstream::in);
if(infile.fail())
{
    cout<<"Error opening inFILE in emp_dep"<<endl;
    throw "Error in file";
}
outfile.open(outputf.c_str(),ofstream::in|ofstream::out|ofstream::app);
if(outfile.fail())
{
    cout<<"Error opening outFILE in emp_dep"<<endl;
    throw "Error in file";
}
outfile<<fixed<<showpoint;
while(!infile.eof())
{
    infile>>emp_name;
    infile>>emp_dept;
    infile>>id;
    infile>>sal;
    if(emp_dept.compare(dept)==0)
    {
        outfile<<left;
        outfile<<setw(20)<<emp_name;
        outfile<<right;
        outfile<<setw(20)<<emp_dept;
        outfile<<setw(10)<<id;
        outfile<<setw(20)<<sal<<endl;
    }
}
infile.close();
outfile.close();
return true;
}
bool FileInterface::print_emp(int emp_id)
{
string emp_name,emp_dept;
int id;
double sal;
fstream infile;
infile.open(inputf.c_str(),ifstream::in);
if(infile.fail())
{
    cout<<"Error opening inFILE in print_emp"<<endl;
    throw "Error in file";
}
while(!infile.eof())
{
    infile>>emp_name;
    infile>>emp_dept;
    infile>>id;
    infile>>sal;
    if(id==emp_id)
    {
        cout<<"EMP_NAME : "<<emp_name<<endl;
        cout<<"EMP_DEPT : "<<emp_dept<<endl;
        cout<<"ID : "<<id<<endl;
        cout<<"SAL : "<<sal<<endl;
        break;
    }
}
infile.close();
return true;
}
bool FileInterface::print_emp(string name)
{
string emp_name,emp_dept;
int id;
double sal;
fstream infile;
infile.open(inputf.c_str(),ifstream::in);
if(infile.fail())
{
    cout<<"Error opening inFILE in print_emp1"<<endl;
    throw "Error in file";
}
while(!infile.eof())
{
    infile>>emp_name;
    infile>>emp_dept;
    infile>>id;
    infile>>sal;
    if(emp_name.compare(name)==0)
    {
        cout<<"EMP_NAME : "<<emp_name<<endl;
        cout<<"EMP_DEPT : "<<emp_dept<<endl;
        cout<<"ID : "<<id<<endl;
        cout<<"SAL : "<<sal<<endl;
    }
}
infile.close();
return true;
}
于 2012-11-07T07:38:41.520 回答