How to modify numbers in file "C"

Hello.. I want to modify a file that contain numbers which are divided by ",".
The numbers in file are represented like this:

Example

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
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
:) nice... I have test that and works perfectly.. ty for your help. Now I'll try to reproduce that in C language.

Thank you so much
lastchance
!!!
And I'll try to do that opening a file.txt, and read it first, but that's enough I understand how it works.
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
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.
Sorry I'm more with 'C' not C++ but I do understand what did you wrote :D
Thank you again..
Okay.. done in C language. A bit rudimentary but it works. :)

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
30
31
32
33
34
35
36
37
38
#include <Windows.h>
#include <stdio.h>

#pragma warning(disable: 4996) // disable fopen unsafe warning function (instead use fopen_s)

#define OLDFILE "before.txt"
#define NEWFILE "after.txt"

int main()
{
	remove(NEWFILE);
	FILE* oldFile = fopen(OLDFILE, "r");
	FILE* newFile = fopen(NEWFILE, "a+");
	float a;
		
	if(oldFile != NULL)
	{
		while (fgetc(oldFile) != EOF)
		{
			while (fscanf(oldFile, "%f", &a) == 1)
			{
				if (a == 0.0) fprintf(newFile, "%.0f\t", a);
				else fprintf(newFile, "%.1f\t", a);
			}
		}

		fclose(oldFile);
		fclose(newFile);
	}
	else
	{
		MessageBoxA(NULL, "before.txt does not exist, check the file", "Error !", MB_ICONERROR | MB_OK);
		fclose(newFile);
		remove(NEWFILE);
	}

	return (0);
}
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.
Topic archived. No new replies allowed.