'How to let user write into the file in c++ using put() function?
I'm writing a c++ program to open a file with the user-defined file name. And then using the put() function to let the user write into the file until a newline character is encountered. But on running the program I'm not able to write into the file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream outfile;
char name[20];
cout<<"Enter the name of file to be opened"<<endl;
cin.getline(name, 20);
outfile.open(name, ios::out );
if(!outfile){
cout<<"File cannot be opened"<<endl;
return 1;
}
char ch;
cout<<"File opened successfully"<<endl;
cout<<"Write into the file"<<endl;
outfile.seekp(0);
while(ch != ' '){
outfile.put(ch);
}
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|