why i cannot take input from std::cin and store in file directly as in 12th line using myfile instead of str?
str is also an object as well as myfile is also an object to file stream!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <fstream>
#include<string>
usingnamespace std;
int main()
{
string str;
ofstream myfile;
myfile.open("hello.txt");
myfile<<"hello how are you"<<endl;
getline(std::cin,str);
myfile.close();
return 0;
}
.
a) a better title would be input from file on stdin because it sounds like you want to write to file with stdin.
b) cin is not the same thing at all as stdin
c) you need to work on your english. i dont understand what your asking. do you mean you want to read the line from your file? just use ifstream
> str is also an object as well as myfile is also an object
There is no an `Object' base class that everything derive from
If it were, it doesn't matter. `getline()' asks for an string, not a generic object http://www.cplusplus.com/reference/string/string/getline/
actually i want to take input from std in and want to store that in file thats pointer is myfile
as we use getline(myfile,str) here we take input from file and then store to string str that worls well
like that i want to take input from stdin instead of myfile and then i want to store it in myfile instead of str?
i dopn't know why it not works?
here is the code
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
int main()
{
string str;
ofstream myfile;
myfile.open("hello.txt");
myfile<<"hello how are you"<<endl;
getline(std::cin,myfile);
myfile.close();