Reformat output txt file every 6 commas

I am trying to make a code which takes unformatted data from a .txt file and re-formats it into a new txt file. In particular, I'd like to make a new line every 6th comma (sort of).

Example of the unformatted .txt file:

68321,1648447,23-Sissies+Dragon,267,391,19,1024859602,1734694,Beit +52,541,279,5,938073616,1650226,54+AntiDemerol,523,430,7,279663618,1655600,46+Hello,464,603,16,993860414,1653819,N...+2,680,485,0,1111466665,1580568,Viras

My thought was to search for every 7th comma in the file, then back the cursor up 5 spots and make a new line. This is because the Size value can vary from 900 to 13500 (as shown in the example above)

Right now I am able to read the file, search for a comma, and replace it with a new line, then print it out into a new .txt file. However, it does this for every single comma, where I want to do it every 7th comma. I am still a new programmer, just teaching myself for fun/work - so I am interested in learning the correct way to accomplish this, not just the 'correct' code. So if anyone who is able to help could also explain to me what each line of code is doing, it would help me out a bunch!

Thanks everyone


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
  #include <fstream>
#include <iostream>
#include <string>
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include "ctype.h"

using namespace std;

int main()
{
	ifstream ifile("CLunformatted.txt",ios::binary); //read file 'CLunformatted.txt" 
	ifile.seekg(0,ios_base::end); // read characters from the beginning of the file to the end, starting at 0
	long s=ifile.tellg(); // set variable 's' to the 'tellg' command
	char *buffer=new char[s]; // set char variable buffer equal to array [s]  ***buffer serves as a temp storage for txt read from file
	ifile.seekg(0); // sets cursor to 0 position 
	ifile.read(buffer,s); // read 's' amount of characters from the file and save them to buffer 
	ifile.close(); // close the CLunformatted.txt file
	string txt(buffer,s); // create a string (txt) from the data stored in buffer ('s' char long)
	delete[] buffer; // delete buffer variable
	size_t off=0; // size_t is an unsigned int which stores the size of a particular variable (in this case 'off') - off is set to 0 

	while ((off=txt.find(",",off))!=string::npos) //while the cursor is not at the end of the string, look for the '+' symbol, and store it in 'off' variable
		txt.replace(off,sizeof(",")-1,"\n"); // replace the ',' with '\n' in the file by finding the location, moving the cursor back one space, and printing. 
	ofstream ofile("Formatted.txt"); // make new output file called Formatted.txt
	ofile.write(txt.c_str(),txt.size()); // converts txt.size into a C style string, and then writes to the file

	return 0;
}



This outputs the following:

68321
1648447
23-Sissies+Dragon
267
391
19
1024859602
1734694
Beitar+52
541
279
5
938073616
1650226
54-+AntiDemerol
523
430
7


My hope is to have the output program look like this (roughly):

Just an 'FYI' to help this make a bit more sense (I hope)

The outputs represent the following)

ID# (always 5 digits), Code (always 7 digits), City Name (Length Varies), X coord (always 3 Digits), Y Coord (always 3 digits), Island (1 or 2 digits), Size (between 3-5 digits)

68321,1648447,23-Sissies+Dragon,267,391,19,10248
59602,1734694,Beit +52,541,279,5,9380 
(etc.) 


Last edited on
from line 21 until the while loop

1
2
3
4
5
6
7
8
9
10
11
12
size_t oldOff = 0; 
size_t newOff = 0 ; 
int commasCounter = 0 ;
int commasLimiter = 7 ;


while ((newOff=txt.find(",", oldOff ) ) &&  newOff!=string::npos )
{

// Do you want to figure it yourself ?

}


why commasLimiter = 7 not 6 ?
68321,1648447,23-Sissies+Dragon,267,391,19,10248
--------1----------2-----------------------3----4----5---6--------7th comma = '\n'

I hope that will help ..
I have tried it and it worked...
I have only given you the variables and leave the rest for you if you want to figure it yourself .
If you want the code inside the while loop you can tell me ,but I'm sure you can solve it ..
Also I'm a beginner and maybe there are better solutions than this..

Good luck..

obscure - thank you for the response. I had been trying to use a counter as well, but kept getting weird results.

As for why I am using 7 commas instead of 6, well as you showed above,

68321,1648447,23-Sissies+Dragon,267,391,19,1024859602,1734694,Beit
--------1----------2----------------------3----4----5--6---------------7

68321,1648447,23-Sissies+Dragon,267,391,19,1024859602|

at that point I want to move the cursor backards 5 spots to here:

68321,1648447,23-Sissies+Dragon,267,391,19,10248|59602

this is because the first set of numbers in each row is always 5 digits, but the last set of numbers can be 3-5 digits long...does that make sense? or am I thinking about this wrong...

as for your code - thanks again. I am going to work on it now and see if i can get the answer inside of the while loop!
I spent part of this evening working on the inner part of the while loop, and although I believe I am close, it is cutting off the first digit of each row after the first loop.

Here is my new revised Code:

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
39
40
41
42
43
44
45
46
47
48
49
#include <fstream>
#include <iostream>
#include <string>
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include "ctype.h"

using namespace std;

int main()
{
	ifstream ifile("CLunformatted.txt",ios::binary); 
	ifile.seekg(0,ios_base::end); 
	long s=ifile.tellg(); 
	char *buffer=new char[s]; 
	ifile.seekg(0);  
	ifile.read(buffer,s); 
	ifile.close(); 
	string txt(buffer,s); 
	delete[] buffer; 
	size_t oldOff = 0; 
	int commasCount = 0 ;
	int commasLimiter = 6 ;
	string str2 (",");
	size_t newOff = txt.find(str2); 


while (newOff!=string::npos )
{
	if (newOff!=string::npos) 
	  { 
		  newOff = txt.find(",",newOff+1,1);
		  commasCount ++;
		  if (commasCount%commasLimiter==0)
		  {

			txt.replace(newOff-5,str2.length(),"\n");
			 ofstream ofile("Formatted.txt"); 
			 ofile.write(txt.c_str(),txt.size()); 
		  }

	  }


}
		 
	return 0;
}


Here is the Output .txt file:

68321,1648447,23-Sissies+Dragon,267,391,19,10248
9602,1734694,Beitar+52,541,279,5,9380
3616,1650226,54-+AntiDemerol,523,430,7,2796
3618,1655600,46+Hello,464,603,16,9938
0414,1653819,N...+2,680,485,0,11114
6665,1580568,Viras+city,553,419,0,10711
2996,1571395,55Core51,534,516,9,12360
2487,1878474,Arwen1987%27s+city,656,318,15,1012
5921,1656576,O63-Matties+Way,613,354,13,13716
1641,1673920,West+Palm,344,592,2,6663

6597,1728807,Helium,596,734,6,10461
2530,1682114,UM+IDK+YET,299,594,17,9612

6613,678335,Invader+Zin,450,513,1,8827
3732,1664095,brain+fart,514,639,9,3521
4121,945555,dashling%27s+city+10,376,334,13,997
2769,1664753,o34+Tavrobel,352,410,10,10824


0730,719255,55-03,554,549,1,11479

1906,719255,Rue+Nord,447,595,7,11523
2173,1675192,FUFU+LABOO,669,591,14,9304
1945,618238,lazyk%27s+city+3,637,659,7,4966
8294,1663044,37N-BeeLoved,393,705,8,12074
1958,1676472,F-5-33+Krystol+City,349,314,10,2537
7817,1651186,Assocacion+Antrax,282,530,8,9443
9043,981306,55+Laters+Baby,520,557,14,10148



Here is the Unformatted .txt file for reference:

68321,1648447,23-Sissies+Dragon,267,391,19,1024859602,1734694,Beitar+52,541,279,5,938073616,1650226,54-+AntiDemerol,523,430,7,279663618,1655600,46+Hello,464,603,16,993860414,1653819,N...+2,680,485,0,1111466665,1580568,Viras+city,553,419,0,1071162996,1571395,55Core51,534,516,9,1236072487,1878474,Arwen1987%27s+city,656,318,15,101245921,1656576,O63-Matties+Way,613,354,13,1371661641,1673920,West+Palm,344,592,2,6663
66597,1728807,Helium,596,734,6,1046162530,1682114,UM+IDK+YET,299,594,17,9612
66613,678335,Invader+Zin,450,513,1,882773732,1664095,brain+fart,514,639,9,352174121,945555,dashling%27s+city+10,376,334,13,99762769,1664753,o34+Tavrobel,352,410,10,10824
60730,719255,55-03,554,549,1,11479
21906,719255,Rue+Nord,447,595,7,1152372173,1675192,FUFU+LABOO,669,591,14,930471945,618238,lazyk%27s+city+3,637,659,7,496668294,1663044,37N-BeeLoved,393,705,8,1207471958,1676472,F-5-33+Krystol+City,349,314,10,253767817,1651186,Assocacion+Antrax,282,530,8,9443
9043,981306,55+Laters+Baby,520,557,14,10148


as I stated, my program is close to putting out the correct output - but every line after the first one is deleting the first digit from the row...any ideas why this may be occuring?
sorry for late response...
try this..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int commasCounter = 0 ;
int commasLimiter = 7 ;
size_t newOff = 0; 
	

while ((newOff=txt.find(",", ++newOff ) ) &&  newOff!=string::npos ) 
{ 
	if((++commasCounter) % commasLimiter == 0 )
	txt.insert( newOff -5,"\n"); // change replace with insert this is what causing the first digit to disappear	 				
}

ofstream ofile("Formatted.txt"); 
ofile.write(txt.c_str(),txt.size()); 


Your code can also give a true result but it throws out_of_range error ..
there is something wrong..

Thank you very much Obscure.

I am not exactly sure why mine was giving the 'Out_of_range' error, but I appreciate your help with this. I am obviously a much bigger 'begginner' than you....

Thanks again.
Topic archived. No new replies allowed.