Format text(ifstream, ofstream)

This is a discussion on Format text(ifstream, ofstream) within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by jackfraust Yes i can do that but when i do that, it counts all the excess whitespace ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,960
    Quote Originally Posted by jackfraust
    Yes i can do that but when i do that, it counts all the excess whitespace too and we back to where i started again about removing them.
    Once you have used my suggestion to skip over the whitespace, you can then simply take the length of the string (and the space separator) and add it to a running total. If the total exceeds the width, you print a newline, reset the total, and then print the string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by laserlight View Post
    Once you have used my suggestion to skip over the whitespace, you can then simply take the length of the string (and the space separator) and add it to a running total. If the total exceeds the width, you print a newline, reset the total, and then print the string.
    how do i do that?
    because the way i tried i got an error.

  3. #18
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Post what you tried.

  4. #19
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by R.Stiltskin View Post
    Post what you tried.
    Code:
     cout.width(70);
    cout << buff << ' ' ;

  5. #20
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Try what laserlight suggested:
    Let LINEMAX = 70
    Define line_length and word_length variables. Initialize line_length = 0. Each time you read a word into buff, before printing it, determine word_length, add word_length to line_length.
    If line_length > LINEMAX, print a newline, set line_length = word_length + 1 and print the word + ' '.
    Otherwise just add word_length + 1 to line_length and print.

    The only problem with this is that the lines that you "break" will end with a blank space. If that doesn't matter to you, fine. Otherwise, you can adapt the code I posted in #6.

  6. #21
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by R.Stiltskin View Post
    Try what laserlight suggested:
    Let LINEMAX = 70
    Define line_length and word_length variables. Initialize line_length = 0. Each time you read a word into buff, before printing it, determine word_length, add word_length to line_length.
    If line_length > LINEMAX, print a newline, set line_length = word_length + 1 and print the word + ' '.
    Otherwise just add word_length + 1 to line_length and print.

    The only problem with this is that the lines that you "break" will end with a blank space. If that doesn't matter to you, fine. Otherwise, you can adapt the code I posted in #6.
    Do i write a C program to count the words? or just use the ">>" operator to store the words?

  7. #22
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by jackfraust View Post
    Do i write a C program to count the words? or just use the ">>" operator to store the words?
    I already described in #20 what you should do, step by step. Read it again. Of course, you have to write code to perform these steps. Combine this code with either the code I posted in #6, or that laserlight posted in #5. As I said before, the only difference is that hers will put a blank space at the end of each line & mine won't.

  8. #23
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by R.Stiltskin View Post
    I already described in #20 what you should do, step by step. Read it again. Of course, you have to write code to perform these steps. Combine this code with either the code I posted in #6, or that laserlight posted in #5. As I said before, the only difference is that hers will put a blank space at the end of each line & mine won't.
    Code:
    		int word_length = 0;
    		int line_length = 0;
    		
    		while ( input >> buff ){
    			line_length += word_length;
    			if ( line_length > 30 ){
    				cout << "\n";
    			line_length = word_length + 1;
    			cout << buff << ' ';}
    			else{
    				line_length += word_length + 1;
    				cout << buff << ' ';}
    
      }
    but it doesn't make much a difference though. there are alot of blank spaces stil.

  9. #24
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    It's not "advanced programming" -- you just have to think carefully about exactly what you want the program to do. Here's my version. Make sure you can trace it in your head, step by step. If there's something here that you don't understand, ask.

    Code:
      const int MAX = 70;     // reduced to 70 instead of 100 
    
    
      int word_length, line_length = 0;
      if( input >> buff ) {
        line_length += strlen( buff );
        cout << buff;
        while( input >> buff ) {
          word_length = strlen( buff );
          line_length += word_length + 1;
          if( line_length > MAX ) {
            cout << "\n" << buff;
            line_length = word_length;
          } else {
            cout << ' ' << buff;
          }
        }
        cout << "\n";
      }
    This will work if you don't try to give it a word longer than 69 chars. Anything bigger than that is likely to either print garbage or crash it by overflowing the buffer.
    Last edited by R.Stiltskin; 04-03-2009 at 02:58 PM.

  10. #25
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by R.Stiltskin View Post
    It's not "advanced programming" -- you just have to think carefully about exactly what you want the program to do. Here's my version. Make sure you can trace it in your head, step by step. If there's something here that you don't understand, ask.

    Code:
      const int MAX = 70;     // reduced to 70 instead of 100 
    
    
      int word_length, line_length = 0;
      if( input >> buff ) {
        line_length += strlen( buff );
        cout << buff;
        while( input >> buff ) {
          word_length = strlen( buff );
          line_length += word_length + 1;
          if( line_length > MAX ) {
            cout << "\n" << buff;
            line_length = word_length;
          } else {
            cout << ' ' << buff;
          }
        }
        cout << "\n";
      }
    This will work if you don't try to give it a word longer than 69 chars. Anything bigger than that is likely to either print garbage or crash it by overflowing the buffer.
    I keep getting an error because strlen can't convert char to const char*. I even used char *buff but the program says that buff is uninitialized. If i keep getting this problem should i use cin.getline instead of the " input << buff"?

  11. #26
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Did you change your definition of buff?
    You should have
    Code:
    const int MAX = 70;
    char buff[MAX];
    in that order.

  12. #27
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by R.Stiltskin View Post
    Did you change your definition of buff?
    You should have
    Code:
    const int MAX = 70;
    char buff[MAX];
    in that order.
    If i wanna create prompt for the range(width) for the line, can i do it by creating a pointer since constants cannot be changed?

  13. #28
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by jackfraust View Post
    If i wanna create prompt for the range(width) for the line, can i do it by creating a pointer since constants cannot be changed?
    No, you must provide an actual char array or a c++ string to store the input. Right now, the constant MAX is serving two purposes: one is to set the size of the input buffer buff, and the other is to control the length of your output lines.
    But if you want to prompt the user for the output line length, just define another int variable for that purpose (DON'T name it line_length since you're already using that name in the editing loop), and edit the code that controls output line length:
    Code:
    ...
    if( line_length > MAX ) { // edit this line -- replace MAX with your new variable
    ...
    buff must be either a c++ string or a char array with a constant size, and that size must be big enough to fit the longest word.

  14. #29
    Registered User
    Join Date
    Feb 2009
    Posts
    72
    Quote Originally Posted by R.Stiltskin View Post
    No, you must provide an actual char array or a c++ string to store the input. Right now, the constant MAX is serving two purposes: one is to set the size of the input buffer buff, and the other is to control the length of your output lines.
    But if you want to prompt the user for the output line length, just define another int variable for that purpose (DON'T name it line_length since you're already using that name in the editing loop), and edit the code that controls output line length:
    Code:
    ...
    if( line_length > MAX ) { // edit this line -- replace MAX with your new variable
    ...
    buff must be either a c++ string or a char array with a constant size, and that size must be big enough to fit the longest word.
    I tried to write "buff" to ofstream but the text created only shows asian characters
    (i.e 甀据⹨洀牥s⸀찀쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌 )

    Code:
    output.write( buff, sizeof buff)
    output.close();

  15. #30
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Why are you writing the entire buffer? If you read a 5 letter word into a 70 character buff, buff contains those 5 letters, plus a '\0', plus 64 bytes of garbage.

    Use
    Code:
    output << buff;
    output.close();
    which will stop writing when it reaches the null character.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  3. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM
  4. Capturing key presses
    By cgod in forum Windows Programming
    Replies: 6
    Last Post: 11-25-2004, 12:10 PM
  5. Using ofstream in private of a class-errors
    By loobian in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2003, 09:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21