Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Monday, January 9, 2023

Write + read w+ mode in C programming

 Write + read w+ mode in C programming

 

In the "w+" mode of file handling in C programming, the file is opened for both reading and writing. If the file does not exist, it is created. If the file already exists, its contents are truncated (i.e., the file is made empty). This means that any data that was previously stored in the file is deleted and the file pointer is positioned at the beginning of the file, ready for writing.

 

Example 1:

 

WAP to show data writing and reading operation to/from a data file.         

Ans:

#include<stdio.h>

#include<conio.h>

void main()

{

char name[20],add[20];

int rollno;

FILE fp;

fp=fopen("STUFENT.DAT","w+");

for(i=0;i<10;i++)

{

printf("Enter name:\n");

scanf("%s",name);

printf("Enter address:\n");

scanf("%s",add);

printf("Enter roll no\n");

scanf("%d",&rollno);

 

//writing to a dat file

fprintf(fp,"%s%s%d",name,add,rollno);

}

rewind(fp);

 

// Reading from file.

for(i=0;i<10;i++)

{

fscanf(fp,"%s%s%d",name,add,&rollno);

printf("Name=%s\tAddress=%s\t Rollno=%d",name,add,rollno);

fclose(fp);

getch();

}

}

 

Example 2:

A sequential data file emp.txt has contains employee name, post and salary. Write a C program to add few new records on it and display all the records on the screen.

#include <stdio.h>

#include <conio.h>

main()

{

int id;

char name[20],post[30];

FILE *fp;

fp=fopen("d:\\emp.txt","w+");

char c='y';

while(c=='y')

{

printf("Enter ID:\n");

scanf("%d",&id);

printf("Enter employee name:\n");

scanf("%s",name);

printf("Enter post:\n");

scanf("%s",post);

fprintf(fp,"%d%s%s",id,name,post);

printf("Do you need more record (press y)");

c=getche();

}

rewind(fp);

while(fscanf(fp,"%d%s%s",&id,name,post)!=EOF)

{

printf("\n%d\t%s\t%s",id,name,post);

}

fclose(fp);

}

 

Example 3:

WAP that reads successive records from new data file and display each record on the screen in an appropriate format.

Ans:

#include<stdio.h>

void main()

{

struct employee

{

char name[15];

int age;

int salary;

};

struct employee e;

int i,n;

FILE *fp;

printf("How many employee are there\n");

scanf("%d",&n);

/* Writing data to the file */

fp=fopen("employee.dat","w+");

for(i=0;i<n;i++)

{

printf("Enter name age and salary of employee\n");

scanf("%s%d%d",e.name,&e.age,&e.salary);

fprintf(fp,"%s\n%d\n%d",e.name,e.age,e.salary);

}

/* Displaying data */

rewind(fp);

for(i=0;i<n;i++)

{

fscanf(fp,"%s%d%d",e.name,&e.age,&e.salary);

printf("\n%s\n%d\n%d\n",e.name,e.age,e.salary);

}

getch();

}

 

Example: 4

WAP to store std-no, name and marks of 'n' students in a data file. Display the records in appropriate format reading from the file.                                                                  

Ans:

#include<stdio.h>

#include<conio.h>

void main()

{

struct student

{

char name[10];

int roll,marks;

};

struct student st;

int i,n;

FILE *fp;

fp=fopen("student.txt","w+");

printf("How many students are there\n");

scanf("%d",&n);

for(i=0;i<n;i++)

{

printf("Enter name,rollno and total marks of student\n");

scanf("%s%d%d",st.name,&st.roll,&st.marks);

fprintf(fp,"%s\n%d\n%d\n",st.name,st.roll,st.marks);

}

rewind(fp);

printf("\n Displaying records of file\n");

for(i=0;i<n;i++)

{

fscanf(fp,"%s%d%d",st.name,&st.roll,&st.marks);

printf("Name: %s\n",st.name);

printf("Roll no: %d\n",st.roll);

printf("Marks: %d\n\n",st.marks);

}

fclose(fp);

getch();           

}

 

Example 5:

Write a C program to enter ID, employee_name, and post of the employee and store them in a data file named "emp.txt". Display each record on the screen in an appropriate format.

#include <stdio.h>

#include <conio.h>

main()

{

int id;

char name[20],post[30];

FILE *fp;

fp=fopen("d:\\emp.txt","w+");

char c='y';

while(c=='y')

{

 printf("Enter ID:\n");

 scanf("%d",&id);

 printf("Enter employee name:\n");

scanf("%s",name);

 printf("Enter post:\n");

 scanf("%s",post);

 printf(fp,"%d%s%s",id,name,post);

printf("Do you need more record (press y)");

 c=getche();

}

rewind(fp);

while(fscanf(fp,"%d%s%s",&id,name,post)!=EOF)

{

printf("\n%d\t%s\t%s",id,name,post);

}

fclose(fp);

}

 

Example 6:

WAP to input name, salary and age of 'n' no of employee and store them in a data file "emp.dat" and display all the contents of file.

Ans:

#include<stdio.h>

#include<conio.h>

void main()

{

struct employee

{

char name[20];

in tag;

float sal;

};

struct employee e;

int i,n;

FILE *fp;

printf("Enter how many no\n");

scanf("%d",&n);

fp=fopen("emp.dat","w+");

printf("\n Enter name, age and salary");

for (i=0;i<n;i++)

{

scanf("%s,%d,%f",e[i].name,&e[i].ag,&e[i].sal;

fprintf(fp,"%s\t%d\t%f",e[i].name,e[i].ag,e[i].sal);

}

rewind(fp);

printf("\n Name\tAge\tSalary");

for(i=0;i<9i++)

{

fscanf(fp,"%s,%d,%f",e[i].name,&e[i].ag,&e[i].sal);

printf(%s\t%d\t%f",e[i].name,e[i].ag,e[i].sal);

}

fclose(fp);

getch();

}

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages