Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<iomanip>
  4. #include<Windows.h>
  5. #include<string>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. int main() {
  11. SetConsoleCP(1251);
  12. SetConsoleOutputCP(1251);
  13. string File;
  14. //Файл.txt
  15. ifstream fin;
  16.  
  17.  
  18. do {
  19. cout << "Введите имя файла: ";
  20. cin >> File;
  21. fin.open(File);
  22. } while (!fin);
  23.  
  24.  
  25. int count = 0, t;
  26.  
  27. int chet = 0;
  28.  
  29.  
  30.  
  31. while (!(fin >> ws).eof()) {
  32. streampos p;//объявление переменной p типа streampos
  33. p = fin.tellg();//получает указатель позиции файла
  34. fin.seekg(p);//устанавливает указатель позиции файла
  35. fin >> t;//запись в t файла
  36. fin.clear();//очитска состояния потока
  37. cout << t << " ";//вывод в консоль данных
  38. chet += 1;//счет элементов
  39.  
  40.  
  41. string str;
  42. fin.clear();//очистка потока
  43. if (!fin.fail()) {
  44. char c = fin.peek();//получения символа текущей позиции указателя
  45. if (c == ' ' || c == '\t' || c == '\n' || c == EOF) {
  46. count++;
  47. }
  48.  
  49. else {
  50. fin.seekg(p);
  51. fin >> str;
  52. cout << "Найдена ошибка в элементах массива: " << "\"" << str << "\"" << endl;
  53. cout << "Номер индексa: " << "\"" << count + 1 << "\"" << endl;
  54. break;
  55. }
  56.  
  57. }
  58.  
  59.  
  60.  
  61.  
  62. }
  63.  
  64.  
  65.  
  66. cout << "Количество элементов в файле: " << chet << endl;
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. int* mass = new(nothrow) int[chet];
  76. if (!mass) {
  77. cout << "Ошибка выделения памяти!" << endl;
  78. fin.close();
  79. return 0;
  80. }
  81. else if (chet == 0) {
  82. cout << "Файл пуст!" << endl;
  83.  
  84. }
  85. else {
  86. cout << "Все круто!" << endl;
  87. }
  88. delete[]mass;
  89. return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement