1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6.  
  7. void flush_left ( vector<string> v1, int width)
  8. {
  9. ofstream out_file ( "flush_left.txt");
  10.  
  11. // Outputs the first line, " -----".
  12. for ( int i =0; i< width+4; i++)
  13. {
  14. out_file << "-";
  15. }
  16. out_file << endl;
  17.  
  18. int nWidth = width;
  19. int new_line = 0;
  20. int line_size =0;
  21.  
  22. for ( int i = 0; i < v1.size() ;i++)
  23. {
  24. line_size = line_size + v1[i].size();
  25. if ( new_line == 0)
  26. {
  27.  
  28. out_file << "| " << v1[i];
  29. new_line = 1;
  30. }
  31. else
  32. {
  33. if ( line_size + 1 <= nWidth)
  34. {
  35. out_file << " " << v1[i];
  36. // nWidth = nWidth- v1[i].size();
  37. new_line = 1;
  38. line_size++;
  39. }
  40. else
  41. {
  42. for ( int j = line_size - v1[i].size(); j < nWidth + 1; j++)
  43. {
  44. out_file << " ";
  45. }
  46. out_file << "|" << endl;
  47. // nWidth = width;
  48. out_file << "| " << v1[i];
  49. line_size = v1[i].size();
  50. new_line = 1;
  51. }
  52. }
  53. }
  54. for ( int j = line_size - v1[v1.size() - 1].size(); j < nWidth + 1; j++)
  55. {
  56. out_file << " ";
  57. }
  58. out_file << "|" << endl;
  59. // nWidth = width;
  60.  
  61. for ( int i =0; i< width+4; i++)
  62. {
  63. out_file << "-";
  64. }
  65. }
  66.  
  67.  
  68.  
  69.  
  70. int main()
  71. {
  72. cout << " What specified width do you want it " << endl;
  73. cout << "to be displayed ? " << endl;
  74.  
  75. int width;
  76. cin >> width;
  77.  
  78. ifstream read_file;
  79. read_file.open( "file.txt" );
  80. if (!read_file.is_open())
  81. {
  82. cout << " File not found " << endl;
  83. }
  84. vector<string> s_vector;
  85. string input_string;
  86. while (!read_file.eof())
  87. {
  88. read_file >> input_string;
  89. s_vector.push_back( input_string);
  90. }
  91.  
  92. flush_left ( s_vector, width);
  93.  
  94.  
  95. return 0;
  96. }
  97.  
  98.  
  99.  
  100.