Suppose you are given a text file that contains the names of people. Every name
ID: 3534989 • Letter: S
Question
Suppose you are given a text file that contains the names of people. Every name in the file consists of a first name and last name. Unfortunately, the programmer who created that file of names had a strange sense of humor and did not guarantee that each name was on a single line of the file. Read this file of names and write them to a new file, one name per line. For example, if the input file contains:
Bob Jones Fred
Charles Ed
Marston
Jeff
William
The output file should be:
Bob Jones
Fred Charles
Ed Marston
Jeff Williams
Explanation / Answer
#include<iostream.h>
struct name
{
char first[30],last[30];
};
void main()
{
name a;
char n;
fstream file,file1;
file.open("name.txt");
file1.open("output.txt");
while(file!=eof)
{
a=file.read((*ch)&a,sizeof(a));
file1.write((*ch)&a,sizeof(a));
}
cout<<"file contain ";
while(file1!=eof)
{
a=file1.read((*ch)&a,sizeof(a));
cout<<a.first<<" "<<b.last;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.