Solved

remove unwanted characters and then write to a file

Posted on 2004-10-26
214 Views
Last Modified: 2010-04-01
Hi, experts,
I have a file, it contains '=', ' " " ' in each field. I want to remove those unwanted characters, and then write to a new file.
Here is my code, but it seems not working. So please help.


void removeChar(char* src, char delim ) {
      int i=0,j=0;
      char buf[MaxSize];
      while (src[i] != '\0' ) {
            if (src[i] == delim){
                  i++;
                  continue;
            }
            buf[j++]=src[i];
            i++;
      }
      buf[j] = '\0';
      strcpy(src,buf);
};

char* MyStrTok(char* text, const char del)
{
    static char *curTok = NULL;
    char *result = NULL;
    if ( text != NULL )
    {
        curTok = text;
    }
    result = curTok;
    if ( curTok != NULL )
    {
        //curTok = ::strchr(curTok, del);
        curTok = strchr(curTok, del);

        if ( curTok != NULL ) *curTok++ = 0;
    }
    return result;
};


main(int arc, char *arv[])
{

//      cout << "Begin" << endl;

      ifstream fin("test.csv");
      ofstream fout("fout.csv");
      char row_read[512];
      char row_elem[50][50];

      int i=0;

while( fin.getline( row_read, sizeof( row_read ) ) )
{
      //////////////////////////////////////////
      // skip first line
      //////////////////////////////////////////
/*      if( isFirstLine == 0)
      {
                  //isFirstLine = false;
                  isFirstLine = 1;
                  continue ;
      }
*/
      //////////////////////////////////////////
      // get tokens, parse string with ","
      //////////////////////////////////////////
      char *token = MyStrTok( row_read, ',' );

      while( token != NULL )
      {
            ///////////////////////////////////////////////////////////
            // strim '"', '=' etc.. in the fields
            //////////////////////////////////////////////////////////
             //for ( i = 0; i < row_elem.size(); i++ )
            char intrBuf[MaxSize];
                                char elem[100];
             strcpy(intrBuf,token);
             removeChar(intrBuf,'=');      
             removeChar(intrBuf,'"');
            strcpy(row_elem[i], intrBuf );
                                sprintf(elem, row_elem[i])
            i++;
            
            fout << elem << endl;
                  }
}
0
Question by:justinY
    15 Comments
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    I don't see where in your main while statement is called MyStrTok again.

    while (token!=NULL)     produces an infinite loop since token doesn't change anymore.
    0
     

    Author Comment

    by:justinY
    You are absolutely right.  whats the fix ?
    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    Here is a corrected version of your main() function. If you want the commas in your output file, you have to change

         fout << elem;

    by
         fout << elem << ',' ;

    However, you wouldn't need any tokenizing in that case.

    Regards, Alex

    main(int arc, char *arv[])
    {
       
        //     cout << "Begin" << endl;
       
        ifstream fin("test.csv");
        ofstream fout("fout.csv");
        char row_read[512];
        char row_elem[50][50];
       
        int i=0;
       
        while( fin.getline( row_read, sizeof( row_read ) ) )
        {
            //////////////////////////////////////////
            // skip first line
            //////////////////////////////////////////
            /*     if( isFirstLine == 0)
            {
            //isFirstLine = false;
            isFirstLine = 1;
            continue ;
            }
            */
            //////////////////////////////////////////
            // get tokens, parse string with ","
            //////////////////////////////////////////
            char *token = MyStrTok( row_read, ',' );
           
            while( token != NULL )
            {
                ///////////////////////////////////////////////////////////
                // strim '"', '=' etc.. in the fields
                //////////////////////////////////////////////////////////
                //for ( i = 0; i < row_elem.size(); i++ )
                char intrBuf[MaxSize];
                char elem[100];
                strcpy(intrBuf,token);
                removeChar(intrBuf,'=');    
                removeChar(intrBuf,'"');
                strcpy(row_elem[i], intrBuf );
                sprintf(elem, row_elem[i]);
                i++;
               
                fout << elem;
                token = MyStrTok( NULL, ',' );
            }
            fout << endl;
        }
        return 0;
    }

    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    I think you need:
        token = MyStrTok(NULL, ',' );

    after this line:
         fout << elem << endl;
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Sorry, simultaneous writting
    0
     
    LVL 3

    Expert Comment

    by:HendrikTYR
    Here is a nice little char remover that does not require to create a temporary buffer:

    void removeChar(char* src, char delim ) {
         char *dest = src;
         while(*src) {
             if(*src == delim) src++;
             else *dest++ = *src++;
         }
         *dest = 0;
    }
    0
     
    LVL 55

    Expert Comment

    by:Jaime Olivares
    Since you have to remove 2 characters, you can design a double remover. Will be faster than use the single remover twice.

    void doubleRemoveChar(char* src, char delim1, char delim2 )
    {
         for (char *dest=src; *src; src++) {
             if(*src!=delim1 && *src!=delim2)
                 *dest++ = *src;
         }
         *dest = 0;
    }
    0
     

    Author Comment

    by:justinY
    Hi, Alex,

    I have run time error. and it only writes 3 lines to the output file
    0
     

    Author Comment

    by:justinY
    Alex, I got that fixed.
    I use this
    sprintf(elem, intrBuf);

    instead of
    strcpy(row_elem[i], intrBuf );
    sprintf(elem, row_elem[i]);

    Thanks guys.
    0
     

    Author Comment

    by:justinY
    one more question for you guys

    I add function at the front
    string GetField(string &aStr, int aFieldNum, char aDelim)
    {
        istringstream ss(aStr);
        string field;
        while (getline(ss, field, ',') && aFieldNum > 0 )
        {
            --aFieldNum;
        }
        return field;
    }

    and in my main, right after
    fout << endl;
    I want to open this oytput file and do something else, so I add
        ifstream fin11;
        ofstream fout11;
        fin11.open("C:\\test\\resource\\transactions2.csv");
        fout11.open("C:\\test\\resource\\fla.txt");
        string line;
        while ( getline(fin, line) )
        {
             // assuming that the 26th column is load amount column
            string fn = GetField(line, 25, ',');
            if ( atoi(fn.c_str()) != 0 )
            {
                fout << line.c_str() << endl;
            }
        }

    ....
    I dont have compiling error, but the final output file fla.txt has nothing in it. whats wrong ?

    Thanks
    0
     

    Author Comment

    by:justinY
    sorry should be
    fin11.open("fout.csv");
    fout11.open("fout11.csv)
    0
     
    LVL 39

    Accepted Solution

    by:
    >>>> I use this
    >>>> sprintf(elem, intrBuf);

    Yes, elem is a 100 char array what is better than 50 chars. What is MaxSize?

    I used that test.csv file below and none of my entries exceeds 50 char size.

    aaaaaaaa,"bbbbbb",cccccc,"dddddd","eeeeeeeeee","ffffffffff"
    "xxxx","yyyyyy","zzzz z","xyz"
    "","","jjjj","","",iii

    Generally,  i would recommend to make all buffers same size to avoid conflicts.

    >>>> the final output file fla.txt has nothing in it. whats wrong ?

    We need the contents of the input file to answer the question. I would guess that either string fn is empty after GetField(..) or atoi(fn.c_str()) gives 0 cause the string fn doesn't start with a digit.

    Regards, Alex



    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    >>>> fin11.open("fout.csv");

    I assume that's the output file of the prog above? If yes, did you write the commas as i suggested above? The GetField function will not work properly if the file has no commas.

    Regards, Alex
    0
     

    Author Comment

    by:justinY
    yes. I did and fout.csv file is normal. it has everything I expect. But fout11.csv has nothing.
    If I seperate them into 2 programs , it works well. I really dont understand why.
    0
     
    LVL 39

    Expert Comment

    by:itsmeandnobodyelse
    >>>> while ( getline(fin, line) )

    That line is wrong. You have to use fin11 instead of fin.

    Regards, Alex

       
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: MongoDB Object-Document Mapper for NodeJS

    NodeJS (JavaScript on the server) is awesome, but some developers get confused about NoSQL when it comes to working in Node with MongoDB (NoSQL database). Do you need a better explanation of how to use Node.js with MongoDB? The most popular choice is the Mongoose library.

    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now