Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. void writeIntegerTobinaryFile(string filePath)
  2. {
  3. int inputInteger;
  4. cin » inputInteger;
  5.  
  6. fstream binaryFile(filePath, ios::out | ios :: binary | ios::app);
  7.  
  8. binaryFile.write((char*)&inputInteger,sizeof(inputInteger));
  9.  
  10. binaryFile.close();
  11. }
  12.  
  13. void readIntegerFromFile(string filePath)
  14. {
  15. int outputInteger;
  16. fstream binaryFile((char*)&outputInteger, sizeof(outputInteger));
  17. cout « outputInteger « endl;
  18. binaryFile.close();
  19.  
  20. }
  21.  
  22.  
  23.  
  24.  
  25. ofstream fout;
  26. fout.open("file.txt"); // a path to txt file where we'll print out text
  27. ifstream ifs;
  28. ifs.open("testfile.txt"); // inital text
  29.  
  30. int maxLength; // variable for holding maximum length value
  31. cout << "enter length ";
  32. cin >> maxLength;
  33.  
  34. string str;
  35.  
  36. if (!ifs.is_open()) // statement for testing file existing
  37. {
  38. cout << "We couldn't open it";
  39. }
  40. else
  41. {
  42.  
  43. cout << "\n File has been successufly opened ";
  44. char ch;
  45. while (ifs.get(ch))
  46. {
  47. str += ch; //appending char to a string variable
  48. }
  49. int counter = 0;
  50. int start = 0;
  51. int end = 0;
  52. for (int i = 0; i < str.length() + 1; i++)
  53. {
  54. if (str[i] != ' ')
  55. {
  56. counter++;
  57. if (i == str.length())
  58. {
  59. end = i;
  60. str.erase(str.begin() + start, str.begin() + end);
  61. break;
  62. }
  63. }
  64. else
  65. {
  66. end = i;
  67. if ((end - start) > maxLength)
  68. {
  69. str.erase(str.begin() + start, str.begin() + end);
  70. i -= end - start;
  71. }
  72. start = i;
  73. end = i;
  74. counter = 0;
  75. }
  76.  
  77. }
  78.  
  79.  
  80. for (int i = 0; i < str.length(); i++)
  81. {
  82. fout << str[i];
  83. }
  84.  
  85. }
  86. ifs.close();
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement