Язык C++ 20 баллов! Создать текстовый файл с произвольной информацией. Организовать просмотр содержимого файла. Организовать чтение и обработку данных из файла.

Приложения:

digaika99: А если в файле несколько данных, то выводится только последняя.

Ответы

Ответ дал: nikitaeda99
1
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. string function_1(string& str, string& object) {
  6. object = (str.substr(0, str.find_first_of(';'))) + " ";
  7. str.erase(0, str.find_first_of(';') + 1);
  8. return object;
  9. }
  10. string function_2(string& str, string& object) {
  11. object += (str.substr(0, str.find_first_of(';'))) + " ";
  12. str.erase(0, str.find_first_of(';') + 1);
  13. return object;
  14. }
  15. int main() {
  16. setlocale(LC_ALL, "Ru");
  17. try {
  18.  fstream in;
  19.  in.open("input.txt");
  20.  if (!in) {
  21.   throw "Файл не создан";
  22.  }
  23.  if (in.peek() == EOF) {
  24.   throw "Файл пуст";
  25.  }
  26.  string str,
  27.   name,
  28.   telNum,
  29.   index,
  30.   country,
  31.   region,
  32.   area,
  33.   city,
  34.   street,
  35.   house,
  36.   flat,
  37.   car,
  38.   numCar,
  39.   numCarPass;
  40.  while (getline(in, str)) {
  41.   //Ф.И.О.
  42.   function_1(str, name);
  43.   function_2(str, name);
  44.   function_2(str, name);
  45.   //Номер тел.
  46.   function_1(str, telNum);
  47.   //Домашний адресс
  48.   function_1(str, index);
  49.   function_1(str, country);
  50.   function_1(str, region);
  51.   function_1(str, area);
  52.   function_1(str, city);
  53.   function_1(str, street);
  54.   function_1(str, house);
  55.   function_1(str, flat);
  56.   //Машина
  57.   function_1(str, car);
  58.   function_1(str, numCar);
  59.   function_1(str, numCarPass);
  60.  }
  61.  cout << "name\t" << name
  62.   << "\ntelNum\t" << telNum
  63.   << "\nindex\t" << index
  64.   << "\ncountry\t" << country
  65.   << "\nregion\t" << region
  66.   << "\narea\t" << area
  67.   << "\ncity\t" << city
  68.   << "\nstreet\t" << street
  69.   << "\nhouse\t" << house
  70.   << "\nflat\t" << flat
  71.   << "\ncar\t" << car
  72.   << "\nnumCar\t" << numCar
  73.   << "\nnumCarPass\t" << numCarPass;
  74.  in.close();
  75. }
  76. catch (const char* msg) {
  77.  cout << "Ошибка: " << msg;
  78. }
  79. return 0;
  80. }

input.txt Зыбенко;Михаил;Петрович;+375332282282;220007;Беларусь;Минская;Партизанский;Минск;Пушкина;10;5;Ваз;7788AK-4;123.

Приложения:

digaika99: А если несколько марок автомобиля "Ваз", то через условие прописать?
Вас заинтересует