LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2003, 07:21 AM   #1
maarten
LQ Newbie
 
Registered: Jun 2003
Distribution: Debian
Posts: 8

Rep: Reputation: 0
strange ofstream output


Hi all,

I just started learning c++ and i was fooling around a little bit, trying to make something that would put my input in a file; i wrote this:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
ofstream SaveFile("text.txt", ios::ate);
SaveFile << cin;
SaveFile.close();

return 0;
}

I compiled and run i, but instead of asking for my input, it just ended and cat text.txt gave 0x8049b38. I know this has something to do with memory, but thats all. It's not terribly important, but if someone could explain - very simple!- why it does that, i can stop losing my sleep over it .

thx,

maarten
 
Old 09-16-2003, 07:54 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
im not too familiar with the iostream library so someone will more than likely tell me im completely wrong but here goes, cin is an object probably of type ifstream, now when an ofstream recieves an object derived from ios or some other basic iostreams class, it gets its address hence here you are writing the address of the cin object to a file.

to do what you want you need something like this:
Code:
#include <iostream>

using namespace std;

int main()
{
    while(!cin.eof())
        cout << (char)cin.get();
    return 0;
}
 
Old 09-16-2003, 12:42 PM   #3
maarten
LQ Newbie
 
Registered: Jun 2003
Distribution: Debian
Posts: 8

Original Poster
Rep: Reputation: 0
Ah, adresses, thats not for another chapter or 2 I think

But thanks anyway, I will try out your example,

maarten
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange continuous output Nightfrost Linux - General 2 11-02-2004 05:28 AM
Strange traceroute output egurski Linux - Networking 0 07-13-2004 07:25 PM
Strange dmesg output voyciz Linux - Networking 3 06-08-2004 01:05 PM
Strange Output (C++)&#1618; Mohsen Programming 6 05-07-2004 05:45 PM
very strange dmesg output salparadise Linux - Software 6 04-08-2004 12:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration