simple textfile without using structure

Sep 9, 2015 at 12:41am
hi everyone pls do me a favor i worked for my project using a simple textfile pass by parameter without using a sturcture...the ff. code must like this
add record, update record, search record, delete record. the pls add a code that you like to add, update, search, delete a record? thank you so much guy

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

struct IDDATA {

char ID[50],
Fname[50],
Mname[50],
Lname[50],
course[50],
college[50];

};

void AddNewRecord(FILE * fp, struct IDDATA e, long int IDDATASIZE)
{
struct IDDATA tmp;
clrscr();
printf("ADD NEW RECORD FORM\n");
printf("ID No. : \n ");
gets(e.ID);
printf("First Name : ");
gets(e.Fname);
printf("Middle Name : ");
gets(e.Mname);
printf("Last Name : ");
gets(e.Lname);
printf("Course : ");
gets(e.course);
printf("College : ");
gets(e.college);



fseek(fp,0,SEEK_END);
fwrite(&e,IDDATASIZE,1,fp);

printf("Add Another Record? ( Y=[yes] or N=[no] ) : ");
fflush(stdout);
scanf("%c",&choice);
fclose(fp);
}


void SortID(struct IDDATA temp[], int tempsize)
{
int a, b, c=0;
struct IDDATA tmp;

for (a = 1; a<tempsize ; a++) {
for (b = 0; b < tempsize - 1; b++)
{
if (atoi(temp[b].ID) > atoi(temp[b + 1].ID)) // compares current array and next array has the highest value
{
tmp = temp[b]; // temporarily store highest value to tmp variable
temp[b] = temp[b + 1]; // select next array and store value from next array.
temp[b + 1] = tmp; // sets the current array as has the highest value
}
}
}

for(c=0; c<tempsize; c++)
{
printf("%s",temp[c].ID);
printf("\t%s",temp[c].Fname);
printf("\t\t%s",temp[c].Mname);
printf("\t\t%s",temp[c].Lname);
printf("\t\t%s",temp[c].course);
printf("\t%s\n",temp[c].college);
}

}

void DisplayRecord(FILE * fp, struct IDDATA e, long int IDDATASIZE)
{
struct IDDATA idbase[100];
int idc =0;
clrscr();
rewind(fp);
printf("DISPLAY RECORD\n\n");
printf("ID No.");
printf("\tFirst Name");
printf("\tMiddle Name");
printf("\tLast Name");
printf("\tCourse");
printf("\tCollege\n");

while(fread(&e,IDDATASIZE,1,fp)==1){


idbase[idc] = e;
idc++;
}

SortID(idbase, idc);
fclose(fp);




}



void main()
{

clrscr();
FILE *fp, *ft;

int choice=0;



struct IDDATA e;

long int IDDATASIZE;

fp=fopen("c:/IDBASE.txt","rb+");
if(fp==NULL) {

fp=fopen("c:/IDBASE.txt","wb+");

if(fp==NULL)
{
puts("Cannot open file");
return;
}
}

char number[50];

IDDATASIZE = sizeof(e);


printf("Welcome To Student ID System\n");
printf("1. Add New Record\n");
printf("2. Display Record\n");
printf("3. Update Record\n");
printf("4. Search Record\n");
printf("5. Delete Record\n");
printf("6. Exit\n");
printf("\nEnter Choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1:
AddNewRecord(fp, e, IDDATASIZE);
break;

case 2:
DisplayRecord(fp, e, IDDATASIZE);
break;
}




getch();





}


Sep 9, 2015 at 12:44am
Why void main()? Why no structures is it supposed to be without structure or class?
Sep 9, 2015 at 12:49am
bcoz my instructor want it to be simple . . :(
pls help me
Sep 9, 2015 at 12:57am
I will try it on my compiler then..
Sep 9, 2015 at 1:44am
thank you sir
just update me
Sep 9, 2015 at 2:03am
try 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
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

struct RecordData
{
	unsigned int m_id;
	std::string m_firstName;
	std::string m_middleName;
	std::string m_lastName;
	std::string m_courseName;
	std::string m_collegeName;
	friend std::ostream& operator << (std::ostream& os, const RecordData& record)
	{
		return os 
			<< record.m_id << " "
			<< record.m_firstName << " "
			<< record.m_middleName << " "
			<< record.m_lastName << " "
			<< record.m_courseName << " "
			<< record.m_collegeName << " "
			<< std::endl;
	}
	friend std::istream& operator >> (std::istream& is, RecordData& record)
	{
		return is
			>> record.m_id
			>> record.m_firstName
			>> record.m_middleName
			>> record.m_lastName
			>> record.m_courseName
			>> record.m_collegeName;
	}
};

std::vector<RecordData> g_records;

std::fstream g_fileStream("IDBASE.txt");

//will write records in the file append
void addNewRecord(void)
{
	if (!g_fileStream.is_open())
	{
		g_fileStream.open("IDBASE.txt", std::ios::out | std::ios::app);

		if (!g_fileStream.good())
		{
			std::cout << "could not open this file\n";
			return;
		}
	}
	unsigned int id;
	std::string fName;
	std::string mName;
	std::string lName;
	std::string crsName;
	std::string colName;
	std::cin >> id;
	std::cin >> fName;
	std::cin >> mName;
	std::cin >> lName;
	std::cin >> crsName;
	std::cin >> colName;
	RecordData record = {id,fName,mName,lName,crsName,colName};
	if (g_fileStream.is_open())
	{
		g_fileStream << record;
	}
}
//will read all content of the file and will store it in records
void displayRecords(void)
{
	if (g_fileStream.is_open())
	{
		g_fileStream.close();
		g_fileStream.open("IDBASE.txt", std::ios::in);
		if (!g_fileStream.good())
		{
			std::cout << "could not open this file\n";
			return;
		}
		while (!g_fileStream.eof())
		{
			RecordData record;
			g_fileStream >> record;
			g_records.push_back(record);
		}

		g_fileStream.close();

		for (int i = 0; i < g_records.size(); i)
			std::cout << (const RecordData&)g_records[i ++] ;
	}
}

int main(int argc , char* argv[])
{
	for (;;)
	{
		int choice = 0;
		do
		{
			std::cout
				<< "Welcome To Student ID System\n"
				<< "1. Add New Record\n"
				<< "2. Display Record\n"
				<< "3. Update Record\n"
				<< "4. Search Record\n"
				<< "5. Delete Record\n"
				<< "6. Exit\n"
				<< "\nEnter Choice: ";
			std::cin >> choice;
		} while (choice < 1 || choice > 6);
		switch (choice)
		{
		case 1:
			addNewRecord();
			break;
		case 2:
			displayRecords();
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			break;
		case 6:
			if (g_fileStream.is_open())
				g_fileStream.close();
			std::cout << "Bye\n";
			return EXIT_SUCCESS;
			break;
		default:
			break;
		}
	}
	return EXIT_SUCCESS;
}
Sep 10, 2015 at 3:36pm
thank you sir. .. .but the error is unable to open file string, iostream, vector ,stream, vector
Sep 10, 2015 at 4:31pm
I will send you a private mail for that in C code.
Topic archived. No new replies allowed.