30/09/2018, 21:06

Cần các bro giúp đỡ trong vấn đề đọc file c++

em có 1 bài tập như sau tạo chương trình quản lý sinh viên gồm stt họ tên năm sinh tổng điểm đầu tiên bắt nhập từ bàn phím in ra file nhap.txt sau đó đọc lại dữ liệu từ file đó sắp xếp và ghi ra xuat.txt
cấu trúc bản ghi như sau
sl sinh viên
stt
tên
năm sinh
tổng điểm

em đang code đến đây
// btth.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
fstream f;
struct sv
{
	char hoten[30];
	int namsinh, stt;
	double tongdiem;
};
class SV {
private:
	sv *p;
	int slsv;
public:
	void nhap();
	void ghi();
	void doc();
	void xuat();
};
void SV::nhap() {
	cout << "
hay nhap vao so luong hoc sinh: "; cin >> slsv;
	p = new sv[slsv - 1];
	for (int i = 0; i < slsv; i++) {
		cout << "
sinh vien thu " << i + 1;
		p[i].stt = i + 1;
		cout << "
ten sinh vien: "; cin >> p[i].hoten;
		cout << "
nam sinh: "; cin >> p[i].namsinh;
		cout << "
tong diem: "; cin >> p[i].tongdiem;
	}
}
void SV::ghi() {
	f.open("NHAP.txt", ios::out);
	f << slsv<<endl;
	for (int i = 0; i < slsv; i++) {
		f << p[i].stt << endl << p[i].hoten << endl << p[i].namsinh << endl << p[i].tongdiem << endl;
		f.close;
		delete[] p;
	}
}
void SV::doc() {
	int i = 0;
	f.open("NHAP.txt", ios::in);
	char dat[3];
	string line;
	f >> dat;
	slsv = atoi(dat);
	p = new sv[slsv - 1];
	while (!f.eof()) {
		getline(f, line);
		const char* ht = line.c_str();
		p[i].hoten = ht;
   }
}

em bị mắc ở chỗ em tạo 1 mảng cấp phát động p mới. nhưng bị lỗi không thể thay đổi thuộc tính họ tên của đối tượng p[i] các bro có giải pháp nào giúp mình tìm cách đọc file này với.

Bài liên quan
0