Click here to Skip to main content
14,668,908 members
Rate this:
Please Sign up or sign in to vote.
 need help trying to figure out how to use the conversionFile command in c++ program to read an input text file ("test.txt") line by line into an array of strings and then convert it from pipe delimited to comma delimited by writing the changed text into an output csv file. Also need to add additional quotes where they were given. I am stuck on where to start. I know how to do the reading into the array but the actual conversion of pipe to comma delimited, I am stuck on.

What I have tried:

The given "test.txt" is:
 Miles per hour|6,445|being the "second" team |5.54|9.98|6,555.00 
 "Ending" game| left at "beginning"|Elizabeth, New Jersey|25.25|6.78|987.01
 |End at night, or during the day|"Let's go"|65,978.21|0.00|123.45
 Left-base night|10/07/1900|||4.07|777.23 
"Let's start it"|Start Baseball Game|Starting the new game to win

The output csv file is expected to look like this when the pipe delimited is turned to commas and additional quotes are added: 
Miles per hour,"6,445","being the ""second"" team member",5.54,9.98,"6,555.00", """Ending"" game","left at ""beginning""","Denver, Colorado",25.25,6.78,987.01, ,"End at night, during the day","""Let's go""","65,978.21",0.00,123.45,
 Left-base night, 10/07/1900,,,4.07,777.23, 
"""Let's start it""", Start Baseball Game, Starting the new game to win,
Updated 7-Apr-20 4:32am

Rate this:
Please Sign up or sign in to vote.

Solution 1

There is no "conversionFile command in c++" - you will have to write your own, or find code to exactly do what you want. The latter is unlikely, the former is pretty simple.

But ... while we are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
randomp103 7-Apr-20 4:15am
I am not expected anyone to do everything for me as I said before I do not know where to start to do the conversion after the pipe-delimited text file is read into the array of strings.
OriginalGriff 7-Apr-20 4:32am
Start at the beginning.
Read the file into your array.
Then loop through the array, passing each lien to a "conversion" method.
In the method, process the line into it's separate fields.
Then process each field as described.
Rebuild each line from eth converted fields with commas to separate them, and return the new string.
Back in your loop, build your new strings into a new array, replace the old strings with your new ones, or output each row to a new file.

Do each step at a time, testing it to make sure it works before moving on to the next.
How is that complicated to work out? :)
Rate this:
Please Sign up or sign in to vote.

Solution 2

The simplest way to convert data between csv files is to use OleDB[^]. You can read text file with Schema.ini File (Text File Driver)[^] by specifying delimiter and dump data into another text file using different dilimiter.
For connection string details, please see: Textfile connection strings -[^]

Steps to do:
1. Open OleDbConnection Class (System.Data.OleDb) | Microsoft Docs[^]
2. Create OleDbCommand Class (System.Data.OleDb) | Microsoft Docs[^]
3. Create OldDbDataReader[^] and call command.ExecuteReader[^] method to get data into that reader.
4. Create DataTable[^] and call Load method[^] to data into it.
5. Finally, write a method, which dumps data into text file by processing each row of datatable.

1. Follow the steps 1-3
3. Read data from OleDbDataReader[^] and write each record into text file using new csv format.

Alternative #3
Use RTU (Ready To Use) solutions:
GitHub - ben-strasser/fast-cpp-csv-parser: fast-cpp-csv-parser[^]
GitHub - al-eax/CSVWriter: Simple c++ csv writer class[^]
GitHub - jay/CSV: C++ classes to read/write CSV records from/to a stream.[^]

Good luck!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100