[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

remove unwanted characters and then write to a file

Posted on 2004-10-26
16
Medium Priority
?
222 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
Comment
Question by:justinY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
  • +1
16 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12412172
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
ID: 12412295
You are absolutely right.  whats the fix ?
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12412316
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 55

Expert Comment

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

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

Expert Comment

by:Jaime Olivares
ID: 12412361
Sorry, simultaneous writting
0
 
LVL 3

Expert Comment

by:HendrikTYR
ID: 12412362
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
ID: 12412448
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
ID: 12412583
Hi, Alex,

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

Author Comment

by:justinY
ID: 12412753
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
ID: 12413124
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
ID: 12413149
sorry should be
fin11.open("fout.csv");
fout11.open("fout11.csv)
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
ID: 12413548
>>>> 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
ID: 12413612
>>>> 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
ID: 12414097
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
ID: 12418716
>>>> while ( getline(fin, line) )

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

Regards, Alex

   
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

650 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