School task

The application prints a menu with the following choices:

Read student information from file
Write student information to file
Exit
The user (and my tests) types 1, then 3 (for example).

Choice 1 does:

Opens the file student_read.txt
Reads in each of the lines
Prints the information using the format from 5.1
Choice 2 does:

Asks the user for the student information (like 5.1)
Writes that information to the student_write.txt file, example below
Choice 3 exits the program

Requirements:
The format of the text file must look like this:

3872187
John Doe
21

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);
}





When i try the run the code i have problem with reading from the text file and write to the text file
Does the file open correctly?
1
2
3
4
5
  FILE* f = fopen("student_read.txt", "r");
  if (f == NULL)
  {
     std::cout << "File did not open correctly.";
  }


Why are you writing the file handling code in C? C++ provides classes and functions to do this. This is becoming a theme in your posts. Stop writing C. Use C++.

Last edited on
With CPP it's kind of looks like this.
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
#include <iostream>
#include <fstream>
#include <string>

int input(void);
void output(float);

int main()
{
	float result;
	int choice, num;
	printf("Press 1 to read student information from infile\n");
	printf("Press 2 to write student information to infile\n");
	printf("Press 3 to exit\n");
	printf("Enter your choice:\n");
	choice = input();

	struct student_t
	{
		std::string s;
		int a;
		int i;
	};

	student_t p1;

	switch (choice)
	{
	case 1:
	{
		std::ifstream infile;
		infile.open("student_read.txt");
		std::string str_a, str_i;
		getline(infile, str_i);
		p1.i = std::stoi(str_i);
		getline(infile, p1.s);
		getline(infile, str_a);
		p1.a = std::stoi(str_a);
		infile.close();

		std::cout << p1.i << std::endl << p1.s << std::endl << p1.a << std::endl;

		break;
	}
	case 2:
	{
		std::cout << "Student number?" << '\n';
		std::cin >> p1.i;
		std::cin.ignore(); // clear the endline that's in the input buffer

		std::cout << "Student name?" << '\n';
		std::getline(std::cin, p1.s);

		std::cout << "Student age?" << '\n';
		std::cin >> p1.a;

		std::cout << "Student id: " << p1.i << '\n'
			<< "Name: " << p1.s << '\n'
			<< "Age: " << p1.a << '\n';

		std::ofstream outfile;
		outfile.open("student_write.txt");
		outfile << p1.i << std::endl << p1.s << std::endl << p1.a << std::endl;
		outfile.close();
		break;
	}
	case 3:
		std::cout << "the program will close...\n";
		break;
	default:
		std::cerr << "Wrong input\n";
	}

	std::cout << "To continue press ENTER...";
	std::cin.get();
	return 0;
}

int input(void)
{
	int number;
	std::cin >> number;
	std::cin.ignore();
	return (number);
}

void output(float number)
{
	std::cout << number;
}


C is about 3 times faster than C++, but if you write a C++ code use the C++ libs i think.
Topic archived. No new replies allowed.