0% found this document useful (0 votes)
33 views

Program To Read From A File Placed in A Directory.

The document contains 4 C++ programs that demonstrate reading from and writing to files. The first program reads characters from a text file and prints them. The second program prompts the user for names and roll numbers, writes them to a file. The third program reads names from a file and prints them. The fourth program defines a student structure, writes structure variables to a file, then reads the file and prints a structure variable.

Uploaded by

Rupinderkaur786
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Program To Read From A File Placed in A Directory.

The document contains 4 C++ programs that demonstrate reading from and writing to files. The first program reads characters from a text file and prints them. The second program prompts the user for names and roll numbers, writes them to a file. The third program reads names from a file and prints them. The fourth program defines a student structure, writes structure variables to a file, then reads the file and prints a structure variable.

Uploaded by

Rupinderkaur786
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Program to read from a file placed in a directory..

#include <stdio.h>
#include<conio.h>
//#include <Windows.h>
int main()
{
FILE *fd;
char ch;
char buff[78];
fd=fopen("d:\\ric.txt","r");
ch=getc(fd);
while(ch!=EOF)
{
printf("%c",ch);
ch=getc(fd);
}
//ch=fscanf(fd,"%s",);
//fputc('ch',fd);
//getch();
fclose(fd);
}

Program to read in a text file


include <iostream>
#include<string.h>
using namespace std;
int main()
{
FILE *fd;
char name[10];
int k;
int roll;//struct student stu;
fd=fopen("d:\\ric.txt","w");
for(k=0;k<=2;k++)
{
cout<<"name";
cin>>name;
cout<<"roll";
cin>>roll;
fprintf(fd,"name=%s roll=%d",name,roll);
}
fclose(fd);
}

Progam to read and write

#include <iostream>
#include<string.h>
#include<conio.h>
using namespace std;
int main()
{
FILE *fd;
char name[10];
int k;
//

int roll;//struct student stu;


fd=fopen("d:\\ric.txt","w");
///*for(k=0;k<=2;k++)
//{*/
for(k=0;k<2;k++)

{
cout<<"name";
cin>>name;
// // cout<<"roll";
// //
cin>>roll;
fprintf(fd,"name=%s",name);
}
fclose(fd);
fd=fopen("d:\\ric.txt","r");
fscanf(fd,"%s",name);
//while(ch!=EOF)
//{
printf("%s",name);
// ch=fscanf(fd,"%s");
fclose(fd);
getch();}

Program to read and write file using structure.


#include <iostream>
#include<string.h>
#include<conio.h>
//#include<Ras.h>
using namespace std;
struct student
{
char name[10];
int age;
};
int main()
{
FILE *fd;
//char name[10];
//int k;
struct student s1;
char data[12];
// fd=fopen("d:\\ric.txt","w");
///*for(k=0;k<=2;k++)
//{*/
//for(k=0;k<2;k++)
//{
//cout<<"name:";
//cin>>s1.name;
//cout<<"age:";
//cin>>s1.age;
//fprintf(fd,"name=%s age=%d",s1.name,s1.age);
//}
//fclose(fd);

//

fd=fopen("d:\\ric.txt","r");
//
fread(&data,sizeof(student),12,fd);
// //while(ch!=EOF)
// //{
printf("%s%d",s1.name,s1.age);
printf("%s",data);
fclose(fd);getch();}

You might also like