Thread: file handling using index file

  1. #1
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69

    file handling using index file

    hello everyone

    i have an assignment to complete as soon as possible. It's something like:
    1.create (or open existing) Index and Record files.
    2.User can enter a record with roll no, name and marks.
    3.store this record at the end of file and add roll no as a key to record file.
    4.After each insertion, all keys(roll no) of Index file should be sorted in ascending order and while swapping two keys, the corresponding record also must be swapped.
    5.Perform display, Search, Modified, Delete operations using index file.

    I have done a framework of it but first want to perform search operation and there is problem. ftell() tells that roll nos are stored in index file at 2,6,10,14..... locations but
    the sort() function checks locations 0,2,4....2,4,6....4,6,8...

    can anyone tell me where i'm wrong? any other modifications?

    thanks to all in advance

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    
    typedef struct student 
    {
    	int roll;
    	char name[20];
    	float marks;
    
    } stud;
    
    void sort(int n)
    {
    	FILE *fp_index, *fp_record;
    	int i, j, r1, r2, r3;
    	stud s1, s2, s3;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    
    	for (i = 0; i < n; i++)
    	{
    		for (j = 0; j < n; j++)
    		{
    			fseek(fp_record, sizeof(s1) * j, SEEK_SET);
    			fseek(fp_index, sizeof(int) * j, SEEK_SET);
    
    			printf("\n\nFtell : %d", ftell(fp_index));
    
    			fread(&r1, sizeof(int), 1, fp_index);
    
    			printf("\n\nFtell : %d", ftell(fp_index));
    
    			fread(&r2, sizeof(int), 1, fp_index);
    
    			printf("\n\nFtell : %d", ftell(fp_index));
    
    			if (r1 > r2)	//swap record and index
    			{
    				r3 = r1;
    				r1 = r2;
    				r2 = r3;
    				fseek(fp_record, (sizeof(s1)) * j, SEEK_SET);
    				fwrite(&r1, sizeof(int), 1, fp_record);
    				fwrite(&r2, sizeof(int), 1, fp_record);
    			}
    		}
    	}
    
    	fclose(fp_index);
    	fclose(fp_record);
    
    }
    
    
    void add(stud *s1)
    {
    	float temp;
    	int n = 0;
    	FILE *fp_index, *fp_record;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	printf("\n\nEnter roll no: ");
    	scanf("%d", &s1->roll);
    	printf("\n\nEnter Name: ");
    	scanf("%s", s1->name);
    	printf("\n\nEnter marks: ");
    	scanf("%f", &temp);
    	s1->marks = temp;
    
    	fseek(fp_record, sizeof(*s1), SEEK_END);
    
    	if (!(fwrite(s1, sizeof(*s1), 1, fp_record)))
    		printf("\n\nCould NOT add !!!");
    	else
    	{
    		fseek(fp_index, sizeof(int), SEEK_END);
    		fwrite(&(s1->roll), sizeof(int), 1, fp_index);
    	}
    	rewind(fp_index);
    	while ((fread(s1, sizeof(int), 1, fp_index)))
    		n++;
    
    	n = n / sizeof(int);
    
    	fclose(fp_index);
    	fclose(fp_record);
    
    	if (n > 1)
    		sort(n);
    
    }
    
    void edit(stud *s2, int r_n)
    {
    	FILE *fp;
    	float temp;
    
    	fp = fopen("g.txt", "r+b");
    	fseek(fp, sizeof(*s2) * (r_n - 1), 0);
    	fread(s2, sizeof(*s2), 1, fp);
    
    	printf("\n\nRoll :%d", s2->roll);
    	printf("\n\nName :%s", s2->name);
    	printf("\n\nMarks :%f", s2->marks);
    
    	printf("\n\n\nEnter New Name: ");
    	scanf("%s", s2->name);
    	printf("\n\nEnter New marks: ");
    	scanf("%f", &temp);
    	s2->marks = temp;
    
    	fseek(fp, sizeof(*s2) * (r_n - 1), 0);
    	if (!(fwrite(s2, sizeof(*s2), 1, fp)))
    		printf("\n\nCould NOT modify !!!");
    
    	fclose(fp);
    	printf("\n\n");
    }
    
    void search(stud *s2, int r_n)
    {
    	int r, flag = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    			break;
    		}
    	}
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_record, sizeof(*s2) * (ftell(fp_index)), 0);
    		fread(s2, sizeof(*s2), 1, fp_record);
    
    		printf("\n\nRoll :%d", s2->roll);
    		printf("\n\nName :%s", s2->name);
    		printf("\n\nMarks :%f", s2->marks);
    	}
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    
    }
    
    void display(stud *s2)
    {
    	FILE *fp;
    	int flag = 1;
    	fp = fopen("record.txt", "rb");
    
    	while (fread(s2, sizeof(*s2), 1, fp))
    	{
    		if ((s2->roll) != 0)
    		{
    			if (flag == 1)
    				printf("\n\nRoll No	 Name	 marks");
    			flag = 0;
    			printf("\n\n%d", s2->roll);
    			printf("	%s", s2->name);
    			printf("	%f", s2->marks);
    		}
    	}
    	if (flag == 1)
    		printf("\n\nNo record found !!!");
    	printf("\n\n");
    	fclose(fp);
    }
    
    void delet(stud *s2, int r_n)
    {
    	FILE *temp_fp, *fp;
    
    	fp = fopen("g.txt", "r+b");
    	temp_fp = fopen("temp.txt", "r+b");
    
    	while (fread(s2, sizeof(*s2), 1, fp))
    	{
    		if ((s2->roll) != r_n)
    		{
    			fseek(temp_fp, sizeof(*s2) * ((s2->roll) - 1), 0);
    			fwrite(s2, sizeof(*s2), 1, temp_fp);
    		}
    	}
    
    	fclose(fp);
    	unlink("g.txt");
    	rename("temp.txt", "g.txt");
    
    	fclose(temp_fp);
    	printf("\n\n");
    }
    
    
    int main()
    {
    	FILE *fp;
    	stud s1, s2;
    	int choice, roll_no;
    
    	clrscr();
    
    	fp = fopen("record.txt", "a+b");
    	fclose(fp);
    	fp = fopen("temp.txt", "a+b");
    	fclose(fp);
    	fp = fopen("index.txt", "a+b");
    	fclose(fp);
    
    	while (1)
    	{
    		printf("\n\n1:Add	2:Display	3:Search	4:Edit	     5:Delete       6:Exit \n\n");
    		scanf("%d", &choice);
    		switch (choice)
    		{
    			case 1:
    				add(&s1);
    
    				break;
    			case 2:
    
    				display(&s2);
    
    				break;
    			case 3:
    				printf("\n\nEnter Roll No To Search Record : ");
    				scanf("%d", &roll_no);
    				search(&s2, roll_no);
    
    				break;
    			case 4:
    				printf("\n\nEnter Roll No To Edit Record : ");
    				scanf("%d", &roll_no);
    
    				edit(&s2, roll_no);
    
    				break;
    			case 5:
    				printf("\n\nEnter Roll No To Delete Record : ");
    				scanf("%d", &roll_no);
    
    				delet(&s2, roll_no);
    
    				break;
    			case 6:
    				exit(0);
    
    			default:
    				printf("\n\nInvalid choice !!!");
    		}
    	}
    
    	getch();
    	return 0;
    }

  2. #2
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    after lots of efforts i have done add() and search() but search() not working still. delete() not modified for this project yet. any kind of suggestion is welcome.


    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    
    typedef struct student {
    	int roll;
    	char name[20];
    	float marks;
    
    } stud;
    
    void sort(int n)
    {
    	FILE *fp_index, *fp_record;
    	int i, j, r1, r2, r3;
    	stud s1, s2, s3;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    
    	for (i = 0; i < n - 1; i++)
    	{
    		for (j = 0; j < n - 1; j++)
    		{
    
    			fseek(fp_record, sizeof(s1) * j, SEEK_SET);
    			fseek(fp_index, sizeof(int) * j, SEEK_SET);
    
    			fread(&r1, sizeof(int), 1, fp_index);
    
    			fread(&r2, sizeof(int), 1, fp_index);
    
    			if (r1 > r2)	//swap record and index
    			{
    				fread(&s1, sizeof(s1), 1, fp_record);
    				fread(&s2, sizeof(s2), 1, fp_record);
    
    				fseek(fp_record, (sizeof(s1)) * j, SEEK_SET);
    				fseek(fp_index, (sizeof(int)) * j, SEEK_SET);
    
    
    				fwrite(&r2, sizeof(int), 1, fp_index);
    				fwrite(&r1, sizeof(int), 1, fp_index);
    
    				fwrite(&s2, sizeof(s1), 1, fp_record);
    				fwrite(&s1, sizeof(s1), 1, fp_record);
    			}
    		}
    	}
    
    	fclose(fp_index);
    	fclose(fp_record);
    
    }
    
    void add()
    {
    	int n = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    	stud s1;
    
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	printf("\n\nEnter roll no: ");
    	scanf("%d", &s1.roll);
    	printf("\nEnter Name: ");
    	scanf("%s", s1.name);
    	printf("\nEnter marks: ");
    	scanf("%f", &temp);
    	s1.marks = temp;
    
    	fseek(fp_record, 0, SEEK_END);
    
    	if (fwrite(&s1, sizeof(s1), 1, fp_record))
    	{
    		fseek(fp_index, 0, SEEK_END);
    		fwrite(&(s1.roll), sizeof(int), 1, fp_index);
    	}
    	else
    	{
    		printf("\n\nCould NOT add !!!");
    	}
    	rewind(fp_index);
    	while ((fread(&s1, sizeof(int), 1, fp_index)))
    		n++;
    
    	fclose(fp_index);
    	fclose(fp_record);
    
    	if (n > 1)
    		sort(n);
    
    }
    
    void edit(int r_n)
    {
    	int r, loc, rec, flag = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    	stud s2;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    
    			break;
    		}
    	}
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
    		loc = ftell(fp_index);
    
    		fseek(fp_record, loc / sizeof(int) * sizeof(s2), SEEK_SET);
    
    		fread(&s2, sizeof(s2), 1, fp_record);
    
    		printf("\n\nRoll :%d", s2.roll);
    		printf("\n\nName :%s", s2.name);
    		printf("\n\nMarks :%f", s2.marks);
    
    		printf("\nEnter New Name : ");
    		scanf("%s", &s2.name);
    		printf("\nEnter New marks : ");
    		scanf("%f", &temp);
    		s2.marks = temp;
    
    		fseek(fp_record, (-1) * sizeof(s2), SEEK_CUR);
    
    		if(!(fwrite(&s2, sizeof(s2), 1, fp_record)));
    		printf("\n\nCould NOT modified !!!");
    
    	}
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    
    }
    
    void search(int r_n)
    {
    	int r, loc, flag = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    	stud s2;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    
    			break;
    		}
    	}
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
    		loc = ftell(fp_index);
    		fseek(fp_record, loc / sizeof(int) * sizeof(s2), SEEK_SET);
    		fread(&s2, sizeof(s2), 1, fp_record);
    
    		printf("\n\nRoll :%d", s2.roll);
    		printf("\n\nName :%s", s2.name);
    		printf("\n\nMarks :%f", s2.marks);
    	}
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    }
    
    void display()
    {
    	FILE *fp;
    	int flag = 1;
    	stud s2;
    	fp = fopen("record.txt", "rb");
    
    	while (fread(&s2, sizeof(s2), 1, fp))
    	{
    		if ((s2.roll) != 0)
    		{
    			if (flag == 1)
    				printf("\n\nRoll No	 Name	 marks");
    			flag = 0;
    			printf("\n\n%d", s2.roll);
    			printf("	%s", s2.name);
    			printf("	%f", s2.marks);
    		}
    	}
    	if (flag == 1)
    		printf("\n\nNo record found !!!");
    	printf("\n\n");
    	fclose(fp);
    }
    
    void delet(stud *s2, int r_n)
    {
    	FILE *temp_fp, *fp;
    
    	fp = fopen("g.txt", "r+b");
    	temp_fp = fopen("temp.txt", "r+b");
    
    	while (fread(s2, sizeof(*s2), 1, fp))
    	{
    		if ((s2->roll) != r_n)
    		{
    			fseek(temp_fp, sizeof(*s2) * ((s2->roll) - 1), 0);
    			fwrite(s2, sizeof(*s2), 1, temp_fp);
    		}
    	}
    
    	fclose(fp);
    	unlink("g.txt");
    	rename("temp.txt", "g.txt");
    
    	fclose(temp_fp);
    	printf("\n\n");
    }
    
    
    int main()
    {
    	FILE *fp;
    	stud s1, s2;
    	int choice, roll_no;
    
    	clrscr();
    
    	fp = fopen("record.txt", "a+b");
    	fclose(fp);
    	fp = fopen("temp.txt", "a+b");
    	fclose(fp);
    	fp = fopen("index.txt", "a+b");
    	fclose(fp);
    
    	while (1)
    	{
    		printf("\n\n1:Add	2:Display	3:Search	4:Edit	     5:Delete       6:Exit \n\n");
    		scanf("%d", &choice);
    		switch (choice)
    		{
    			case 1:
    				add();
    
    				break;
    			case 2:
    
    				display();
    
    				break;
    			case 3:
    				printf("\n\nEnter Roll No To Search Record : ");
    				scanf("%d", &roll_no);
    				search(roll_no);
    
    				break;
    			case 4:
    				printf("\n\nEnter Roll No To Edit Record : ");
    				scanf("%d", &roll_no);
    
    				edit(roll_no);
    
    				break;
    			case 5:
    				printf("\n\nEnter Roll No To Delete Record : ");
    				scanf("%d", &roll_no);
    
    				delet(&s2, roll_no);
    
    				break;
    			case 6:
    				exit(0);
    
    			default:
    				printf("\n\nInvalid choice !!!");
    		}
    	}
    
    	getch();
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    but search() not working still
    This does not tell us very much. What is wrong with your search function? Be specific.

    delete() not modified for this project yet
    Does this mean that you want to finish your search function first. Or is there a problem with this function also?

    Jim

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Actually, I ran your code and the search function seemed to work just fine for me. What do you think is wrong with it (be very specific like Jim asked)?

    There is one small error I noticed in the edit() function though. Near the bottom, you have:
    Code:
            if(!(fwrite(&s2, sizeof(s2), 1, fp_record)));
            printf("\n\nCould NOT modified !!!");
    Drop that semicolon to avoid getting a "Could NOT modified !!!" error when the record was successfully modified.

  5. #5
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    thank you and i also found search() working properly.
    Now i'm trying edit() and using ftell() after every use of fseek(), fread() and fwrite(). But at line commented with PROBLEM, ftell(fp_record) gives 1 which should be 0 at that point because fp_record is not changed till then. i will look delete() after edit() is completed. and really thanks for help

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    
    typedef struct student {
    	int roll;
    	char name[20];
    	float marks;
    
    } stud;
    
    void sort(int n)
    {
    	FILE *fp_index, *fp_record;
    	int i, j, r1, r2, r3;
    	stud s1, s2, s3;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    
    	for (i = 0; i < n - 1; i++)
    	{
    		for (j = 0; j < n - 1; j++)
    		{
    
    			fseek(fp_record, sizeof(s1) * j, SEEK_SET);
    			fseek(fp_index, sizeof(int) * j, SEEK_SET);
    
    			fread(&r1, sizeof(int), 1, fp_index);
    
    			fread(&r2, sizeof(int), 1, fp_index);
    
    			if (r1 > r2)	//swap record and index
    			{
    				fread(&s1, sizeof(s1), 1, fp_record);
    				fread(&s2, sizeof(s2), 1, fp_record);
    
    				fseek(fp_record, (sizeof(s1)) * j, SEEK_SET);
    				fseek(fp_index, (sizeof(int)) * j, SEEK_SET);
    
    
    				fwrite(&r2, sizeof(int), 1, fp_index);
    				fwrite(&r1, sizeof(int), 1, fp_index);
    
    				fwrite(&s2, sizeof(s1), 1, fp_record);
    				fwrite(&s1, sizeof(s1), 1, fp_record);
    			}
    		}
    	}
    
    	fclose(fp_index);
    	fclose(fp_record);
    
    }
    
    void add()
    {
    	int n = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    	stud s1;
    
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	printf("\n\nEnter roll no: ");
    	scanf("%d", &s1.roll);
    	printf("\nEnter Name: ");
    	scanf("%s", s1.name);
    	printf("\nEnter marks: ");
    	scanf("%f", &temp);
    	s1.marks = temp;
    
    	fseek(fp_record, 0, SEEK_END);
    
    	if (fwrite(&s1, sizeof(s1), 1, fp_record))
    	{
    		fseek(fp_index, 0, SEEK_END);
    		fwrite(&(s1.roll), sizeof(int), 1, fp_index);
    	}
    	else
    	{
    		printf("\n\nCould NOT add !!!");
    	}
    	rewind(fp_index);
    	while ((fread(&s1, sizeof(int), 1, fp_index)))
    		n++;
    
    	fclose(fp_index);
    	fclose(fp_record);
    
    	if (n > 1)
    		sort(n);
    
    }
    
    void edit(int r_n)
    {
    	int r, loc, rec, flag = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    	stud s2;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	printf("\nindex  %d		record  %d",ftell(fp_index),ftell(fp_record));
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    
    			break;
    		}
    		printf("\nindex  %d		record  %d",ftell(fp_index),ftell(fp_record));                  //PROBLEM
    
    	}
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
    
    		printf("\nindex  %d		record  %d",ftell(fp_index),ftell(fp_record));
    
    		loc=ftell(fp_index);
    		fseek(fp_record, loc / sizeof(int) * sizeof(s2), SEEK_SET);
    
    		printf("\nindex  %d		record  %d",ftell(fp_index),ftell(fp_record));
    
    		fread(&s2, sizeof(s2), 1, fp_record);
    
    		printf("\nindex  %d		record  %d",ftell(fp_index),ftell(fp_record));
    
    		printf("\n\nRoll :%d", s2.roll);
    		printf("\n\nName :%s", s2.name);
    		printf("\n\nMarks :%f", s2.marks);
    
    		printf("\nEnter New Name : ");
    		scanf("%s", &s2.name);
    		printf("\nEnter New marks : ");
    		scanf("%f", &temp);
    		s2.marks = temp;
    
    		fseek(fp_record, (-1) * sizeof(s2), SEEK_CUR);
    
    		printf("\nindex  %d		record  %d",ftell(fp_index),ftell(fp_record));
    
    		if(!(fwrite(&s2, sizeof(s2), 1, fp_record)))
    			printf("\n\nCould NOT modified !!!");
    
    		printf("\nindex  %d		record  %d",ftell(fp_index),ftell(fp_record));
    
    	}
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    
    }
    
    void search(int r_n)
    {
    	int r, loc, flag = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    	stud s2;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    
    			break;
    		}
    	}
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
    		loc = ftell(fp_index);
    		fseek(fp_record, loc / sizeof(int) * sizeof(s2), SEEK_SET);
    		fread(&s2, sizeof(s2), 1, fp_record);
    
    		printf("\n\nRoll :%d", s2.roll);
    		printf("\n\nName :%s", s2.name);
    		printf("\n\nMarks :%f", s2.marks);
    	}
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    }
    
    void display()
    {
    	FILE *fp;
    	int flag = 1;
    	stud s2;
    	fp = fopen("record.txt", "rb");
    
    	while (fread(&s2, sizeof(s2), 1, fp))
    	{
    		if ((s2.roll) != 0)
    		{
    			if (flag == 1)
    				printf("\n\nRoll No	 Name	 marks");
    			flag = 0;
    			printf("\n\n%d", s2.roll);
    			printf("	%s", s2.name);
    			printf("	%f", s2.marks);
    		}
    	}
    	if (flag == 1)
    		printf("\n\nNo record found !!!");
    	printf("\n\n");
    	fclose(fp);
    }
    
    void delet(stud *s2, int r_n)
    {
    	FILE *temp_fp, *fp;
    
    	fp = fopen("g.txt", "r+b");
    	temp_fp = fopen("temp.txt", "r+b");
    
    	while (fread(s2, sizeof(*s2), 1, fp))
    	{
    		if ((s2->roll) != r_n)
    		{
    			fseek(temp_fp, sizeof(*s2) * ((s2->roll) - 1), 0);
    			fwrite(s2, sizeof(*s2), 1, temp_fp);
    		}
    	}
    
    	fclose(fp);
    	unlink("g.txt");
    	rename("temp.txt", "g.txt");
    
    	fclose(temp_fp);
    	printf("\n\n");
    }
    
    
    int main()
    {
    	FILE *fp;
    	stud s1, s2;
    	int choice, roll_no;
    
    	clrscr();
    
    	fp = fopen("record.txt", "a+b");
    	fclose(fp);
    	fp = fopen("temp.txt", "a+b");
    	fclose(fp);
    	fp = fopen("index.txt", "a+b");
    	fclose(fp);
    
    	while (1)
    	{
    		printf("\n\n1:Add	2:Display	3:Search	4:Edit	     5:Delete       6:Exit \n\n");
    		scanf("%d", &choice);
    		switch (choice)
    		{
    			case 1:
    				add();
    
    				break;
    			case 2:
    
    				display();
    
    				break;
    			case 3:
    				printf("\n\nEnter Roll No To Search Record : ");
    				scanf("%d", &roll_no);
    				search(roll_no);
    
    				break;
    			case 4:
    				printf("\n\nEnter Roll No To Edit Record : ");
    				scanf("%d", &roll_no);
    
    				edit(roll_no);
    
    				break;
    			case 5:
    				printf("\n\nEnter Roll No To Delete Record : ");
    				scanf("%d", &roll_no);
    
    				delet(&s2, roll_no);
    
    				break;
    			case 6:
    				exit(0);
    
    			default:
    				printf("\n\nInvalid choice !!!");
    		}
    	}
    
    	getch();
    	return 0;
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    When I compile the code for edit() I get these warnings:

    /main.c||In function ‘edit’:|
    /main.c|21|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’|
    /main.c|21|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long int’|
    /main.c|31|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’|
    /main.c|31|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long int’|
    /main.c|42|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’|
    /main.c|42|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long int’|
    /main.c|47|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’|
    /main.c|47|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long int’|
    /main.c|51|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’|
    /main.c|51|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long int’|
    /main.c|58|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’|
    /main.c|65|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’|
    /main.c|65|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long int’|
    /main.c|70|warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’|
    /main.c|70|warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long int’|
    /main.c|13|warning: unused variable ‘rec’|
    You should fix these warnings.


    Jim

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Again, I couldn't replicate your problem. I got 0 as expected. One other small error I forgot to mention before:
    Code:
    scanf("%s", &s2.name);
    Drop that &. s2.name gives you the address scanf needs to store the variable.

    EDIT: To follow up Jim's post, you should always make sure your code compiles without warnings. It will help you catch things that might cause real problems (like your scanf call), instead of them getting lost in a sea of largely harmless warnings.
    Last edited by anduril462; 01-25-2011 at 12:50 PM.

  8. #8
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    thanks to everyone for guide.

    All the functions are working fine without warning on PellesC(64 bit) except delet(). when I wants to delete a record from file, i shifts all records after that one to left side. Now the problem is when i shifts all records to left side, the last record remains there thus creating two same records at the end.

    Don't have idea what to do. please suggest few methods to handle this problem.

    thank you once again.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    There is no standard way that I am aware of, but you do have a few options:
    1. Lazy delete - don't slide records down, just flag the record as unused.
    2. Copy the valid records to a new file, delete the old and rename the new one.
    3. If you're using a POSIX compliant system, look at the truncate or ftruncate funtions.
    4. If you're using Windows, look at _chsize.

  10. #10
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    oops.. i'm sorry. I forgot to post new code. here it is

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    
    typedef struct student {
    	int roll;
    	char name[20];
    	float marks;
    } stud;
    
    void sort(int n)
    {
    	FILE *fp_index, *fp_record;
    	int i, j, r1, r2;
    	stud s1, s2;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	for (i = 0; i < n - 1; i++)
    	{
    		for (j = 0; j < n - 1; j++)
    		{
    			fseek(fp_record, sizeof(s1) * j, SEEK_SET);
    			fseek(fp_index, sizeof(int) * j, SEEK_SET);
    
    			fread(&r1, sizeof(int), 1, fp_index);
    
    			fread(&r2, sizeof(int), 1, fp_index);
    
    			if (r1 > r2)	//swap record and index
    			{
    				fread(&s1, sizeof(s1), 1, fp_record);
    				fread(&s2, sizeof(s2), 1, fp_record);
    
    				fseek(fp_record, (sizeof(s1)) * j, SEEK_SET);
    				fseek(fp_index, (sizeof(int)) * j, SEEK_SET);
    
    				fwrite(&r2, sizeof(int), 1, fp_index);
    				fwrite(&r1, sizeof(int), 1, fp_index);
    
    				fwrite(&s2, sizeof(s1), 1, fp_record);
    				fwrite(&s1, sizeof(s1), 1, fp_record);
    			}
    		}
    	}
    
    	fclose(fp_index);
    	fclose(fp_record);
    }
    
    void add(stud *s1)
    {
    	int n = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	printf("\n\nEnter roll no: ");
    	scanf("%d", &s1->roll);
    	printf("\nEnter Name: ");
    	scanf("%s", s1->name);
    	printf("\nEnter marks: ");
    	scanf("%f", &temp);
    	s1->marks = temp;
    
    	fseek(fp_record, 0, SEEK_END);
    
    	if (fwrite(s1, sizeof(*s1), 1, fp_record))
    	{
    		fseek(fp_index, 0, SEEK_END);
    		fwrite(&(s1->roll), sizeof(int), 1, fp_index);
    	}
    	else
    	{
    		printf("\n\nCould NOT add !!!");
    	}
    	rewind(fp_index);
    	while ((fread(s1, sizeof(int), 1, fp_index)))
    		n++;
    
    	fclose(fp_index);
    	fclose(fp_record);
    
    	if (n > 1)
    		sort(n);
    }
    
    void edit(stud *s2, int r_n)
    {
    	int r, loc, flag = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    
    			break;
    		}
    	}
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
    		loc = ftell(fp_index);
    
    		fseek(fp_record, loc / sizeof(int) * sizeof(*s2), SEEK_SET);
    
    		fread(s2, sizeof(*s2), 1, fp_record);
    
    		printf("\n\nRoll :%d", s2->roll);
    		printf("\n\nName :%s", s2->name);
    		printf("\n\nMarks :%f", s2->marks);
    
    		printf("\nEnter New Name : ");
    		scanf("%s", &(*s2->name));
    		printf("\nEnter New marks : ");
    		scanf("%f", &temp);
    		(s2->marks) = temp;
    
    		fseek(fp_record, (-1) * sizeof(*s2), SEEK_CUR);
    
    		fwrite(s2, sizeof(*s2), 1, fp_record);
    	}
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    }
    
    void search(stud *s2, int r_n)
    {
    	int r, loc, flag = 0;
    
    	FILE *fp_index, *fp_record;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    
    			break;
    		}
    	}
    
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
    		loc = ftell(fp_index);
    		fseek(fp_record, loc / sizeof(int) * sizeof(*s2), SEEK_SET);
    		fread(s2, sizeof(*s2), 1, fp_record);
    
    		printf("\n\nRoll :%d", s2->roll);
    		printf("\n\nName :%s", s2->name);
    		printf("\n\nMarks :%f", s2->marks);
    	}
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    }
    
    void delet(stud *s2, int r_n)
    {
    	int r, loc, flag = 0;
    	char ch;
    
    	FILE *fp_index, *fp_record;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    
    			break;
    		}
    	}
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
    		loc = ftell(fp_index);
    
    		fseek(fp_record, loc / sizeof(int) * sizeof(*s2), SEEK_SET);
    		fread(s2, sizeof(*s2), 1, fp_record);
    
    		printf("\n\nRoll :%d", s2->roll);
    		printf("\n\nName :%s", s2->name);
    		printf("\n\nMarks :%f", s2->marks);
    
    		printf("\n\nDo you want to delete this record ? y/n  ");
    		fflush(stdin);
    		scanf("%c", &ch);
    
    		if (ch == 'n' || ch == 'N')
    			return;
    
    		fseek(fp_index, sizeof(int), SEEK_CUR);	//  delete index value
    		while (fread(&r, sizeof(int), 1, fp_index))
    		{
    			fseek(fp_index, (-2) * sizeof(int), SEEK_CUR);
    			fwrite(&r, sizeof(int), 1, fp_index);
    			fseek(fp_index, sizeof(int), SEEK_CUR);
    		}
    
    		while (fread(s2, sizeof(*s2), 1, fp_record))
    		{
    			fseek(fp_record, (-2) * sizeof(*s2), SEEK_CUR);
    			fwrite(s2, sizeof(*s2), 1, fp_record);
    			fseek(fp_record, sizeof(*s2), SEEK_CUR);
    		}
    	}
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    }
    
    void display(stud *s2)
    {
    	FILE *fp;
    	int flag = 1, ch, r;
    
    	printf("\n\n1:Index File \n\n2:Record File  ");
    	scanf("%d", &ch);
    
    	switch (ch)
    	{
    		case 1:
    
    			fp = fopen("index.txt", "rb");
    
    			while (fread(&r, sizeof(int), 1, fp))
    			{
    				flag = 0;
    				printf("\n%d", r);
    			}
    
    			break;
    
    		case 2:
    
    			fp = fopen("record.txt", "rb");
    
    			while (fread(s2, sizeof(*s2), 1, fp))
    			{
    				if ((s2->roll) != 0)
    				{
    					if (flag == 1)
    						printf("\n\nRoll No	 Name	 marks");
    					flag = 0;
    					printf("\n\n%d", s2->roll);
    					printf("	%s", s2->name);
    					printf("	%f", s2->marks);
    				}
    			}
    
    			break;
    
    		default:
    
    			printf("\n\nInvalid choice !!!");
    	}
    
    	if (flag == 1)
    		printf("\n\nNo record found !!!");
    	printf("\n\n");
    	fclose(fp);
    }
    
    int main()
    {
    	FILE *fp;
    	stud s1, s2;
    	int choice, roll_no;
    
    	fp = fopen("record.txt", "a+b");
    	fclose(fp);
    
    	fp = fopen("index.txt", "a+b");
    	fclose(fp);
    
    	while (1)
    	{
    		printf("\n\n1:Add	2:Display	3:Search	4:Edit	     5:Delete       6:Exit \n\n");
    		scanf("%d", &choice);
    		switch (choice)
    		{
    			case 1:
    				add(&s1);
    
    				break;
    			case 2:
    
    				display(&s2);
    
    				break;
    			case 3:
    				printf("\n\nEnter Roll No To Search Record : ");
    				scanf("%d", &roll_no);
    				search(&s2, roll_no);
    
    				break;
    			case 4:
    				printf("\n\nEnter Roll No To Edit Record : ");
    				scanf("%d", &roll_no);
    
    				edit(&s2, roll_no);
    
    				break;
    			case 5:
    				printf("\n\nEnter Roll No To Delete Record : ");
    				scanf("%d", &roll_no);
    
    				delet(&s2, roll_no);
    
    				break;
    			case 6:
    				exit(0);
    
    			default:
    				printf("\n\nInvalid choice !!!");
    		}
    	}
    
    	return 0;
    }

  11. #11
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    ok. here i'm done. thank you very much anduril462.

    _chsize() worked great for me on pellesC 64bit.

    But the ........ is that i have to make it compatible with turbo c++ because we have this one in our labs.

    i have changed few functions which are there in turbo c++

    _fileno to fileno
    _filelength to filelength
    _chsize to chsize

    but then edit() and delete() functions are NOT working. I know that very few will be having this kind of compiler, but however i have to do this.

    anduril462 thanks again.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<io.h>
    
    typedef struct student 
    {
    	int roll;
    	char name[20];
    	float marks;
    } stud;
    
    void sort(int n)
    {
    	FILE *fp_index, *fp_record;
    	int i, j, r1, r2;
    	stud s1, s2;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	for (i = 0; i < n - 1; i++)
    	{
    		for (j = 0; j < n - 1; j++)
    		{
    			fseek(fp_record, sizeof(s1) * j, SEEK_SET);
    			fseek(fp_index, sizeof(int) * j, SEEK_SET);
    
    			fread(&r1, sizeof(int), 1, fp_index);
    
    			fread(&r2, sizeof(int), 1, fp_index);
    
    			if (r1 > r2)	//swap record and index
    			{
    				fread(&s1, sizeof(s1), 1, fp_record);
    				fread(&s2, sizeof(s2), 1, fp_record);
    
    				fseek(fp_record, (sizeof(s1)) * j, SEEK_SET);
    				fseek(fp_index, (sizeof(int)) * j, SEEK_SET);
    
    				fwrite(&r2, sizeof(int), 1, fp_index);
    				fwrite(&r1, sizeof(int), 1, fp_index);
    
    				fwrite(&s2, sizeof(s1), 1, fp_record);
    				fwrite(&s1, sizeof(s1), 1, fp_record);
    			}
    		}
    	}
    
    	fclose(fp_index);
    	fclose(fp_record);
    }
    
    void add(stud *s1)
    {
    	int n = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	printf("\n\nEnter roll no: ");
    	scanf("%d", &s1->roll);
    	printf("\nEnter Name: ");
    	scanf("%s", s1->name);
    	printf("\nEnter marks: ");
    	scanf("%f", &temp);
    	s1->marks = temp;
    
    	fseek(fp_record, 0, SEEK_END);
    
    	if (fwrite(s1, sizeof(*s1), 1, fp_record))
    	{
    		fseek(fp_index, 0, SEEK_END);
    		fwrite(&(s1->roll), sizeof(int), 1, fp_index);
    	}
    	else
    	{
    		printf("\n\nCould NOT add !!!");
    	}
    	rewind(fp_index);
    	while ((fread(s1, sizeof(int), 1, fp_index)))
    		n++;
    
    	fclose(fp_index);
    	fclose(fp_record);
    
    	if (n > 1)
    		sort(n);
    }
    
    void edit(stud *s2, int r_n)
    {
    	int r, loc, flag = 0;
    	float temp;
    	FILE *fp_index, *fp_record;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    
    			break;
    		}
    	}
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
    		loc = ftell(fp_index);
    
    		fseek(fp_record, loc / sizeof(int) * sizeof(*s2), SEEK_SET);
    
    		fread(s2, sizeof(*s2), 1, fp_record);
    
    		printf("\n\nRoll :%d", s2->roll);
    		printf("\n\nName :%s", s2->name);
    		printf("\n\nMarks :%f", s2->marks);
    
    		printf("\nEnter New Name : ");
    		scanf("%s", &(*s2->name));
    		printf("\nEnter New marks : ");
    		scanf("%f", &temp);
    		(s2->marks) = temp;
    
    		fseek(fp_record, (-1) * sizeof(*s2), SEEK_CUR);
    
    		fwrite(s2, sizeof(*s2), 1, fp_record);
    	}
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    }
    
    void search(stud *s2, int r_n)
    {
    	int r, loc, flag = 0;
    
    	FILE *fp_index, *fp_record;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    
    			break;
    		}
    	}
    
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
    		loc = ftell(fp_index);
    		fseek(fp_record, loc / sizeof(int) * sizeof(*s2), SEEK_SET);
    		fread(s2, sizeof(*s2), 1, fp_record);
    
    		printf("\n\nRoll :%d", s2->roll);
    		printf("\n\nName :%s", s2->name);
    		printf("\n\nMarks :%f", s2->marks);
    	}
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    }
    
    void delet(stud *s2, int r_n)
    {
    	int r, loc, flag = 0,file_handler_ind,file_handler_rec,file_size_ind,file_size_rec;
    	char ch;
    
    	FILE *fp_index, *fp_record;
    
    	fp_index = fopen("index.txt", "r+b");
    	fp_record = fopen("record.txt", "r+b");
    
    	file_handler_ind=_fileno(fp_index);
    	file_handler_rec=_fileno(fp_record);
    	
    	file_size_ind=_filelength(file_handler_ind);
    	file_size_rec=_filelength(file_handler_rec);
    
    	while (fread(&r, sizeof(int), 1, fp_index))
    	{
    		if (r == r_n)
    		{
    			flag = 1;
    
    			break;
    		}
    	}
    	if (!flag)
    	{
    		printf("\n\nRecord NOT found !!!");
    	}
    	else
    	{
    		fseek(fp_index, (-1) * sizeof(int), SEEK_CUR);
    		loc = ftell(fp_index);
    
    		fseek(fp_record, loc / sizeof(int) * sizeof(*s2), SEEK_SET);
    		fread(s2, sizeof(*s2), 1, fp_record);
    
    		printf("\n\nRoll :%d", s2->roll);
    		printf("\n\nName :%s", s2->name);
    		printf("\n\nMarks :%f", s2->marks);
    
    		printf("\n\nDo you want to delete this record ? y/n  ");
    		fflush(stdin);
    		scanf("%c", &ch);
    
    		if (ch == 'n' || ch == 'N')
    			return;
    
    		fseek(fp_index, sizeof(int), SEEK_CUR);	//  delete index value
    		while (fread(&r, sizeof(int), 1, fp_index))
    		{
    			fseek(fp_index, (-2) * sizeof(int), SEEK_CUR);
    			fwrite(&r, sizeof(int), 1, fp_index);
    			fseek(fp_index, sizeof(int), SEEK_CUR);
    		}
    
    		while (fread(s2, sizeof(*s2), 1, fp_record))
    		{
    			fseek(fp_record, (-2) * sizeof(*s2), SEEK_CUR);
    			fwrite(s2, sizeof(*s2), 1, fp_record);
    			fseek(fp_record, sizeof(*s2), SEEK_CUR);
    		}
    	}
    	
    	_chsize(file_handler_ind,file_size_ind-sizeof(int));
    	_chsize(file_handler_rec,file_size_rec-sizeof(*s2));
    
    	printf("\n\n");
    	fclose(fp_index);
    	fclose(fp_record);
    }
    
    void display(stud *s2)
    {
    	FILE *fp=NULL;
    	int flag = 1, ch, r;
    
    	printf("\n\n1:Index File \n\n2:Record File  ");
    	scanf("%d", &ch);
    
    	switch (ch)
    	{
    		case 1:
    
    			fp = fopen("index.txt", "rb");
    			
    			while (fread(&r, sizeof(int), 1, fp))
    			{
    				flag = 0;
    				printf("\n%d", r);
    			}
    
    			break;
    
    		case 2:
    
    			fp = fopen("record.txt", "rb");
    			
    			while (fread(s2, sizeof(*s2), 1, fp))
    			{
    				if ((s2->roll) != 0)
    				{
    					if (flag == 1)
    						printf("\n\nRoll No	 Name	 marks");
    					flag = 0;
    					printf("\n\n%d", s2->roll);
    					printf("	%s", s2->name);
    					printf("	%f", s2->marks);
    				}
    			}
    
    			break;
    
    		default:
    
    			printf("\n\nInvalid choice !!!");
    	}
    
    	if (flag == 1)
    		printf("\n\nNo record found !!!");
    	printf("\n\n");
    	fclose(fp);
    }
    
    int main()
    {
    	FILE *fp;
    	stud s1, s2;
    	int choice, roll_no;
    
    	fp = fopen("record.txt", "a+b");	
    	fclose(fp);
    
    	fp = fopen("index.txt", "a+b");
    	fclose(fp);
    
    	while (1)
    	{
    		printf("\n\n1:Add	2:Display	3:Search	4:Edit	     5:Delete       6:Exit \n\n");
    		scanf("%d", &choice);
    		switch (choice)
    		{
    			case 1:
    				add(&s1);
    
    				break;
    			case 2:
    
    				display(&s2);
    
    				break;
    			case 3:
    				printf("\n\nEnter Roll No To Search Record : ");
    				scanf("%d", &roll_no);
    				search(&s2, roll_no);
    
    				break;
    			case 4:
    				printf("\n\nEnter Roll No To Edit Record : ");
    				scanf("%d", &roll_no);
    
    				edit(&s2, roll_no);
    
    				break;
    			case 5:
    				printf("\n\nEnter Roll No To Delete Record : ");
    				scanf("%d", &roll_no);
    
    				delet(&s2, roll_no);
    
    				break;
    			case 6:
    				exit(0);
    
    			default:
    				printf("\n\nInvalid choice !!!");
    		}
    	}
    
    	return 0;
    }

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You were getting great help, so I stayed out of it, but I use Turbo C a good deal. Not the C++ compiler, but the C compiler.

    I don't understand why you're using an index, if you're actually swapping record contents. Seems like that's defeating one of the biggest benefits of using an indexed file, but some assignments are just that way.

    Are these still your requirements?
    i have an assignment to complete as soon as possible. It's something like:
    1.create (or open existing) Index and Record files.
    2.User can enter a record with roll no, name and marks.
    3.store this record at the end of file and add roll no as a key to record file.
    4.After each insertion, all keys(roll no) of Index file should be sorted in ascending order and while swapping two keys, the corresponding record also must be swapped.
    5.Perform display, Search, Modified, Delete operations using index file.
    And is your last posted code, your current code? What's your time frame, now?

  13. #13
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69
    yes, they told me to do what i have posted in the first post. And you are right that what i'm doing isn't an efficient way to do that task. But I have to do what they have stated. It's something like 'just do what I said'.
    Now coming to problem, is possible to use this code in turbo c with little changes? N why it's currently not working no it?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What's "not working" about your edit and delete functions?

    And are you certain, via looking at the docs or writing test code, that fileno, filelength, and chsize work exactly the same as the _ versions?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I suggest replacing exit(0), with return 0 in your menu code.

    There is no difference, in the way you use exit(0) with return 0, and it allows io.h not to be included, saving 400 lines of compiled code.

    In the sorting function, there seems to be an error. J should go to <n rather than to < n-1, or do you use the last record for some reason that it shouldn't be sorted?

    Code:
    for (i = 0; i < n - 1; i++)
    {
         //for (j = 0; j < n - 1; j++)  // for j, only i+1 to n-1 need to be compared
         for (j = i+1; j < n; j++)  //correct
    Do you have a test file that you're using for this, or will any file do for testing? (I'm just adding some records by hand, for now).

    You did not fix the underline problem for Turbo C. Remove the underlines in front of _filelength, _fileno, and _chsize. Turbo C won't compile them, as is - linker error. After removal of these underlines, it will compile and length.

    Your edit function isn't editing out the right record. The new record is being added to the end of the file, only. You will add NEW records like this, but edited records need to locate the right record first, and then add the actual fields, not make a new record and leave the old one.
    Last edited by Adak; 02-03-2011 at 10:55 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM