justinY
asked on
maximum length reading on one line
My program reads input file and writes to output file, but it only writes 250 char length in one line and then writes the rest of chars to the second line.
How can I fix that to make my program writes all the chars to one line ?
or I should ask this way : whats the maximum char length I can have on one line ?
Thanks
How can I fix that to make my program writes all the chars to one line ?
or I should ask this way : whats the maximum char length I can have on one line ?
Thanks
ASKER
the following is my code. It works well with short output file.
But with this long output length. It writes first 250 char length onto one line and writes the rest chars to next line.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdio>
using namespace std;
const int MaxSize=1024;
////////////////////////// ////////// ////////// ////////// ////
// Custom version of strtok(), fixing empty field problem
////////////////////////// ////////// ////////// ////////// ////
char* MyStrTok(char* text, const char del)
{
static char *curTok = NULL;
char *result = NULL;
if ( text != NULL )
{
curTok = text;
}
result = curTok;
if ( curTok != NULL )
{
curTok = ::strchr(curTok, del);
if ( curTok != NULL ) *curTok++ = 0;
}
return result;
};
////////////////////////// ////////// //////
// Remove delimiter
////////////////////////// ////////// //////
void removeChar(char* src, char delim )
{
int i=0,j=0;
char buf[MaxSize];
while (src[i] != '\0' )
{
if (src[i] == delim)
{
i++;
continue;
}
buf[j++]=src[i];
i++;
}
buf[j] = '\0';
strcpy(src,buf);
};
////////////////////////// /////
//
// main
//
////////////////////////// /////
main(int arc, char *arv[])
{
ifstream fin("test.csv");
ofstream fout("fout.txt");
char row_read[512];
char row_elem[100][100];
int i = 0;
int isFirstLine = 0;
////////////////////////// ////////// ////////// ////////// ////////// /
// for each line of file
// assume there are 35 columns in each row in file fin.txt
////////////////////////// ////////// ////////// ////////// ////////// /
while( fin.getline( row_read, sizeof( row_read ) ) )
{
if (isFirstLine ==0 )
{
isFirstLine = 1;
continue;
}
////////////////////////// ////////// //////
// parse string with ","
////////////////////////// ////////// //////
char *token = MyStrTok( row_read, ',' );
while( token != NULL )
{
////////////////////////// ////////// ////////// ////////// ///
// strim '"', '=' etc.. in the fields
////////////////////////// ////////// ////////// ////////// //
/*
char intrBuf[MaxSize];
strcpy(intrBuf,token);
removeChar(intrBuf,'=');
removeChar(intrBuf,'"');
strcpy(row_elem[i], intrBuf );
*/
strcpy(row_elem[i], token);
/* Get next token: */
token = MyStrTok( NULL, ',' );
i++;
} // end of while
////////////////////////// ////////// ////////// ////////// ////////// ////
// output to file, first colmn replaced with value from reference file
////////////////////////// ////////// ////////// ////////// ////////// ////
fout<<setw(9)<< row_elem[0]
<<setw(2) << "US"
<<setw(1) << " "
<<setw(2) << row_elem[3]
<<setw(4)<< row_elem[4]
<<setw(3) << " "
<<setw(4) << " "
<<setw(4) << " "
<<setw(2) << row_elem[8]
<<setw(2) << row_elem[9]
<<setw(2) << row_elem[10]
<<setw(2) << row_elem[11]
<<setw(3) << row_elem[12]
<<setw(3) << row_elem[13]
<<setw(60) << row_elem[14]
<<setw(5) << row_elem[15]
<<setw(15) << row_elem[16]
<<setw(1) << "Y"
<<setw(6) << row_elem[0]
<<setw(1) << "S"
<<setw(1) << "S"
<<setw(1) << "Y"
<<setw(1) << "Y"
<<setw(1) << "Y"
<<setw(1) << "Y"
<<setw(2) << " "
<<setw(15) << row_elem[26]
<<setw(40) << row_elem[27]
<<setw(9) << " "
<<setw(9) << " "
<<setw(2) << "NA"
<<setw(16) << row_elem[31]
<<setw(1) << "N"
<<setw(16) << row_elem[33]
<<setw(1) << "N"
<<setw(1) << "N"
<<setw(1) << "Y"
<<setw(1) << "N"
<<setw(1) << "S"
<<setw(1) << "N"
<<setw(5) << row_elem[40]
<<setw(1) << "N"
<<setw(5) << row_elem[42]
<<setw(1) << "N"
<<setw(5) << " "
<<setw(5) << " "
<<setw(1) << "N"
<<setw(3) << "N"
<<setw(10) << row_elem[48]
<<setw(20) << " "
<<setw(1) << row_elem[50]
<<setw(16) << row_elem[51]
<<setw(1) << "N"
<<setw(1) << "N"
<<endl;
i=0;
} // end of while
return 0;
}
But with this long output length. It writes first 250 char length onto one line and writes the rest chars to next line.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdio>
using namespace std;
const int MaxSize=1024;
//////////////////////////
// Custom version of strtok(), fixing empty field problem
//////////////////////////
char* MyStrTok(char* text, const char del)
{
static char *curTok = NULL;
char *result = NULL;
if ( text != NULL )
{
curTok = text;
}
result = curTok;
if ( curTok != NULL )
{
curTok = ::strchr(curTok, del);
if ( curTok != NULL ) *curTok++ = 0;
}
return result;
};
//////////////////////////
// Remove delimiter
//////////////////////////
void removeChar(char* src, char delim )
{
int i=0,j=0;
char buf[MaxSize];
while (src[i] != '\0' )
{
if (src[i] == delim)
{
i++;
continue;
}
buf[j++]=src[i];
i++;
}
buf[j] = '\0';
strcpy(src,buf);
};
//////////////////////////
//
// main
//
//////////////////////////
main(int arc, char *arv[])
{
ifstream fin("test.csv");
ofstream fout("fout.txt");
char row_read[512];
char row_elem[100][100];
int i = 0;
int isFirstLine = 0;
//////////////////////////
// for each line of file
// assume there are 35 columns in each row in file fin.txt
//////////////////////////
while( fin.getline( row_read, sizeof( row_read ) ) )
{
if (isFirstLine ==0 )
{
isFirstLine = 1;
continue;
}
//////////////////////////
// parse string with ","
//////////////////////////
char *token = MyStrTok( row_read, ',' );
while( token != NULL )
{
//////////////////////////
// strim '"', '=' etc.. in the fields
//////////////////////////
/*
char intrBuf[MaxSize];
strcpy(intrBuf,token);
removeChar(intrBuf,'=');
removeChar(intrBuf,'"');
strcpy(row_elem[i], intrBuf );
*/
strcpy(row_elem[i], token);
/* Get next token: */
token = MyStrTok( NULL, ',' );
i++;
} // end of while
//////////////////////////
// output to file, first colmn replaced with value from reference file
//////////////////////////
fout<<setw(9)<< row_elem[0]
<<setw(2) << "US"
<<setw(1) << " "
<<setw(2) << row_elem[3]
<<setw(4)<< row_elem[4]
<<setw(3) << " "
<<setw(4) << " "
<<setw(4) << " "
<<setw(2) << row_elem[8]
<<setw(2) << row_elem[9]
<<setw(2) << row_elem[10]
<<setw(2) << row_elem[11]
<<setw(3) << row_elem[12]
<<setw(3) << row_elem[13]
<<setw(60) << row_elem[14]
<<setw(5) << row_elem[15]
<<setw(15) << row_elem[16]
<<setw(1) << "Y"
<<setw(6) << row_elem[0]
<<setw(1) << "S"
<<setw(1) << "S"
<<setw(1) << "Y"
<<setw(1) << "Y"
<<setw(1) << "Y"
<<setw(1) << "Y"
<<setw(2) << " "
<<setw(15) << row_elem[26]
<<setw(40) << row_elem[27]
<<setw(9) << " "
<<setw(9) << " "
<<setw(2) << "NA"
<<setw(16) << row_elem[31]
<<setw(1) << "N"
<<setw(16) << row_elem[33]
<<setw(1) << "N"
<<setw(1) << "N"
<<setw(1) << "Y"
<<setw(1) << "N"
<<setw(1) << "S"
<<setw(1) << "N"
<<setw(5) << row_elem[40]
<<setw(1) << "N"
<<setw(5) << row_elem[42]
<<setw(1) << "N"
<<setw(5) << " "
<<setw(5) << " "
<<setw(1) << "N"
<<setw(3) << "N"
<<setw(10) << row_elem[48]
<<setw(20) << " "
<<setw(1) << row_elem[50]
<<setw(16) << row_elem[51]
<<setw(1) << "N"
<<setw(1) << "N"
<<endl;
i=0;
} // end of while
return 0;
}
ASKER
I should say this way:
on my fout.txt file, It writes first 250 ( or less than 250 ) char length onto one line and writes the rest chars to next line.
on my fout.txt file, It writes first 250 ( or less than 250 ) char length onto one line and writes the rest chars to next line.
ASKER
Never Mind guys. False alarm. It has something to do with the monitor settings.
I opened this same file on different monitors. One flat panel gives me displaying all on one line. But my big old one gives me two lines.
I opened this same file on different monitors. One flat panel gives me displaying all on one line. But my big old one gives me two lines.
Good to hear that you solved it. Ask Community Services to refund your points :-)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
zaghaghi, I am more than happy to hear more from you about the editor. Can you kindly give me details ? thanks
He only wants to tell you that depending on which editor you use it may be possible to have different settings for the length of a line. But since we don't know OS/Editor you are using there is not much more to say.
ASKER
I am using windows2000 professional and wordpad.
In View->Options you can set word wrapping. Set it to 'no wrap' and it will show real lines.
in some editors -likes 'word pad' that you use for viewing text files- the "no wrap" option is off by default and your lines wraps in the editor, so you must on this option for your editor.
have a good programming day.
have a good programming day.
Please show us your code for the read/write operations.
======
Werner