Advertisement
thinhckhcmus

thao tac tap tin co ban_mang(luu,doc)

May 23rd, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. struct sinhvien
  6. {
  7. string ten;
  8. int mssv;
  9. float dtb;
  10. };
  11. typedef sinhvien SV;
  12. void LuuDS(SV a[],int n)// luu file
  13. {
  14. ofstream FileOut;
  15. FileOut.open("DoAn.txt");
  16. if (FileOut.is_open())
  17. {
  18.  
  19. for (int i = 0; i < n; i++)
  20. {
  21. FileOut << a[i].ten << ",";
  22. FileOut << a[i].mssv << ",";
  23. FileOut << a[i].dtb << endl;
  24. }
  25. }
  26. FileOut.close();
  27. }
  28. void DocDs(SV a[], int n)// doc file
  29. {
  30. ifstream Filein;
  31. Filein.open("DoAn.txt");
  32. string ch;
  33. cout << "doc danh sach" << endl;
  34. if (Filein.is_open())
  35. {
  36. for (int i = 0; i < n; i++)
  37. {
  38. getline(Filein, a[i].ten, ',');
  39. cout << a[i].ten << endl;
  40. Filein >> a[i].mssv;
  41. cout << a[i].mssv << endl;
  42. Filein >> a[i].dtb;
  43. cout << a[i].dtb << endl;
  44. getline(Filein, ch, '\n');
  45. }
  46. }
  47. Filein.close();
  48. }
  49. void nhapSV(SV a[], int n)
  50. {
  51.  
  52. cin.ignore('\n', 10);
  53. for (int i = 0; i < n; i++)
  54. {
  55. cout << "nhap ten sinh vien: ";
  56. getline(cin, a[i].ten);
  57. cout << "nhap MSSV: ";
  58. cin >> a[i].mssv;
  59. cout << "nhap DTB: ";
  60. cin >> a[i].dtb;
  61. cin.ignore('\n', 10);// tranh truong hop nhận enter là tên ( không tin xoá thử để biết :))) )
  62. }
  63. }
  64.  
  65. void main()
  66. {
  67. SV a[100];
  68. int n;
  69. cout << "nhap so luong danh sach sinh vien: ";
  70. cin >> n;
  71. nhapSV(a,n);
  72. LuuDS(a, n);
  73. DocDs(a, n);
  74. system("pause");
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement