[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

remove unwanted characters and then write to a file

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
justinY
Asked:
justinY
  • 6
  • 4
  • 4
  • +1
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
justinYAuthor Commented:
You are absolutely right.  whats the fix ?
0
 
itsmeandnobodyelseCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Jaime OlivaresSoftware ArchitectCommented:
I think you need:
    token = MyStrTok(NULL, ',' );

after this line:
     fout << elem << endl;
0
 
Jaime OlivaresSoftware ArchitectCommented:
Sorry, simultaneous writting
0
 
HendrikTYRCommented:
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
 
Jaime OlivaresSoftware ArchitectCommented:
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
 
justinYAuthor Commented:
Hi, Alex,

I have run time error. and it only writes 3 lines to the output file
0
 
justinYAuthor Commented:
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
 
justinYAuthor Commented:
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
 
justinYAuthor Commented:
sorry should be
fin11.open("fout.csv");
fout11.open("fout11.csv)
0
 
itsmeandnobodyelseCommented:
>>>> 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
 
itsmeandnobodyelseCommented:
>>>> 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
 
justinYAuthor Commented:
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
 
itsmeandnobodyelseCommented:
>>>> while ( getline(fin, line) )

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

Regards, Alex

   
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 6
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now