Clean Special Characters from a TXT file

How to clean NUL CR FF characters from a TXT file

Page 1 of 1

2 Replies - 3790 Views - Last Post: 09 December 2010 - 05:41 AM Rate Topic: -----

#1 rumbitas  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 07-December 10

Clean Special Characters from a TXT file

Posted 07 December 2010 - 03:29 AM

Hello, I am developing a program that collects information from a text file, treat it and save in another text file. The problem is that the original file has special characters "NUL" "STX" "FF" "CR" "LF" and do not know how to remove or omit.

An example would be:

Posted Image

My source code is:

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

using namespace std;

void saveFile(string line, char * name)
{
     ofstream fs;
     
     fs.open(name, ios::app);
     
     for(int i=0; i<99; i++){
         if(i >= 0 && i < 13)
            fs << line[i];
         else if(i >= 13 && i < 18)
            fs << "-";
         else if(i >= 18 && i < 99)
            fs << line[i];
     }
     fs << endl;
     
     fs.close();
}


int main(int argc, char *argv[])
{
   system("cls");
   
   cout << endl;
   cout << "===================================================================" << endl;
   cout << "             Tratamiento de ficheros de cobro ver. 1.0             " << endl;
   cout << "===================================================================" << endl;
   /*   
   if(argc!=2) {
      cout << "La expresion correcta seria: ficob <fichero_entrada> <fichero_salida>" <<endl;
      return 1;
   }*/
   
   //ifstream infile(argv[1]);
   ifstream infile("TEST-COBROS.TXT");
   
   if (!infile)
   {
      cerr << "No se puede abrir el ." << endl;

      return 1;
   }

   cout << "Tratando el fichero" << argv[1] << "..." << endl;

   vector<string> lines;
   string line;

   while (!infile.eof())
   {
      //lines.push_back(line);
      getline(infile, line, '\n');
      cout << line << endl;
      saveFile(line, "TEST.TXT");  
      
   }

   infile.close();

   return 0;
}






Thanks for your help (I'm a newbie in the C++ Programming).

See u soon.

Is This A Good Question/Topic? 0
  • +

Replies To: Clean Special Characters from a TXT file

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Clean Special Characters from a TXT file

Posted 07 December 2010 - 09:01 AM

Well after the call to getline() you know the length of the string read from the file. Use a for() loop, you can examine each character in the string to see if it's
what you want. Using iscntrl or isprint you decide whether or not to copy the character to a new string using the string class's append method.
Was This Post Helpful? 1
  • +
  • -

#3 rumbitas  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 98
  • Joined: 07-December 10

Re: Clean Special Characters from a TXT file

Posted 09 December 2010 - 05:41 AM

Thank you n8wxs. I've seen your message today, however, I got a solution. Although I would like to correct faults, to improve the program or make it more efficient. If anyone has time or is boring, any comments are welcome.

Thank you very much from Spain. See you soon.

P. S. I attached the source code. Sorry for the comments and captions of the source code, I do it in spanish/english. It's the first version.

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <time.h>


using namespace std;

void removeChar(string s, char x)
{
    int i, j;
    
    for(i=0; s[i] != 0; ++i)
    {
        while(s[i]==x) /* copy all chars, including NULL at end, over char to left */
        {
            j=i;
            while(s[j]!=0)
            {
                s[j]='-';
                ++j;
            }
        }
    }   
}

int countLines(char * filename)
{
    ifstream infile;

    infile.open(filename);

    if ( !infile )
       return 0;
    
    int size = 0;
    string line;

    while ( !infile.eof() )
    {
        getline(infile, line);
        size++;
    }
    
    infile.close();
    return size;
    
}

void duerme(int retraso)
{
   float t1 = clock(), t2;
   
   fflush(stdout);
   
   do
   {
      t2 = clock();
      
   }while(((t2 - t1) / CLK_TCK) < retraso);
}  

void saveFile(string line, char * name)
{
     ofstream fs;
     
     fs.open(name, ios::app);
     
     for(int i=0; i<99; i++){
         if(i >= 0 && i < 13)
             fs << line[i];
         else if(i >= 13 && i < 18)
             fs << '-';
         else if(i >= 18 && i < 99)
             fs << line[i];
     }
     
     fs << endl;
     
     fs.close();
}


int main(int argc, char *argv[])
{
   system("cls");
   
   int Lines, numLineas = 0, numArreglos = 0;
   
   cout << endl;
   cout << "__________________________________________________________________________" << endl << endl;
   cout << "                 Tratamiento de ficheros de cobro ver. 1.0             " << endl;
   cout << "__________________________________________________________________________" << endl;
      
   if(argc!=3) {
      cout << "\n\nLa expresion correcta seria: " << argv[0] << " <fichero_entrada> <fichero_salida>" <<endl;
      return 1;
   }
   
   ifstream infile(argv[1]);
   
   if (!infile)
   {
      cerr << "No se puede abrir el fichero " << argv[1] << endl;

      return 1;
   }

   cout << endl << "Tratando el fichero: " <<  argv[1] << endl << endl;
   cout << "Cargando";
   for(int i=0; i<3; i++){
   duerme(2);
   cout << ".";
   }
   

   vector<string> lines;
   string line, lineAux;

   Lines = countLines(argv[1]) - 1;
   
   while(numLineas < Lines)
   {
      //lines.push_back(line);
      getline(infile, line, '\n');
      if(line.length() < 99)
      {
          getline(infile, lineAux, '\n');
          line = line + ' ' + lineAux;
          Lines--;
      }
      
      for(int ascii=0; ascii<=32; ascii++)
          removeChar(line, char(ascii));
          
      //cout << line << endl << endl;
      
      saveFile(line, argv[2]);  
      line.clear();
      lineAux.clear();
      numLineas++;
   }
   cout << endl << "__________________________________________________________________________";
   cout << endl << endl << "Numero de Registros Procesados: " << Lines << endl;
   cout << endl << "__________________________________________________________________________";
   infile.close();

   return 0;
}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1