C++/File/File Utility

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Concatenate files

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>
  3. include <string>

using std::cerr; using std::cout; using std::endl; using std::getline; using std::ifstream; using std::string; int main(int argc, char **argv) {

 int fail_count = 0;
 for (int i = 1; i < argc; ++i) {
   ifstream in(argv[i]);
   if (in) {
     string s;
     while (getline(in, s))
       cout << s << endl;
   } else {
     cerr << "cannot open file " << argv[i] << endl;
     ++fail_count;
   }
 }
 return fail_count;

}


 </source>


Copy a file and display number of chars copied.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>

using namespace std; int main(int argc, char *argv[]) {

 if(argc!=3) {
   cout << "Usage: CPY <input> <output>\n";
   return 1;
 }
 ifstream fin(argv[1], ios::in | ios::binary); // open input file
 ofstream fout(argv[2], ios::out | ios::binary); // create output file
 if(!fin) {
   cout << "Cannot open input file\n";
   return 1;
 }
 if(!fout) {
   cout << "Cannot open output file\n";
   return 1;
 }
 char ch;
 unsigned count=0;
 while(!fin.eof()) {
   fin.get(ch);
   if(!fin.eof()) {
     fout.put(ch);
     count++;
   }
 }
 cout << "Number of bytes copied: " << count << "\n";
 fin.close();
 fout.close();
 return 0;

}


 </source>


Copy a file and reverse case of letters.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>
  3. include <cctype>

using namespace std; int main(int argc, char *argv[]) {

 char ch;
 if(argc!=3) {
   cout << "Usage: COPYREV <source> <target>\n";
   return 1;
 }
 ifstream in(argv[1]);
 if(!in) {
   cout << "Cannot open input file.\n";
   return 1;
 }
 ofstream out(argv[2]);
 if(!out) {
   cout << "Cannot open output file.\n";
   return 1;
 }
 while(!in.eof()) {
   ch = in.get();
   if(!in.eof()) {
     if(islower(ch)) ch = toupper(ch);
     else ch = tolower(ch);
     out.put(ch);
   }
 };
 in.close();
 out.close();
 return 0;

}


 </source>


Copy a file and reverse case of letters with error checking.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>
  3. include <cctype>

using namespace std; int main(int argc, char *argv[]) {

 char ch;
 if(argc!=3) {
   cout << "Usage: COPYREV <source> <target>\n";
   return 1;
 }
 ifstream in(argv[1]);
 if(!in) {
   cout << "Cannot open input file.\n";
   return 1;
 }
 ofstream out(argv[2]);
 if(!out) {
   cout << "Cannot open output file";
   return 1;
 }
 while(!in.eof()) {
   ch = in.get();
   if(!in.good() && !in.eof()) 
     return 1;
   if(!in.eof()) {
     if(islower(ch)) 
        ch = toupper(ch);
     else 
        ch = tolower(ch);
     out.put(ch);
     if(!out.good()) 
        return 1;
   }
 };
 in.close();
 out.close();
 if(!in.good() && !out.good()) 
    return 1;
 return 0;

}


 </source>


Copy and convert tabs to spaces.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>

using namespace std; int main(int argc, char *argv[]) {

 if(argc!=3) {
   cout << "Usage: CPY <in> <out>\n";
   return 1;
 }
 ifstream in(argv[1]);
 if(!in) {
   cout << "Cannot open input file.\n";
   return 1;
 }
 ofstream out(argv[2]);
 if(!out) {
   cout << "Cannot open output file.\n";
   return 1;
 }
 char ch;
 int i = 8;
   
 while(!in.eof()) {
   in.get(ch); 
   if(ch=="\t") 
      for( ; i>0; i--) 
         out.put(" ");
   else out.put(ch);
   if(i == -1 || ch=="\n") i = 8;
   i--;
 }
 in.close();
 out.close();
 return 0;

}


 </source>


Copy a text file and display number of chars copied.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>

using namespace std; int main(int argc, char *argv[]) {

 if(argc!=3) {
   cout << "Usage: Copy <input> <output>\n";
   return 1;
 }
 ifstream fin(argv[1]); // open input file
 ofstream fout(argv[2]);  // create output file
 if(!fin) {
   cout << "Cannot open input file.\n";
   return 1;
 }
 if(!fout) {
   cout << "Cannot open output file.\n";
   return 1;
 }
 char ch;
 unsigned count=0;
 fin.unsetf(ios::skipws);  // do not skip spaces
 while(!fin.eof()) {
   fin >> ch;
   if(!fin.eof()) {
     fout << ch;
     count++;
   }
 }
 cout << "Number of bytes copied: " << count << "\n";
 fin.close();
 fout.close();
 return 0;

}


 </source>


Copy files

<source lang="cpp">

  1. include <iostream>
  2. include <stdlib.h>
  3. include <fstream>

using namespace std; int main(int argc, char **argv)

{
  char buffer[1];
  
  ifstream input(argv[1], ios::in);
  if (input.fail())
   {
     cout << "Error opening the file " << argv[1];
     exit(1);
   }
  ofstream output(argv[2], ios::out);
  if (output.fail())
   {
     cout << "Error opening the file " << argv[2];
     exit(1);
   }


  do {
    input.read(buffer, sizeof(buffer));
    if (input.good())
      output.write(buffer, sizeof(buffer));
  } while (! input.eof());
  input.close();
  output.close(); 

}


 </source>


Count letters.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>
  3. include <cctype>

using namespace std; int alpha[26]; int main(int argc, char *argv[]) {

 char ch;
 if(argc!=2) {
   cout << "Usage: COUNT <source>\n";
   return 1;
 }
 ifstream in(argv[1]);
 if(!in) {
   cout << "Cannot open input file.\n";
   return 1;
 }
 // init alpha[]
 int i;
 for(i = 0; i <26; i++) alpha[ i ] = 0;
 while(!in.eof()) {
   ch = in.get();
   if(isalpha(ch)) { 
     ch = toupper(ch); 
     alpha[ch-"A"]++;
   }
 };
 for(i = 0; i <26; i++) {
   cout << (char) ("A"+ i) << ": " << alpha[ i ] << "\n";
 }
 in.close();
 return 0;

}


 </source>


Create a file comparision utility.

<source lang="cpp">


  1. include <iostream>
  2. include <fstream>

using namespace std;

int main(int argc, char *argv[]) {

 register int i;  
 int numread;  
 
 unsigned char buffer1[1024], buffer2[1024];  
 
 if(argc!=3) {  
   cout << "Usage: compfiles <file1> <file2>\n";  
   return 1;  
 }  
 
 ifstream f1(argv[1], ios::in | ios::binary);  
 if(!f1) {  
   cout << "Cannot open first file.\n";  
   return 1;  
 }  
 ifstream f2(argv[2], ios::in | ios::binary);  
 if(!f2) {  
   cout << "Cannot open second file.\n";  
   return 1;  
  }  
 
 cout << "Comparing files...\n";  
 
 do {  
   f1.read((char *) buffer1, sizeof buffer1);  
   f2.read((char *) buffer2, sizeof buffer2);  

   if(f1.gcount() != f2.gcount()) { 
     cout << "Files are of differing sizes.\n"; 
     f1.close();  
     f2.close();  
     return 0; 
   } 
   
   for(i = 0; i <f1.gcount(); i++)  // compare contents of buffers  
     if(buffer1[i] != buffer2[i]) {  
       cout << "Files differ.\n";  
       f1.close();  
       f2.close();  
       return 0;  
     }  
 
 } while(!f1.eof() && !f2.eof());  
 
 cout << "Files are the same.\n";  
 
 f1.close();  
 f2.close();  
 
 return 0;  

}


 </source>


Display contents of specified file in both ASCII and in hex.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>
  3. include <cctype>
  4. include <iomanip>

using namespace std; int main(int argc, char *argv[]) {

 if(argc != 2) {
   cout << "Usage: Display <filename>\n";
   return 1;
 }
 ifstream in(argv[ 1 ], ios::in | ios::binary);
 if(!in) {
   cout << "Cannot open input file.\n";
   return 1;
 }
 register int i, j;
 int count = 0;
 char c[16];
 cout.setf(ios::uppercase);
 while(!in.eof()) {
   for(i = 0; i < 16 && !in.eof(); i++) {
     in.get(c[i]);
   }
   if(i <16) 
      i--;                                 // get rid of eof
   for(j = 0; j < i; j++)
     cout << setw(3) << hex << (int) c[j];
   for(; j < 16; j++) 
     cout << "   ";
   cout << "\t";
   for(j = 0; j < i; j++)
     if(isprint(c[ j ])) 
        cout << c[ j ];
     else 
        cout << ".";
   cout << endl;
   count++;
   if(count == 16) {
     count = 0;
     cout << "Press ENTER to continue: ";
     cin.get();
     cout << endl;
   }
 }
 in.close();
 return 0;

}


 </source>


Read and display a text file line by line.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>

using namespace std; int main(int argc, char *argv[]) {

 if(argc != 2) {
   cout << "Usage: Display <filename>\n";
   return 1;
 }
 ifstream in(argv[ 1 ]);                 // input
 if(!in) {
   cout << "Cannot open input file.\n";
   return 1;
 }
 char str[ 255 ];
 while(in) {
   in.getline(str, 255);                // delim defaults to "\n"
   if(in) 
      cout << str << endl;
 }
 in.close();
 return 0;

}


 </source>


Search file.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>
  3. include <cstring>

using namespace std; int main(int argc, char *argv[]) {

 if(argc!=3) {
   cout << "Usage: SEARCH <file> <word>\n";
   return 1;
 }
 ifstream in(argv[1]);
 if(!in) {
   cout << "Cannot open input file.\n";
   return 1;
 }
 char str[255];
 int count=0;
 while(!in.eof()) {
   in >> str;
   if(!strcmp(str, argv[2])) count++;    
 }
 cout << argv[2] << " found " << count;
 cout << " number of times.\n";
 in.close();
 return 0;

}


 </source>


Swap characters in a file.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>

using namespace std; int main(int argc, char *argv[]) {

 if(argc!=2) {
   cout << "usage: SWAP <filename>\n";
   return 1;
 }
 // open file for input/output
 fstream io(argv[1], ios::in | ios::out | ios::binary);
 if(!io) {
   cout << "Cannot open file.\n";
   return 1;
 }
 char ch1, ch2;
 long i;
 for(i = 0 ; !io.eof(); i+=2) {
   io.seekg(i, ios::beg);
   io.get(ch1);
   if(io.eof()) continue;
   io.get(ch2);
   if(io.eof()) continue;
   io.seekg(i, ios::beg);
   io.put(ch2);
   io.put(ch1);
 }
 io.close();
 return 0;

}


 </source>


Swap characters in a file with error checking.

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>

using namespace std; int main(int argc, char *argv[]) {

 if(argc!=2) {
   cout << "usage: SWAP <filename>\n";
   return 1;
 }
 // open file for input/output
 fstream io(argv[1], ios::in | ios::out | ios::binary);
 if(!io) {
   cout << "Cannot open file.\n";
   return 1;
 }
 char ch1, ch2;
 long i;
 for(i = 0 ;!io.eof(); i+=2) {
   io.seekg(i, ios::beg);
   if(!io.good()) 
      return 1;
   io.get(ch1);
   if(io.eof()) 
      continue;
   io.get(ch2);
   if(!io.good()) 
      return 1;
   if(io.eof()) 
      continue;
   io.seekg(i, ios::beg);
   if(!io.good()) 
      return 1;
   io.put(ch2);
   if(!io.good()) 
      return 1;
   io.put(ch1);
   if(!io.good()) 
      return 1;
 }
 io.close();
 if(!io.good()) return 1;
 return 0;

}


 </source>


Word count for input file: file read with ifstream

<source lang="cpp">

  1. include <iostream>
  2. include <fstream>
  3. include <cctype>

using namespace std; int main(int argc, char *argv[]) {

 if(argc!=2) {
   cout << "Usage: COUNT <input>\n";
   return 1;
 }
 ifstream in(argv[1]);
 if(!in) {
   cout << "Cannot open input file.\n";
   return 1;
 }
 int count=0;
 char ch;
 in >> ch; // find first non-space char
 // after first non-space found, do not skip spaces
 in.unsetf(ios::skipws);
 while(!in.eof()) {
   in >> ch;
   if(isspace(ch)) {
     count++;
     while(isspace(ch) && !in.eof()) 
        in >> ch; // find next word
   }
 }
 cout << "Word count: " << count << "\n";
 in.close();
 return 0;

}


 </source>