How to write a C Program To Write Data In A File And Search Data From File in C Programming Language ?
Solution For C Program :
/*C Program To Write Data In A File And Search Data From File.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int r,found;
struct stud
{
char name[30];
int age;
int roll_no;
}st;
FILE *fp;
fp=fopen("test.dat","r+t");
if(fp==NULL)
{
printf("\nCannot open file\n");
exit(1);
}
printf("enter role no:");
found=0;
scanf("%d",&r);
while((!feof(fp))&&(found))
{
fread(&st,sizeof(stud),1,fp);
if(st.roll_no==r)
{
fseek(fp,-sizeof(stud),SEEK_CUR);
printf("enter new name\n");
scanf("%s",st.name);
fwrite(fp,sizeof(stud),1,fp);
found=1;
}
}
if(!found)
printf("record not present");
fclose(fp);
}
You may also learn these C Program/Code :
C Program To Swap Two Numbers Without Using Third Variable
Learn More :
Write
File
- COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM
- C Program File Input & Output Example
- C Program To Destruc Self Execution File ?
- C Program To Store Students Record In A Text File
- Make a copy of file given in argv[1] to file given in argv[2]
- C Program to copy a file to another file
- C Function to read instructions from the file and obey them
- Client/Server C program to make client send the name of a file
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program to Read and Write FIFO
- File Number Control C Program Example
- C Program to opens and reads dictionary file specified in spellrc
- C Program to Recursively converts all file-names to lower-case
- C Program to Find the Size of File using File Handling Function
- C Program Recursive function that searches for a given file in a given folder
- C Program Read a char & print next char ( file to file )
- C Program to Input Student Details into a File For Two Subjects
- Identifies the architecture Windows PE (exe or dll) file was compiled C Program
- File Handling (console to file) in C Program
Data
Search
- Dictionary Word Search C Program
- C Depth First Search
- C Program To Search A Number Inside The Array
- C Program to search an element using linear search or binary search (menu driven program)
- C Program Performs a search and replace for a specified target and replacement string
- C Program Recursive function that searches for a given file in a given folder
- Pre Order, Post order, In order Implement Binary Tree using linked list
- BST Tree in C
- C Program Search Function For Sequence
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list