Click here to Skip to main content
15,886,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
 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,
Posted
Updated 7-Apr-20 4:32am
v2

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.
 
Share this answer
 
v2
Comments
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? :)
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 - ConnectionStrings.com[^]

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.

Alternativelly..
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!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900