đây là 1 trong số các bài tập về nhà của em, anh chị giúp em sửa lỗi bài code c++ này với đây là đề của em,
1, add student new
2, delete a student
3, find a student
ghi vào đầu file
thông tin về sinh viên được lưu trong file input2.txt như sau:
line 1: chứa chức năng và tên sinh viên, mà số sinh viên
các line tiếp theo chứa tên sinh viên và mã số sinh viên
ví dụ
1 john 50--new add
mary 32
peter 34
bài này không được dùng cin, cout, get
em làm được mỗi cái thêm sinh viên nhưng em không biết thêm vào đầu, mà mỗi lần ghi mới nó xóa trắng file rồi ghi cái mới vào thôi ah, còn cái chắc năng tìm em không biết làm sao ah xin anh, chi giúp em với em đã cố gẳng sửa 2 ngày nay nhưng chưa sửa dược ạ,
[IMG]images/smilies/redface.png[/IMG]
Mã:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
void writeToFile(char* name,int choose)
{
ofstream file("input2.txt");
char*name_n;
int number;
name_n=(char*)malloc(100*sizeof(int));
puts("add name student: ");fflush(stdin);
gets(name_n);
puts("add number student(only number): ");
scanf("%d",&number);
file<<choose<<" ";
file<<name_n<<" ";
file<<number<<"
";
file.close();
}
void readFile(char* temp)
{
string line;
ifstream file("input2.txt");
ofstream file_2("MSSV.txt");
if(file.is_open())
{
while(!file.eof())
{
getline(file,line);
cout<<line<<endl;
file_2<<line<<endl;
}
file_2.close();
file.close();
}
else{
puts("unable to open file
");
}
}
void deleteStudent(char* name,int number)
{
ifstream file("input2.txt");
if(file.is_open())
{
while(!file.eof())
{
getline(file,line);
line=" ";
file_2<<line<<endl;
}
file_2.close();
file.close();
}
else{
puts("unable to open file
");
}
file.close();
}
int main(void)
{
char* name;
int choose;
name=(char*)malloc(100*sizeof(char));
puts("1. add a student
2. delete a student in file
3. find a student
4. show all student
5. Exits
");printf("enter choose: ");
scanf("%d",&choose);
switch(choose)
{
case 1:
writeToFile(name,choose);
break;
case 2:
printf("your choose case 2");
break;
case 3:
printf("your choose case 3");
break;
case 4: readFile(name);break;
default: break;
}
return 0;
}
View more random threads:
xã hội vững mạnh, nhu cầu mặc đẹp của con người ngày càng cao. ngành công nghiệp thời trang cũng đang vững mạnh chóng vánh. những nhà máy gia công hàng may mặc chẳng thể đóng góp lặng thầm vào sự...
Tìm xưởng may gia công tại TP Hồ...