Dec 15, 2013 at 12:10am UTC
Hi Everyone!
I have to write a small program that reads a file, remove the old (|)delimiter and replace them by a new one ( ,) so I can convert the file into .CSV OR XLSX file. then enclose any pre-existing field of the text that are surrounded by double quotes inside another pair of double quotes;
I wrote the following code to do the job, for some reason it reads only the first line of the text.
can somebody help me?
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void ProcessData(){
system("CLS");
FileConverter PipeToCommas;
string newline;
string input_sz;
string output_sz;
string the_string;
while(!infile.eof()){
infile >> input_sz;
}
int result = 0;
int length = input_sz.length();
while( result != string::npos ){
if(result < length){
result = input_sz.find(",",result);
if(result != -1){
the_string = input_sz.erase(result, 1);
the_string = input_sz.insert(result, "*");
}
}
}
int vert = 0;
while( vert != string::npos ){
if(vert < length){
vert = input_sz.find("|",result);
if(vert != -1){
the_string = input_sz.erase(vert, 1);
the_string = input_sz.insert(vert, "\",\"");
}
}
}
cout<<the_string<<"\n";
outfile<<the_string;
}
Thanks,
Last edited on Dec 16, 2013 at 6:01pm UTC
Dec 15, 2013 at 5:14am UTC
You sure it's not the last line it processes?
Anyway your closing brace for the while loop is in wrong place.
As it stands, you read the whole file before you process any line.
input_sz will have the value of the last line.
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
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void ProcessData(){
system("CLS" );
FileConverter PipeToCommas;
string newline;
string input_sz;
string output_sz;
string the_string;
while (!infile.eof()){
infile >> input_sz;
} /// move this brace to after your line processing code
int result = 0;
int length = input_sz.length();
while ( result != string::npos ){
if (result < length){
result = input_sz.find("," ,result);
if (result != -1){
the_string = input_sz.erase(result, 1);
the_string = input_sz.insert(result, "*" );
}
}
}
int vert = 0;
while ( vert != string::npos ){
if (vert < length){
vert = input_sz.find("|" ,result);
if (vert != -1){
the_string = input_sz.erase(vert, 1);
the_string = input_sz.insert(vert, "\",\"" );
}
}
}
cout<<the_string<<"\n" ;
outfile<<the_string;
}
Last edited on Dec 15, 2013 at 5:15am UTC
Dec 15, 2013 at 6:16pm UTC
Thank you, but where should I put that closing brace?
Dec 15, 2013 at 8:05pm UTC
sorry I have posted the wrong codes. here are the ones that read only the first line of the text file.
any hints, or advice on how to make it read the whole file will be welcome.
Thanks in advance guys.
void ProcessData(){
system("CLS");
FileConverter PipeToCommas;
string newline;
string input_sz;
string output_sz;
string the_string;
// while(getline(_file, input_sz)){
getline(infile, input_sz);
int result = 0;
int length = input_sz.length();
while( result != string::npos ){
if(result < length){
result = input_sz.find(",",result);
if(result != -1){
the_string = input_sz.erase(result, 1);
the_string = input_sz.insert(result, "*");
}
}
}
int vert = 0;
while( vert != string::npos ){
if(vert < length){
vert = input_sz.find("|",result);
if(vert != -1){
the_string = input_sz.erase(vert, 1);
the_string = input_sz.insert(vert, "\",\"");
}
}
}
for(unsigned int i = 0; i<the_string.length();++i){
if(the_string[i]=='*'){
the_string[i]=',';
}
}
cout<<the_string<<endl;
}