conversion to csv string

Hello All

Could anyone please help me convert a string into a CSV string. Is it possible to put commas at specific positions? The code below puts a comma whereever it finds a space. I want commas at certain specified position?.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

int main()
{
   string line;
    
   cout << "Enter a sentence:\n";
   
   getline(cin,line,'\n');
   

   stringstream ss(line);

   string word;
   string newLine = "";
   
   while ( ss >> word )
   {
     newLine = newLine + word + " ";
   }

   cout << "\n" <<newLine;
   cin.get();
   return 0;
}
What position?
at different positions and I know the exact positions where I need to put the comma

e-g my,name.is,gul hk
Yes, but how do you intend to tell your program where to insert the comma?
There is a string which consists of almost 20 substrings. For example

00403WELCH JONATHAN 2010-10-07 00:45:19.947 LFZ 45000 1 327681 720900 1

and I want to insert commas at these positions.

00403WELCH JONATHAN, 2010-10-07 00:45:19.947, LFZ, 45000, 1, 327681, 720900, 1

Then I would also need to remove the spaces in between
That seems arbitrary to me. Have you thought of how you might specify where to insert the commas?

The bottom line is, if you can't specify it, you can't instruct a program on what to do.
I know the exact start and end position of each substring that is of interest. E-g 00403 would always be 5 characters and the substring is (0,5) so I want to insert a comma after 00403 i-e after the 5th character of this substring, datetime i-e 2010-10-07 00:45:19.947 will always be 24 characters and the substring is (55,24) and I want to insert a comma after this datetime i-e after the 24th character. Is it possible? Please guide.

Above is just an example but which splits the string at each space it finds.
Last edited on
Topic archived. No new replies allowed.