output in file using stdin

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>
using namespace 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;
}
.
closed account (Dy7SLyTq)
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
Have you tried (Assuming you are trying to read a line from the file):
std::getline(myfile, str);

If not then can you try to explain more what you are trying to do on line 12 because you don't seem to do anything with the string in 'str'
closed account (Dy7SLyTq)
you cant do that smac. myfile is an object of ofstream. its like saying getline(cout, str);
I suppose he wants a cat
http://stackoverflow.com/questions/10195343/copy-a-file-in-an-sane-safe-and-efficient-way


> 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/
Last edited on
You can always supply your own overload:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
#include <fstream>

using std::getline;

std::istream& getline(std::istream& is, std::ostream& os, char delimiter = '\n')
{
    std::string buffer;

    if (getline(is, buffer, delimiter))
        os << buffer + '\n';

    return is;
}

int main()
{
    std::string str;
    std::ofstream myfile("hello.txt");

    const char* msg = "Hello.  How are you?\n> ";

    std::cout << msg;
    myfile << msg;

    getline(std::cin, myfile);
}
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();

return 0;
}
Last edited on
@cire
is there any other way apart from overloading!
btw thanks for reminding overloading that works well!
Define "not works".

It has already been stated that std::getline is selective on what parameter types it does accept.

You were initially quite close though. You had:
1
2
std::string buffer;
getline( std::cin, buffer);

cire did write a slightly different code above:
1
2
3
4
5
std::string buffer;
if ( getline( std::cin, buffer ) )
  {
     myfile << buffer + '\n';
  }

Could you spot and explain how this differs?
Topic archived. No new replies allowed.