Bharathiar University
Programming in C++ LabC++ Program 11Write a C++ Program to create a File and to display the contents of that file with line numbers.
In this page
Write a C++ Program to create a File and to display the contents of that file with line numbers.
In this page
- C++ Source Code for MS Dos Turbo C++
- C++ Source Code for Visual studio code
Source code for Turbo C++#include<iostream.h>#include<fstream.h>#include<stdio.h>#include<string.h>#include<conio.h>int main(){ char fname[30], str[100]; fstream fp; int i=0; clrscr(); cout<<"Enter the Name of File: "; gets(fname); fp.open(fname, fstream::out); if(!fp) { cout<<"\nError Occurred!"; return 0; } cout<<"Enter the Data: "; gets(str); while(strlen(str)>0) { fp<<str; fp<<'\n'; gets(str); } fp.close(); fp.open(fname, fstream::in); if(!fp) { cout<<"\nError Occurred!"; return 0; } cout<<"\nContent of "<<fname<<":-\n"; fp.getline(str, 1000); while(strlen(str)>0) { cout<<++i<<"."; cout << str << endl; fp.getline(str, 1000); } fp.close(); getch(); return 0;}
Output:
Output.txt file
#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main()
{
char fname[30], str[100];
fstream fp;
int i=0;
clrscr();
cout<<"Enter the Name of File: ";
gets(fname);
fp.open(fname, fstream::out);
if(!fp)
{
cout<<"\nError Occurred!";
return 0;
}
cout<<"Enter the Data: ";
gets(str);
while(strlen(str)>0)
{
fp<<str;
fp<<'\n';
gets(str);
}
fp.close();
fp.open(fname, fstream::in);
if(!fp)
{
cout<<"\nError Occurred!";
return 0;
}
cout<<"\nContent of "<<fname<<":-\n";
fp.getline(str, 1000);
while(strlen(str)>0)
{
cout<<++i<<".";
cout << str << endl;
fp.getline(str, 1000);
}
fp.close();
getch();
return 0;
}
Source code for Visual studio code
#include<iostream>#include<fstream>#include<stdio.h>#include<string.h>using namespace std;int main(){ char fname[30], str[50],ch; fstream fp; int i=0; cout<<"Enter the Name of File: "; gets(fname); fp.open(fname, fstream::out); if(!fp) { cout<<"\nError Occurred!"; return 0; } cout<<"Enter the Data: "; gets(str); while(strlen(str)>0) { fp<<str; fp<<"\n"; gets(str); } fp.close(); fp.open(fname, fstream::in); if(!fp) { cout<<"\nError Occurred!"; return 0; } cout<<"\nContent of "<<fname<<":-\n"; fp.getline(str, 1000); while(strlen(str)>0) { cout<<++i<<"."; cout << str << endl; fp.getline(str, 1000); } fp.close(); cout<<endl; return 0;}
Output:
test.txt file
#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
char fname[30], str[50],ch;
fstream fp;
int i=0;
cout<<"Enter the Name of File: ";
gets(fname);
fp.open(fname, fstream::out);
if(!fp)
{
cout<<"\nError Occurred!";
return 0;
}
cout<<"Enter the Data: ";
gets(str);
while(strlen(str)>0)
{
fp<<str;
fp<<"\n";
gets(str);
}
fp.close();
fp.open(fname, fstream::in);
if(!fp)
{
cout<<"\nError Occurred!";
return 0;
}
cout<<"\nContent of "<<fname<<":-\n";
fp.getline(str, 1000);
while(strlen(str)>0)
{
cout<<++i<<".";
cout << str << endl;
fp.getline(str, 1000);
}
fp.close();
cout<<endl;
return 0;
}
No comments:
Post a Comment