954,173 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Deleting a record from a file

hai!

(1)I have problem in deleting a record from a file.

I was told that the only way to delete a record from a file is to copy the file to another temporary file and then copy the records to the same file again by skipping the record that u need to delete i don't have problem until the copying of the file to the temporary file but i don't know how to skip the record that needs to be delete.

(2)what's the command to rename a file?

Plz anybody help me!


Urs,
PriyaJaiGanesh.

PriyaJaiGanesh
Newbie Poster
4 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

read the old file, record for record.
Write all records to the tempfile, except the one(s) you want to delete.
Remove the old file.
Copy the tempfile completely to a new file with the same name as the old one.
Remove the tempfile.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
(2)what's the command to rename a file?

int rename (const char *oldname, const char *newname)
The rename function renames the file oldname to newname. The file formerly accessible under the name oldname is afterwards accessible as newname instead. If rename fails, it returns -1.

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 
(1)I have problem in deleting a record from a file.


Lets say you want to delete record 3 in a 10 record file:

int rec_to_delete = 2; // record 3 is index 2 

for(int i = 0; i < 9; i++)
{
    if(i != rec_to_delete)
    {
        // copy record
    }
}


use the if statement so that the record number you want to delete doesnt get copied.

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

syntax to rename a file is
int rename(char *old_filename,char *new_filename);

shridhark
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
I Think this code will help you to solve your problem:

Add Some Student:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<io.h>
#include<fcntl.h>

struct student
{
	char name[20];
	char semCode[10];
	int enrollNo;
	int assignSub;
	int marks;
	int attendence;
};
struct student var;
FILE *ptr;

void refresh()
{
	fclose(ptr);
	ptr = fopen("c:\\file.txt","a+");
}
void addStudentRecord()
{
	refresh();
	fflush(stdin);
	printf("Enter name:\n");
	gets(var.name);

	fflush(stdin);
	printf("Enter Semester Code:\n");
	gets(var.semCode);

	fflush(stdin);
	printf("Enter Enrollment Number:\n");
	scanf("%d",&var.enrollNo);
	
	var.assignSub=0;
	var.marks=-1;

	fwrite(&var,sizeof(struct student),1,ptr);

	printf("Data Entered Successfully\n");
	printf("Press any key..");
	refresh();
	getch();
}

When You want to delete the record:

void deleteStudentRecord()
{
	int enrollNo;
	int counter=0;
	

	FILE *ptr2 = fopen("c:\\file2.txt","a");

	int records = getNoOfRecords();

	refresh();
	fflush(stdin);
	printf("Enter Enrollment Number:\n");
	scanf("%d",&enrollNo);

	while(counter!=records)
	{
		fread(&var,sizeof(struct student),1,ptr);
		if(var.enrollNo==enrollNo)
		{

		}
		else
		{
			fwrite(&var,sizeof(struct student),1,ptr2);
		}
		counter++;
	}
	fcloseall();
	remove("c:\\file.txt");
	rename("c:\\file2.txt","c:\\file.txt");
	printf("Press any key..");
	getch();
}

void main()
{
	int a=0;

	ptr = fopen("c:\\file.txt","a+");

	while(a!=2)
	{
		system("cls");
		printf("Select an Option:\n");
		printf("1.Add Student Record\n");
		Printf("2.Delete Student Record\n");
		scanf("%d",&a);

		switch(a)
		{
				case 1:
				addStudentRecord();
				break;
			
			case 2:
				updateStudentRecord();
				break;

			case 3:
				deleteStudentRecord();
				break;
			default:
				printf("Invalid Option\n");
				break;
		}
}
}

2. rename file:

rename("c:\\file2.txt","c:\\file.txt");
1seo
Newbie Poster
22 posts since Aug 2010
Reputation Points: 10
Solved Threads: 2
 

Rather than doin this,it s better to read the data and store it in some array or linklist then perform that delete operation in that array and now write the new array in to your file....

snivas519
Newbie Poster
10 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You