You are here:

C++/sequential file



I've written a sequential file that can create a file and adding data to it,i'm trying to write a member function that sort records of this file and according the sequential file i mentioned just one key for records and its :"ID"
could you give me a suggestion about sorting sequential file?
i know how to sort an array of data but now the datas are in a file
i think the solution is to put all the records to an array from the file that i have created and then sorting them according to the key,"ID", but i'm not sure about this idea and want your help for sorting records.

here is the code for just adding records to file:

using std::cout;
using std::cin;
using std::endl;
using std::ios;
using std::cerr;

using std::string;

using std::ofstream;

using std::exit;

class employment
  void ADD();
  int ID;
  char Fname[10];
  char Lname[10];
  char City[10];
  double Salary;

void employment::ADD()
  ofstream employee("c:\\tests.dat",ios::app);

     cerr<<"File could not be opened" <<endl;
  cout<<"Enter the ID,Fname,Lname,City and Salary."<<endl
     <<"Enter end-of-file to end input.\n?";

     employee<<ID<<' '<<Fname<<' '<<Lname<<' '<<City<<' '<<Salary<<endl;

int main()
  employment obj;


  return 0;


ANSWER: > i think the solution is to put all the records to an array from the file that i have created
> and then sorting them according to the key,"ID"

Yes, that is the basic idea.
You could use a std::vector instead of an array.
Since ID is the first few characters in every file, if you write out the id with a fixed width, just sorting the lines is sufficient.

#include <iomanip>

// assuming the record was written this way,

     employee << std::setw(10) <<ID<<' '<<Fname<<' '<<Lname<<' '<<City<<' '<<Salary<<endl;

You could sort this way:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>

// sort file on ID where each line in the file has ID as the first field
void sort_file( const char* path )
   std::vector< std::string > lines ;

   // read each line into the vector
       std::ifstream fin(path) ;
       std::string line ;
       while( std::getline( fin, line ) ) lines.push_back( line ) ;

   // sort the vector
   std::sort( lines.begin(), lines.end() ) ;

   // overwrite the file by writing each line in the vector to the file
       std::ofstream fout(path) ;
       for( std::size_t i = 0 ; i < lines.size() ; ++i )
         fout << lines[i] << '\n' ;

---------- FOLLOW-UP ----------

QUESTION: thanx alot for answearing

but i have a question about the code you've been given
could you plz tell what exactly does the 'path' argument do?
and my second question is that how can i write the sort_file function in 'main'?
i've written it like below in 'main':

employee obj;//employee is the class's name


but as i mentioned i don't know what i should write as an argument in sort_file function

and could you plz describe a bit about 'line' and 'fin'?


> what exactly does the 'path' argument do?
'path' is the pathname of the file eg. "/usr/home/auser/data/employee.txt"

> how can i write the sort_file function in 'main'?
You cannot write a function inside another function. You could however write it as a member of the employee class

class employee
    // ...
    void sort_file( const char* path ) ;
    // ...
in which case your code in main would be the way to call it.

> a bit about 'line' and 'fin'?
These are just identifiers (names of variables). 'line' is a string which holds a line read from the file. And 'fin' is the input filestream variable. you could use any valid C++ identifier - for example 'str' and 'in_file' - in place of 'line' and 'fin'.  


All Answers

Answers by Expert:

Ask Experts




my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.


about 15 years or so

post graduate engineer

©2016 All rights reserved.