簡體   English   中英

主要使用fstream和數組讀取,操作和寫入C ++文檔

[英]Reading, manipulating, and writing to C++ document using mainly fstream and arrays

是否有關於閱讀,操縱編寫C ++文件流的易於理解和客觀的指南? 此時任何事情都會有所幫助。

我是否可以使用一種簡單,直接的模型來讀取/寫入/處理由字符串,整數,雙精度字符,字符和布爾組成的txt文檔中的項目列表?

我可以加載文件,將所有內容讀取並轉儲到結構化數組(或內存),然后操縱結構化數組(進行排序,列出,編輯等),然后將其全部轉儲回文件嗎?

讀取文件:

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;

如果您的文件格式化了怎么辦? 根據格式,您將不得不不同地閱讀它。 例如,如果您的文件格式如下

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 
}

上面沒有一個被編譯,它只是給您一個想法。 希望能幫助到你..

    // 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM