Advertisement
avr39ripe

cppTextFilesBasics

Nov 3rd, 2021
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. //ofstream - output stream
  5. //ifstream - input stream
  6. //fstream - input/output stream
  7.  
  8. int main()
  9. {
  10.     //std::ofstream outf;
  11.  
  12.     //outf.open("d:\\file.txt");
  13.  
  14.     //if ( !outf )
  15.     //{
  16.     //  std::cout << "IOERROR: can't create/open file!\n";
  17.     //  return 1;
  18.     //}
  19.  
  20.     //outf << "String #1\n";
  21.     //outf << "String #2\n";
  22.     //outf << "String #3\n";
  23.  
  24.     ////for (int i{ 0 }; i < 10; ++i)
  25.     ////{
  26.     ////    outf << i << '\n';
  27.     ////}
  28.  
  29.     //outf.close();
  30.  
  31.     std::ifstream inf;
  32.  
  33.     inf.open("C:\\Users\\ribchansky_o\\source\\repos\\Study\\Study\\Study.cpp");
  34.  
  35.     if (!inf)
  36.     {
  37.         std::cout << "IOERROR: can't open file!\n";
  38.         return 1;
  39.     }
  40.  
  41.     const int strSize{ 300 };
  42.     const int textSize{ 500 };
  43.  
  44.     char str[strSize];
  45.  
  46.     int lineNumber{ 0 };
  47.     char* text[textSize]{};
  48.     int strLen{};
  49.  
  50.     while (!inf.eof())
  51.     {
  52.         inf.getline(str, strSize - 1);
  53.  
  54.         strLen = strlen(str) + 1;
  55.         text[lineNumber] = new char[strLen];
  56.         strcpy_s(text[lineNumber++], strLen, str);
  57.  
  58.         //std::cout << str << '\n';
  59.     }
  60.    
  61.     inf.close();
  62.  
  63.     std::ofstream outf;
  64.  
  65.     outf.open("d:\\Programm.cpp");
  66.  
  67.     if ( !outf )
  68.     {
  69.         std::cout << "IOERROR: can't create/open file!\n";
  70.         return 1;
  71.     }
  72.  
  73.     for (lineNumber = 0; text[lineNumber]; ++lineNumber)
  74.     {
  75.         std::cout << text[lineNumber] << '\n';
  76.         outf << text[lineNumber] << '\n';
  77.  
  78.         delete[] text[lineNumber];
  79.     }
  80.    
  81.     outf.close();
  82.  
  83.     return 0;
  84. }  
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement