1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include <stdio.h>
#include <string>
#include <iostream>
using std::string;
using std::cin;
using std::cout;
int input();
void output(float);
int main()
{
float result;
int choice, num;
printf("Press 1 to read student information from file\n");
printf("Press 2 to write student information to file\n");
printf("Press 3 to exit\n");
printf("Enter your choice:\n");
choice = input();
switch (choice) {
case 1: {
struct student_t
{
string s;
int a;
int i;
};
student_t p1;
FILE* f = fopen("student_read.txt", "r");
fscanf(f,"%d\n%s\n%d",&p1.i,&p1.s,&p1.a);
fprintf(f,"%d\n%s\n%d",p1.i,p1.s,p1.a);
fclose(f);
break;
}
case 2: {
struct student_t
{
string s;
int a;
int i;
};
student_t p1;
cout << "Student number?"<<'\n';
cin >> p1.i;
cout << "Student name?"<<'\n';
cin.ignore(); // clear the endline that's in the input buffer
std::getline (cin, p1.s);
cout << "Student age?" <<'\n';
cin >> p1.a;
cout << "Student id: " << p1.i << '\n'
<< "Name: " << p1.s << '\n'
<< "Age: " << p1.a << '\n';
FILE* f = fopen("student_write.txt", "w");
fprintf(f,"%d\n%s\n%d",p1.i,p1.s,p1.a);
fclose(f);
}
case 3: {
break;
}
default:
printf("wrong Input\n");
}
return 0;
}
int input()
{
int number;
scanf("%d", &number);
return (number);
}
void output(float number)
{
printf("%f", number);
}
| |