copy does not work 
Author Message
 copy does not work

I have function as follow:
void to_file(std::string file_name)
{
std::ofstream file(file_name.c_str());
std::copy(std::istream_iterator<char>(std::cin),
  std::istream_iterator<char>(),
  std::ostream(file);
Quote:
}

after My input with keyboard,
The function can only generate a file ,
but the file has no any char in it ,
why?

thanks in advance!



Fri, 20 Feb 2004 17:59:47 GMT  
 copy does not work

Quote:

> I have function as follow:
> void to_file(std::string file_name)

Prefer
void to_file(std::string const& file_name)

Quote:
> {
> std::ofstream file(file_name.c_str());
> std::copy(std::istream_iterator<char>(std::cin),
>   std::istream_iterator<char>(),
>   std::ostream(file);

I assume that is a typo, since the std::ostream(file) performs a slice
copy on the ofstream, and ostream isn't an output iterator anyway, so
won't compile as the third arg of copy.

istream_iterator should be used for formatted input. To just bit copy,
use

std::copy(std::istreambuf_iterator<char>(std::cin),
   std::istreambuf_iterator<char>(),
   std::ostreambuf_iterator(file));

Tom



Fri, 20 Feb 2004 22:39:55 GMT  
 copy does not work
with tom's advise , i have changed my function , now it is  as follow
void to_file(std::string const& file_name)
{
 std::ofstream file(file_name.c_str());
 std::copy(std::istreambuf_iterator<char>(std::cin),
   std::istreambuf_iterator<char>(),
   std::ostreambuf_iterator<char>(file));
Quote:
}

it can be complied successfully and could be run,
but how could i end up my enter with char?
thank you very much.


Sat, 21 Feb 2004 10:39:16 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. String copying not working?

2. Number of Copies Not Working

3. Subclassed copy constructor not working

4. Copy/Paste using Ctrl+C/Ctrl+V does not work in CHtmlView Class

5. Copy/Paste via keyboard not working in FormView

6. Rich Edit cut/copy does not work?

7. C++/ATL/ADO - Intellisense not working (statement completion options) not working

8. std::copy isn't doing what I expect

9. Where is the rough work done?

10. Done it - Quick C now works

11. this code works on SGI does not work on linux

12. CMap Lookup not working (at least I cannot get it to work)

 

 
Powered by phpBB® Forum Software