Урок 9. Работа с файлами в с++

Автор: | 15.11.2018

Видео для урока.

Код урока.

  1. // ConsoleApplication3.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7. int main()
  8. {
  9. ofstream myfile;
  10. myfile.open("login.txt");
  11. myfile << "admin";
  12. myfile.close();
  13. myfile.open("password.txt");
  14. myfile << "123";
  15. myfile.close();
  16. ifstream file("password.txt");
  17. char *pass1;
  18. pass1 = new char[100];
  19. char *login1;
  20. login1 = new char[100];
  21. file.getline(pass1,'\n');
  22. file.close();
  23. file.open("login.txt");
  24. file.getline(login1, '\n');
  25. char *login;
  26. char *password;
  27. login = new char[100];
  28. password = new char[100];
  29. cout << "Enter login: ";
  30. cin >> login;
  31. cout << "Enter password: ";
  32. cin >> password;
  33. if (strcmp(login, login1) == 0 && strcmp(pass1, password) == 0)
  34. {
  35. cout << "Hello admin.";
  36. }
  37. else
  38. {
  39. cout << "You are wrong.";
  40. }
  41. cin >> password;
  42. delete [] pass1;
  43. delete [] login;
  44. delete [] password;
  45. return 0;
  46. }
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
  ofstream myfile;
  myfile.open("login.txt");
  myfile << "admin";
  myfile.close();
  myfile.open("password.txt");
  myfile << "123";
  myfile.close();
  ifstream file("password.txt");
  char *pass1;
  pass1 = new char[100];
  char *login1;
  login1 = new char[100];
  
  file.getline(pass1,'\n');
  file.close();
  file.open("login.txt");
  file.getline(login1, '\n');
  
  char *login;
  char *password;
  login = new char[100];
  password = new char[100];
  cout << "Enter login: ";
  cin >> login;
  cout << "Enter password: ";
  cin >> password;
  if (strcmp(login, login1) == 0 && strcmp(pass1, password) == 0)
  {
    cout << "Hello admin.";
  }
  else
    {
    cout << "You are wrong.";
    }
  cin >> password;
  delete [] pass1;
  delete [] login;
  delete [] password;
  return 0;
}