Jan 16, 2022 at 6:41pm Jan 16, 2022 at 6:41pm UTC
Hello.. I want to modify a file that contain numbers which are divided by ",".
The numbers in file are represented like this:
000.0,001.4,003.2,012.1
002.1,021.5,000.2,003.5
023.5,005.4,000.0,032.9
File after have been modified
0 1.4 3.2 12.1
2.1 21.5 0.2 3.5
23.5 5.4 0 32.9
So.. the modified new file have the numbers without the 0 (zeros) and instead of "," (comma) I need to put a horizontal tab between them, except last one because is the last number and comma is no needed.
Can someone help me understand how to do that?
Thanks in advance..
Last edited on Jan 16, 2022 at 6:42pm Jan 16, 2022 at 6:42pm UTC
Jan 16, 2022 at 7:17pm Jan 16, 2022 at 7:17pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
istringstream test( "000.0,001.4,003.2,012.1\n"
"002.1,021.5,000.2,003.5\n"
"023.5,005.4,000.0,032.9\n" );
int main()
{
double a, b, c, d;
char comma;
istream &in = test;
// ifstream in( "test.dat" );
while ( in >> a >> comma >> b >> comma >> c >> comma >> d ) cout << a << '\t' << b << '\t' << c << '\t' << d << '\n' ;
}
0 1.4 3.2 12.1
2.1 21.5 0.2 3.5
23.5 5.4 0 32.9
Last edited on Jan 16, 2022 at 7:20pm Jan 16, 2022 at 7:20pm UTC
Jan 16, 2022 at 7:25pm Jan 16, 2022 at 7:25pm UTC
And I'll try to do that opening a file.txt, and read it first, but that's enough I understand how it works.
Jan 16, 2022 at 7:25pm Jan 16, 2022 at 7:25pm UTC
If you don't know how many items there are in a line then you could use the more general
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
istringstream test( "000.0,001.4,003.2,012.1\n"
"002.1,021.5,000.2,003.5\n"
"023.5,005.4,000.0,032.9\n" );
int main()
{
istream &in = test;
// ifstream in( "test.dat" );
for ( string s; getline( in, s ); )
{
stringstream ss( s );
string number;
while ( getline( ss, number, ',' ) ) cout << stod( number ) << '\t' ;
cout << '\n' ;
}
}
or
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;
istringstream test( "000.0,001.4,003.2,012.1\n"
"002.1,021.5,000.2,003.5\n"
"023.5,005.4,000.0,032.9\n" );
int main()
{
istream &in = test;
// ifstream in( "test.dat" );
for ( string s; getline( in, s ); )
{
for ( char &c : s ) if ( c == ',' ) c = ' ' ;
stringstream ss( s );
for ( double d; ss >> d; ) cout << d << '\t' ;
cout << '\n' ;
}
}
Last edited on Jan 16, 2022 at 7:28pm Jan 16, 2022 at 7:28pm UTC
Jan 16, 2022 at 7:28pm Jan 16, 2022 at 7:28pm UTC
That was an example , in a raw are 19 items and the file contains 4 raws.
So first I'll open the file with the FILE *pointer and I'll add your example.
Jan 16, 2022 at 7:29pm Jan 16, 2022 at 7:29pm UTC
Sorry I'm more with 'C' not C++ but I do understand what did you wrote :D
Thank you again..
Jan 17, 2022 at 3:42pm Jan 17, 2022 at 3:42pm UTC
I think I may have a mistake.. at the end of every line I forgot I don't have to print the horizontal TAB, well.. I believe that's pretty easy to correct.