Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

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
0
justinY
Asked:
justinY
  • 5
  • 4
  • 2
1 Solution
 
griesshCommented:
Hi justinY,

Please show us your code for the read/write operations.

======
Werner
0
 
justinYAuthor Commented:
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;
}
0
 
justinYAuthor Commented:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
justinYAuthor Commented:
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.
0
 
griesshCommented:
Good to hear that you solved it. Ask Community Services to refund your points :-)
0
 
Hamed ZaghaghiCommented:
hi,
your problem doesn't relate to your monitor, it's just about the editor you use to view the file.

have a good programming day;
0
 
justinYAuthor Commented:
zaghaghi, I am more than happy to hear more from you about the editor. Can you kindly give me details ? thanks
0
 
griesshCommented:
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.
0
 
justinYAuthor Commented:
I am using windows2000 professional and wordpad.
0
 
griesshCommented:
In View->Options you can set word wrapping. Set it to 'no wrap' and it will show real lines.
0
 
Hamed ZaghaghiCommented:
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.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now