Loading....
Coupon Accepted Successfully!

 

More Programs

16.26 Write a program to copy contents of one file to another file.

#include<fstream.h>

#include<conio.h>

#include<process.h>

main()

{

clrscr();

char s[12],t[12],c;

fstream out;

ifstream in;

cout<<“\n Enter a Source file name:”;

cin>>s;

cout<<“\n Enter a target file name:”;

cin >>t;

in.open(s,ios::in | ios::nocreate);

if (in.fail())

{

cout<<“\nFile “<<s <<“ Not Found”;

exit(1);

}

out.open(t,ios::out | ios::nocreate);

if (out.fail())

{

out.open(t,ios::out);

while (in.eof()==0)

{

in.get(c);

out.put(c);

}

}

else

cout<<“\n Target file already exist.”;

in.close();

out.close();

return 0;

}

OUTPUT
Enter a Source file name : DATA
Enter a target file name : TEXT
 
Explanation: In the above program, the user enters source and target file names. The existence of the files is checked. In case the source file is absent and the target file is already present, the copying of data will not take place. Appropriate messages are displayed when the file names are not properly entered.
 
The target file is opened in read and nocreate mode. The nocreate flag prevents the opening of a new file if the file is absent. If it is unsuccessful, then the open() statement within the if statement opens the file for writing. The while loop executes the function till the file pointer reaches the end of the source file. The get() statement reads data from the source file, and the put() statement writes the read data to the target file. Thus, the copying of data is carried out. After termination of the while loop, both the files are closed using close() functions.
 

16.27 Write a program to copy content of one file in another file in reverse order. Display the contents of the screen.

#include<fstream.h>

#include<conio.h>

#include<process.h>

main()

{

clrscr();

char s[12],t[12],c;

fstream out;

ifstream in;

cout<<“\n Enter a Source file name:”;

cin>>s;

cout<<“\n Enter a target file name:”;

cin >>t;

in.open(s,ios::in ); //| ios::nocreate);

if (in.fail())

{

cout<<“\nFile”<<s <<“ Not Found”;

exit(1);

}

else

in.seekg(0,ios::end);

out.open(t,ios::out | ios::nocreate);

int b;

if (out.fail())

{

out.open(t,ios::out);

in.seekg(0,ios::end);

b=in.tellg();

for (int i=1;i<=b;i++)
{
in.seekg(-i,ios::end);
in.get(c);
out.put(c);
cout<<c;
}
}
else
cout<<“\nTarget file alredy exist.”;
in.close();
out.close();
return 0;

}

OUTPUT
Enter a Source file name : cpp
Enter a target file name : cp2
GnimmargorP detneirO tcejbO
 
Explanation: In the above program, source and target file names are entered. The content of the source file is copied to the target file in reverse order. The source file is opened for reading, and the target file is opened for writing. Using the tellg() function, the size of the source file is obtained and stored in the variable b. The for loop executes from 1 to b (size of source file). The seekg() function moves the get file pointer in the reverse order, that is, from end to top. The argument –i specifies the number of bytes to be read from the end of the file. The character read by the get() function is written to the target file by the put() function. The cout() statement displays the contents of the variable c on the screen.
 

16.28 Write a program to open a file in read and write mode. Write data to the file and read from it.

#include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

ofstream out (“data.txt”); // creates file for writting

char name[]=”SANJAY”;

int age=25;

float ht=4.5;

out <<name<<“\t”<<age<<“\t”<<ht; // writes data to the data.txt

out.close(); // closes file
ifstream in (“data.txt”); // opens file for reading
in>>name >>age>>ht; // reads data from file and assigns to variables
cout<<endl<<“Name:”<<name; // display data on the screen
cout<<endl<<“Age:”<<age;
cout<<endl<<“Height:”<<ht;

}
 

OUTPUT
Name : SANJAY
Age : 25
Height : 4.5
 
Explanation: In the above program, the file “data.txt” is opened in the write mode. The values of the variable’s name, age, and ht are written to the file. The file is closed using the close() function.
 
Again, the same file is opened in the read mode. The data read are assigned to respective variables and displayed on the screen using the cout() statement.
 

16.29 Write a program to write data to the file in string format also read and display the data in the same fashion.

#include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

char text[100];

ofstream out (“data.txt”);

out<<“ Programming with ANSI and Turbo C”;

out<<“\n Teaches you C with practical programs”;

out.close();

ifstream in (“data.txt”);

while (!in.eof())

{

in.getline(text,100);

cout<<endl<<text;

}

}

OUTPUT
Programming with ANSI and Turbo C
Teaches you C with practical programs
 
Explanation: This program is similar to the previous one. Here, a string is written to the file. Using the getline() function, the string is read from the file and displayed. As soon as the end of the file is detected, the while loop terminates.
 

16.30 Write a program to copy contents of one file to another file. Use rdbuf() function.

#include<fstream.h>

#include<conio.h>

void main()

{

clrscr();

char sfile[20], dfile[20];

cout<<“\nEnter source file:”;

cin>>sfile;

cout<<“\nEnter destination file:”;

cin>>dfile;

ifstream in(sfile);

ofstream out(dfile);

out<<in.rdbuf();

in.close();

out.close();

}

OUTPUT
Enter source file : data.txt
Enter destination file : c.txt
 
Explanation: In the above program, two character arrays sfile [20] and dfile [20] are declared to hold source and destination file names, respectively. The rdbuf() copies entire the source file to the target file.
 

16.31 Write a program to display strings and their addresses.

#include<strstream.h>

#include<conio.h>

main()

{

clrscr();

char s[]=”Sunday”;

char *r=”Monday”;

cout<<“Strings”<<endl;

cout<<s <<endl;

cout<<r <<endl;

cout<<endl<<“Addresses”<<endl;
cout<<&s<<endl; // c style
cout<<(void*) r<<endl;
cout<<(unsigned)&s<<endl;
return 0;

}

OUTPUT
Strings
Sunday
Monday
Addresses
0x8f9cffee
0x8f9c00b1
65518
 
Explanation: In the above program, character array s[] and character pointer r are initialized with strings. The strings and their memory addresses are displayed using cout() statements. The address can be displayed using the & operator in traditional C style. We can also display the address as per the statement cout<<(void*) r. The address is converted from char* into void*. The statement cout<<(unsigned)&s converts the address into an unsigned integer and displays it.
 

16.32 Write a program to show errors occurring during file opening operations.

#include<fstream.h>

#include<conio.h>

#include<process.h>

void main()

{

clrscr();

void errors (ofstream &);

ofstream f;

f.open (“data.txt”,ios::noreplace);

if (!f) errors(f);

else f <<“Hope is a walking dream”;

f.close();

}

void errors (ofstream &f)

{

cout<<endl<<“File opening errors”;

cout<<endl <<“Error state=”<<f.rdstate();

cout<<endl<<“fail()=”<<f.fail();

cout<<endl<<“eof()=”<<f.eof();

cout<<endl<<“bad()=”<<f.bad();
cout<<endl<<“good()=”<<f.good();
_cexit();

}

OUTPUT
File opening errors
Error state = 4
fail()=4
eof() = 0
bad()=4
good()=0
 
Explanation: In the above program, the file data.txt is in the write mode. If the file opening operation fails, the function error() is executed. The if statement checks the value of the object f, and if it is zero, then the function errors() are invoked, which display the errors. The error states displayed in the output are illustrated in Table:

Table: Return values of functions
 

rdstate()

(error state = 4)

The rdstate() function gives the value 4, and it points that the file operation was unsuccessful.

fail()=4 & bad()=4

These functions display a non-zero value, and this is due to an error that is generated during the operation.

eof() = 0

This function returns zero, because the file pointer is not at the end of the file.

good() = 0

This function returns zero, because no bit sets.

 

16.33 Write a program to enter numbers using command line arguments. Calculate the product of all the numbers.

#include<strstream.h>

#include<conio.h>

void main (int argc, char *argv[])

{

int k=1;

long n,s=1;

if (argc<2)

{

cout<<“ Enter numbers ”;

return ;

}

while (--argc)

{

istrstream value (argv[k]);

value>>n;

s*=n;
k++;
}
cout<<endl<<“ Multiplication of entered numbers:”<<s<<endl;
}

OUTPUT
C:\tc3>cmd 4 4 4
Multiplication of entered numbers : 64
C:\tc3>cmd 45545 2
Multiplication of entered numbers : 91090
 
Explanation: The above program can be executed on command prompt by creating its exe file. The number of arguments entered by the user is checked by the first if statement; if arguments are less than two, the message displayed will be “Enter numbers.”
 
If the user enters numbers followed by the file name, the product of all the numbers is calculated and displayed. The object value is the object of the class istrstream, and it is connected to the buffer. Consider the statement value>>n; here, the object value takes data from the buffer and assigns it to the variable n.




Test Your Skills Now!
Take a Quiz now
Reviewer Name