You are here:

C++/c++text file


the program prompt user to enter the filename of a text file and number of columns(width).It will then read the file and then create a new file in which all the lines in the text file will not contain more than the number of columns entered.All lines will terminate with a Carriage Return(CR) and Line Feed(LF).Ensure that any word must not be truncated or splitted but wrap around if it exceeds the number of columns.Name the new file as test_wrapXX.txt where the input file is test.txt and XX is the columns numbers entered by the user.The range of acceptable column numbers will be from 20-100.

As an illustration,suppose user enters an input text file(test.txt) and column number as 20.Suppose the input file consists of the following data:
The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.

Program shall create an output file(test_wrap20.txt) with the following content:
The quick brown fox
jumps over the lazy
dog.The quick brown
fox jumps over the
lazy dog.

code so far:
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
  int width;
  string file_name;

   cout <<"Please enter the name of the file to create: ";
   cin >> file_name;
   cout <<"Please enter the width: ";
   cin >> width;
   cout << endl;
   ofstream f;;
   if (f.is_open())
       f <<"The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.";

       f.close(); //remeber to close your files.
       ifstream inFile;
       string sentence;
       if (inFile.is_open())
         while (!inFile.eof())
         inFile >> sentence;
         cout << sentence <<"";
       } else
         cout << "Could not open the file for reading" <<endl;
       cout << "Could not open file for output!"<<endl;

   return 0;
pls help mi!!!i will be appreciated for all the help!!!  

> All lines will terminate with a Carriage Return(CR) and Line Feed(LF)

this is incorrect (at the very least it is not portable).

operating systems (and file systems) represent a newline with one or two control characters:

Multics, Unix and Unix-like systems (eg. linux) use an ASCII LF (Line feed, 0x0A) for a newline.
Commodore machines, Apple II, Mac OS (version 9 or lower) and OS-9 use an ASCII CR (Carriage Return, 0x0D) for a newline.
DEC RT-11, DOS, OS/2, Microsoft Windows, Symbian OS use CR followed by LF (CR+LF, 0x0D 0x0A) for a newline.
EBCDIC systems (mainlyIBM, like OS/390 and OS/400) use NEL (Next Line, 0x15) for a newline.
there are more exotic variants: eg. CDC 6000 defined a newline as two or more zero-valued six-bit characters at the end of a 60-bit word

programming languages provide abstractions to deal with these differences so that code can be written in a portable manner.

C provides escape sequences '\n' and '\r'. contrary to popular belief, these are not required to be equivalent to specific control characters. the C standard guarantees that
1. each of these escape sequences maps to a unique implementation-defined number that can be stored in a single char.
2. when writing a file in text mode, '\n' is transparently translated to the native newline sequence on the the system (which may be longer than one character).
3. when reading in text mode, the native newline sequence (which may again be longer than one character) is translated back to '\n'.
4. in binary mode i/o, no such translations are performed.

c++ also provides L'\n' and L'\r'; these are the wchar_t equivalents.
for any char_type, basic_ios<char_type,traits_types>::widen('\n') can be portably used.

to get identical behaviour on all platforms, open the file in 'text' mode and use a '\n' to represent a new line.

now comming to the problem at hand:

a. keep a count of the number of characters so far printed on the current line.

b. read one line from the input file (use std::getline)

c. parse the line to read it word by word (use a std::istringstream for this).

d. for each word:
   d1. if the word goes beyond the number of columns allowed,
         i. write a new line '\n' to output.
         ii. reset the number of characters so far printed to zero

   d2. write the word followed by a white space to output

   d3. add the number of characters written to number of characters so far printed

e. write a new line to output

f. go back and repeat for the next line in the input file till eof.

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

void  wrap_lines( const char* input_file_name, const char* output_file_name )
     enum { MAX_CHARS_PER_LINE = 20 } ;
     std::ifstream in_file( input_file_name ) ;
     std::ofstream out_file( output_file_name ) ;
     std::string line ;

     while( std::getline( in_file, line ) )
         std::istringstream stream( line ) ;
         std::string word ;
         std::size_t num_chars = 0 ;

         while( stream >> word )
         if(   ( num_chars+word.size() ) > MAX_CHARS_PER_LINE )
         out_file << '\n' ;
         num_chars = 0 ;
         out_file << word << ' ' ;
         num_chars += word.size() + 1 ;
         out_file << '\n' ;


All Answers

Answers by Expert:

Ask Experts




my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.


about 15 years or so

post graduate engineer

©2016 All rights reserved.