1

Basically, what I'm trying to do is convert a .txt file into a .csv file. The .txt file is separated by pipes '|', and I have to convert the pipes into commas and make some other exceptions for existing commas ',' and double quotes '"'.

However, the main problem I'm having comes in this form within the .txt file: |||

I'm supposed to turn that into: ,,,

But every time, strtok_s always turns those three pipes into one comma.

I've been trying to understand strtok_s() more in order to manipulate it for my purposes (found within the commented-out if-condition), but I haven't been having much luck.

Is there any way to do parsing differently that allows me to consider things more case-by-case or to implement the if-condition with strtok_s()?

Here is my code:

const int MAX_CHARS_PER_LINE = 150;
const int MAX_TOKENS_PER_LINE = 15;
const char* const DELIMITER = "|";

int main(){
// Need to comment code and split it up so it's not all in the main function

    // Basically just asking for file name stuffs
    cout << "Enter an existing input file name: ";
    string fileName;
    getline(cin, fileName);
    fileName = fileName + ".txt";

    cout << "Enter an output file name: ";
    string output;
    getline(cin, output);
    output = output + ".csv";

    ifstream infile;
    ofstream outfile;
    infile.open(fileName);
    if (!infile){
        cout << "File open failure!" << endl;
        system("pause");
        return 0;
    }

    outfile.open(output);

    //THE MEAT OF THE CODE
    while (!infile.eof()){
        char buffer[MAX_CHARS_PER_LINE];
        infile.getline(buffer, MAX_CHARS_PER_LINE);

        int counter = 0;
        const char* token[MAX_TOKENS_PER_LINE];
        char * next_token;

        token[0] = strtok_s(buffer, DELIMITER, &next_token);
        if (token[0]){
            for (counter = 1; counter < MAX_TOKENS_PER_LINE; counter++){
                //if (){                <----- Wtf do I put in the if-condition?
                    //token[counter] = ",";
                //}
                //else{
                    token[counter] = strtok_s(NULL, DELIMITER, &next_token);
                //}
                if (!token[counter]){ // no more tokens put into the array
                    break;
                }
            }
        }

        //Basically just ignore all this stuff, a lot of tedious exceptions
        for (int i = 0; i < counter; i++){
            string temp = token[i];
            string ultimate = "\"";
            if (temp.find('"') != string::npos){
                while (temp.find('"') != string::npos){
                    ultimate += temp.substr(0, temp.find('"'));
                    ultimate += "\"\"";
                    temp = temp.substr(temp.find('"') + 1);
                }
                ultimate += temp;
                ultimate += "\"";
                outfile << ultimate << ',';
            }
            else if (temp.find(',') != string::npos){
                temp = "\"" + temp + "\"";
                outfile << temp << ',';
            }
            else if (temp.compare(",") == 0){
                outfile << temp;
            }
            else{
            outfile << token[i] << ',';
            }
        }
        outfile << endl;
    }


    infile.close();
    outfile.close();
    system("pause");
    return 0;
}
David W
  • 11
  • 2
  • 2
    `while (!infile.eof())` [Nooooooooooooooooooo](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user657267 Oct 29 '14 at 02:51
  • Don't use `strtok_s`. Use the [C++ library algorithm functions](http://en.cppreference.com/w/cpp/algorithm). – 0x499602D2 Nov 01 '14 at 23:35

0 Answers0