Write program to input a multi-word string from keyboard and write this input string along
with first letter of each of its word into a file “initials.txt”. Data already present in the file
initials.txt must be preseved. Display suitable error message if the file opening/writing
operation fails.
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str;
int i=0;
cout<<"Enter a string: ";
getline(cin, str);
fstream file;
file.open("initials.txt", ios::app);
if(file)
{
file<<str[0];
while(str[i]!='\0')
{
if(str[i]==' ')
file<<str[i+1];
i++;
}
}
else
{
cout<<"File not opened successfully!";
}
file.close();
return 0;
}
Comments
Leave a comment