Creating A Read and Write Program

I don't know how to start creating my own program but i need some help about how to create a program that will read and write a list of students..
the program has a switch that has a main menu of: [1]Add [2]Display [3]Update [4]Search [5]Delete [6]Sort [7]Exit... Can someone help me about this program please?



This is what i have been starting with.
I don't know how to create this using procedural process


#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

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
100
101
102
103
104
105
106
107
108
109
110
void main()
{
char choice;
char c[50];
  char id[50];
  char age[50];
  char last[100];
  char first[100];
  char dep[50];
  char add[50];
 b:
 clrscr();
 printf("=====MENU=====");
 printf("\n\n[1] = Add");
 printf("\n[2] = Display");
 printf("\n[3] = Update");
 printf("\n[4] = Search");
 printf("\n[5] = Delete");
 printf("\n[6] = Sort");
 printf("\n[7] = Exit Program");
 printf("\n\nChoice : ");
 scanf("%d",&choice);
 switch (choice)
 {
 case 1:
 clrscr();
 FILE * f;

  f=fopen("test.txt","r+");
    if(f==NULL){
    printf("No Database");
    exit(1);
    }
  else
  {
  a:
  printf("                     ");
  gets(c);
  printf("\nEnter ID No. : ");
  gets(id);
  printf("Enter Age :");
  gets(age);
  printf("Enter Lastname : ");
  gets(last);
  printf("Enter Firstname : ");
  gets(first);
  printf("Enter Department : ");
  gets(dep);
  printf("Enter Address : ");
  gets(add);

  fprintf(f,"%s\t",id);
  fprintf(f,"%s\t",age);
  fprintf(f,"%s\t",last);
  fprintf(f,"%s\t",first);
  fprintf(f,"%s\t",dep);
  fprintf(f,"%s\t",add);
  }
fclose(f);

printf("\n\n\n\n\n\n\t\t");
printf("Add Another Record? ( Y=[yes] or N=[no] ) : ");
fflush(stdin);
scanf("%c",&choice);
switch(choice)
{

case 'y':case'Y':
		goto a;
case  'N':case 'n':
		goto b;
		break;
}

break;

case 2:
clrscr();
   FILE * fp;
   char c;
   fp = fopen("test.txt","r");
   c = getc(f) ;
   printf("ID No.    Age    Lastname    Firstname    Department   Address\n");
   printf("===============================================================\n");
   while (c!= EOF)
   {
		putchar(c);
		c = getc(fp);
   }
   fclose(fp);
	break;

   case 3:
   clrscr();
   FILE * f1;
   char d;
   f1= fopen("test.txt","w");
   d = getc(f) ;
   printf("All Records Successfully Deleted!");
   while (d!= EOF)
   {
		putchar(d);
		d = getc(f1);
   }
   fclose(f1);
	break;

	}
getch();
}
Last edited on
closed account (48T7M4Gy)
Keep up the good work janus. You're doing just fine so far. ;)
closed account (18hRX9L8)
Please wrap your code in code tags so it is easier to read.
Last edited on
i was only in the add and display, and still searching some source about how to program the update, search, delete and, sort... the very hard part is the sorting :( and the procedural method...
You're not doing ok and your program has a number of problems.

First of all, you should use the code format tags to format your code. It's pretty much impossible to understand otherwise. I've formatted it.

You should never have goto in your program. You should try to express that loop in a structured way using a loop statement (like while).

main() returns an int.

Don't declare variables in a switch statement unless they're in a scope, you know, those {} things.

You could start by fixing the goto, then we can look at the other stuff.

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

void main()
{
	char choice;
	char c[50];
	char id[50];
	char age[50];
	char last[100];
	char first[100];
	char dep[50];
	char add[50];

b:
	clrscr();
	printf("=====MENU=====");
	printf("\n\n[1] = Add");
	printf("\n[2] = Display");
	printf("\n[3] = Update");
	printf("\n[4] = Search");
	printf("\n[5] = Delete");
	printf("\n[6] = Sort");
	printf("\n[7] = Exit Program");
	printf("\n\nChoice : ");
	scanf("%d",&choice);
	switch (choice)
	{
	case 1:
		clrscr();
		FILE * f;

		f=fopen("test.txt","r+");
		if(f==NULL){
			printf("No Database");
			exit(1);
		}
		else
		{
		a:
			printf(" ");
			gets(c);
			printf("\nEnter ID No. : ");
			gets(id);
			printf("Enter Age :");
			gets(age);
			printf("Enter Lastname : ");
			gets(last);
			printf("Enter Firstname : ");
			gets(first);
			printf("Enter Department : ");
			gets(dep);
			printf("Enter Address : ");
			gets(add);

			fprintf(f,"%s\t",id);
			fprintf(f,"%s\t",age);
			fprintf(f,"%s\t",last);
			fprintf(f,"%s\t",first);
			fprintf(f,"%s\t",dep);
			fprintf(f,"%s\t",add);
		}
		fclose(f);

		printf("\n\n\n\n\n\n\t\t");
		printf("Add Another Record? ( Y=[yes] or N=[no] ) : ");
		fflush(stdin);
		scanf("%c",&choice);
		switch(choice)
		{
		case 'y':case'Y':
			goto a;
		case 'N':case 'n':
			goto b;
		break;
		}
		break;

	case 2:
		clrscr();
		FILE * fp;
		char c;
		fp = fopen("test.txt","r");
		c = getc(f) ;
		printf("ID No. Age Lastname Firstname Department Address\n");
		printf("===============================================================\n");
		while (c!= EOF)
		{
			putchar(c);
			c = getc(fp);
		}
		fclose(fp);
		break;

	case 3:
		clrscr();
		FILE * f1;
		char d;
		f1= fopen("test.txt","w");
		d = getc(f) ;
		printf("All Records Successfully Deleted!");
		while (d!= EOF)
		{
			putchar(d);
			d = getc(f1);
		}
		fclose(f1);
		break;
	}
	getch();
}
Last edited on
we're using Turbo C... The program runs but the problem is the display, it is good at first run but the second time you add another there was something wrong. the one that you add first is added at the last in your address..
i use goto so that everytime i enter the choices in the end, it goes back to main menu until I press the [7] for exit..

can you make an example at the switch without using goto? please?
Last edited on
can you make an example at the switch without using goto?

Your logic could be something like:
1
2
3
4
5
initialize choice
while choice != quit
	show menu
	get choice
	process choice
I've removed your gotos, they've been replaced with while loops. I've added comments where I've changed code.
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

void main()
{
	char choice = 0;	// initialise choice
	char c[50];
	char id[50];
	char age[50];
	char last[100];
	char first[100];
	char dep[50];
	char add[50];

	while (choice != 7)
	{
		clrscr();
		printf("=====MENU=====");
		printf("\n\n[1] = Add");
		printf("\n[2] = Display");
		printf("\n[3] = Update");
		printf("\n[4] = Search");
		printf("\n[5] = Delete");
		printf("\n[6] = Sort");
		printf("\n[7] = Exit Program");
		printf("\n\nChoice : ");

		scanf("%d",&choice);
		switch (choice)
		{
		case 1:
			while (choice != 7)
			{
				clrscr();
				FILE * f;

				f=fopen("test.txt","r+");
				if(f==NULL){
					printf("No Database");
					break;	// remove exit()
				}
				else
				{
					printf(" ");
					gets(c);
					printf("\nEnter ID No. : ");
					gets(id);
					printf("Enter Age :");
					gets(age);
					printf("Enter Lastname : ");
					gets(last);
					printf("Enter Firstname : ");
					gets(first);
					printf("Enter Department : ");
					gets(dep);
					printf("Enter Address : ");
					gets(add);

					fprintf(f,"%s\t",id);
					fprintf(f,"%s\t",age);
					fprintf(f,"%s\t",last);
					fprintf(f,"%s\t",first);
					fprintf(f,"%s\t",dep);
					fprintf(f,"%s\t",add);
				}
				fclose(f);

				printf("\n\n\n\n\n\n\t\t");
				printf("Add Another Record? ( Y=[yes] or N=[no] ) : ");
				fflush(stdin);
				scanf("%c",&choice);
				if (choice == 'N')
				{
					choice = 7;	// select exit
				}
			}
			break;

		case 2:
			{	// create scope to isolate local variables
				clrscr();
				FILE * fp;
				char c;
				fp = fopen("test.txt","r");
				c = getc(f) ;
				printf("ID No. Age Lastname Firstname Department Address\n");
				printf("===============================================================\n");
				while (c!= EOF)
				{
					putchar(c);
					c = getc(fp);
				}
				fclose(fp);
			}
			break;

		case 3:
			{	// create scope to isolate local variables
				clrscr();
				FILE * f1;
				char d;
				f1= fopen("test.txt","w");
				d = getc(f) ;
				printf("All Records Successfully Deleted!");
				while (d!= EOF)
				{
					putchar(d);
					d = getc(f1);
				}
				fclose(f1);
			}
			break;
		}
	}

	getch();
}
Thank you sir, I tried it and it run but still the second time i put another person, there's still a problem at the end below the address of the second person and also the second person should be below at the first person i've put...

Sir, how about the case 3? It's not about the update..

I've been searching some codes about the update source codes but there are none of it... do you know how to do the codes of update sir?
The code as it stands is too complicated. You're trying to do too many unrelated things in one function. Normally we'd create a function to do just one thing, then call that function when that job is to be performed.

For example, showing and selecting main menu might be one function.

Also, it's helpful if your possible selections are symbols rather than hard coded numbers. There are a number of ways of creating symbolic names, but in C++ an enumerated type is probably the best thing to use here.

Also, this isn't your fault but you're using an ancient version of the language. This is inexcusable given that there are free compilers that support the latest C++11 features.

The code I am posting here hasn't been compiled because I don't have Turbo C++ 3. So be prepared to fix syntax errors and test it yourself.

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>

enum select_t
{
	eAdd = 1, eDisplay, eUpdate, eSearch, eDelete, eSort, eQuit, eAgain
};

select_t ShowAndSelect();
void AddRec();
select_t AddSelect();
void DisplayRec();
void UpdateRec();
void SearchRec();
void DeleteRec();
void SortRec();

int main()
{
	do
	{
		select_t choice = ShowAndSelect();

		switch (choice)
		{
		case eAdd:
			{
				select_t choice = eQuit;

				do
				{
					AddRec();
					choice = ShowAndSelect();
				}
				while (choice == eAgain);
			}
			break;

		case eDisplay:
			DisplayRec();
			break;

		case eUpdate:
			UpdateRec();
			break;
		}
	}
	while (choice != eQuit);

	getch();
}

select_t ShowAndSelect()
{
	clrscr();
	printf("=====MENU=====");
	printf("\n\n[1] = Add");
	printf("\n[2] = Display");
	printf("\n[3] = Update");
	printf("\n[4] = Search");
	printf("\n[5] = Delete");
	printf("\n[6] = Sort");
	printf("\n[7] = Exit Program");
	printf("\n\nChoice : ");

	int choice;
	scanf("%d",&choice);
//	return static_cast<select_t>(choice);	// Don't know if Turbo C++ supports new casts
	return (select_t)choice;
}

void AddRec()
{
	FILE * f = fopen("test.txt","r+");

	if (f==NULL)
	{
		printf("No Database");
	}
	else
	{
		char c[50];
		char id[50];
		char age[50];
		char last[100];
		char first[100];
		char dep[50];
		char add[50];

		printf(" ");
		gets(c);
		printf("\nEnter ID No. : ");
		gets(id);
		printf("Enter Age :");
		gets(age);
		printf("Enter Lastname : ");
		gets(last);
		printf("Enter Firstname : ");
		gets(first);
		printf("Enter Department : ");
		gets(dep);
		printf("Enter Address : ");
		gets(add);

		fprintf(f,"%s\t",id);
		fprintf(f,"%s\t",age);
		fprintf(f,"%s\t",last);
		fprintf(f,"%s\t",first);
		fprintf(f,"%s\t",dep);
		fprintf(f,"%s\t",add);
	}
	fclose(f);
}

select_t AddSelect()
{
	printf("\n\n\n\n\n\n\t\t");
	printf("Add Another Record? ( Y=[yes] or N=[no] ) : ");
	fflush(stdin);

	int choice;
	scanf("%d",&choice);
	return (choice == 'Y') ? eAgain : eQuit;
}

void DisplayRec()
{
	clrscr();
	printf("ID No. Age Lastname Firstname Department Address\n");
	printf("===============================================================\n");

	FILE * fp = fopen("test.txt","r");
	char c = getc(f) ;
	while (c!= EOF)
	{
		putchar(c);
		c = getc(fp);
	}
	fclose(fp);
}

void UpdateRec()
{
	clrscr();
	printf("All Records Successfully Deleted!");

	FILE * f1 = fopen("test.txt","w");
	char d = getc(f) ;
	while (d!= EOF)
	{
		putchar(d);
		d = getc(f1);
	}
	fclose(f1);
}

void SearchRec()
{
}

void DeleteRec()
{
}

void SortRec()
{
}

Last edited on
Hmmm that is why its hard to find out these sources of codes in the internet it's because i am using the ancient programming code... Thank you sir for your help, and the tips you have given me....

Is it possible if we will finish this program in 2 days sir?

Some of them are hard to find in the internet, some are not clearly understandable and some that i found out are full of errors, i tried to fix the errors but it goes back to its main..
There' an error that i can't solve.. The word choice is undefined in this area:

1
2
3
4
5
6
7
8
9
10
11
12
   case eDisplay:
			DisplayRec();
			break;

		case eUpdate:
			UpdateRec();
			break;
		}
	}
	while (choice != eQuit);

	getch();

Look for the scope of choice then .

like that what is x int main ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int x;

int main()
{
   x = 3;
  {
     int x = 2;

    {  
        int x = 1;

       std::cout << x;
    }
  }
return 0;
}
This is what have been written there but the choice is undefined, and still i'm fixing the error why the choice is undefined..

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
int main()
{
	do
	{
		select_t choice = ShowAndSelect();

		switch (choice)
		{
		case eAdd:
			{
				select_t choice = eQuit;

				do
				{
					AddRec();
					choice = ShowAndSelect();
				}
				while (choice == eAgain);
			}
			break;

		case eDisplay:
			DisplayRec();
			break;

		case eUpdate:
			UpdateRec();
			break;
		}
	}
	while (choice != eQuit); // This choice here is undefined, it is correct but dont know why it is undefined //

	getch();


_______________________________________________________________________
Undefined symbol 'choice'
Last edited on
I don't see your enum definition or function prototypes.
Last edited on
The problem with your display code is that you're simply dumping the contents of the file. Since you never write a newilne to the file, there is no newline in the output.

Looking forward, to the search and delete functions, you'll need a way to read individual records from the file. So let's pause a moment and think about how the data should be stored. I suggest that you write each field in a separate line. So the file will like this:
id 1
age 1
last 1
first 1
department 1
address 1
id 2
age 2
last 2
first 2
department 2
address 2
...


Create a class to hold the data. Add a constructor and methods to read/write:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Person {
public:
	Person();
	char c[50];
	char id[50];
	char age[50];
	char last[100];
	char first[100];
	char dep[50];
	char add[50];
	bool read(FILE *fp);
	bool write(FILE *fp);
};


Now write constructor and the read/write methods. The constructor should make each string an empty string.

Continue coding up the stuff to add records to the file and display the file. Once you have that working, here are some hints on search. sort and delete:

- Search is pretty easy: prompt for the name. Then have a loop that reads a record from the file, and if it matches the prompt, prints it out.
- Sort is harder. You'll have to read the entire file into an array of Person instances (see why you needed a class for the person?). Then use std::sort to sort the records. You'll have to write a function that compares records so std::sort knows how to sort them.
- Delete may be the hardest. You can't simply delete data from a file. You'll have to copy the file to a new temporary file. Copy all records EXCEPT the one you want to delete. When you're done, replace the original file with the new one. See if your operating system supports link() and unlink() function calls, or perhaps remove() and rename().

Your previous comments make it sound like you're seaching the internet for code to do this stuff. I don't think that will be productive. You need to learn the language and figure this stuff out yourself.
Thank You Sir Dhayden for the hints...
I'm still learning this kind of stuff. Our Instructor didn't talk about it like that, he just want us to make a program like that using procedural process.
He didn't discuss some of this stuff, we stop only at textfile how to read and write and that's all i know. I'm so poor about this that's why i came here to need some help.
Sorry for the trouble I've made, its my project in the midterm so thanks again for the hints..
I'll try to figure these strings and the others that are part in this program...
Topic archived. No new replies allowed.