User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,593 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,666 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 3063 | Replies: 21
Reply
Join Date: Jul 2005
Posts: 37
Reputation: jasondrey13 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jasondrey13 jasondrey13 is offline Offline
Light Poster

Help help!! how do you save a variable to a...

  #1  
Jul 22nd, 2005
how do i save a variable to a file on the hard drive of the computer running it
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 37
Reputation: jasondrey13 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jasondrey13 jasondrey13 is offline Offline
Light Poster

Help Re: help!! how do you save a variable to a...

  #2  
Jul 22nd, 2005
any one... please?
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: help!! how do you save a variable to a...

  #3  
Jul 22nd, 2005
What kind of information?
Reply With Quote  
Join Date: Jul 2005
Posts: 37
Reputation: jasondrey13 is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
jasondrey13 jasondrey13 is offline Offline
Light Poster

Re: help!! how do you save a variable to a...

  #4  
Jul 22nd, 2005
like all variables int,strings,float,bool,etc.
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: help!! how do you save a variable to a...

  #5  
Jul 22nd, 2005
Try this link, it's a pretty good explanation of File I/O.
http://cplus.about.com/od/beginnerct.../aa051802a.htm
Reply With Quote  
Join Date: Jul 2005
Posts: 26
Reputation: Rearden is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rearden's Avatar
Rearden Rearden is offline Offline
Light Poster

Re: help!! how do you save a variable to a...

  #6  
Jul 22nd, 2005
I read the about tutorial but is there a way to open a file without the using namespace std; declaration?

example:
#include<iostream>
#include<fstream>


int main()
{
std::fstream myFile("animals.txt",ios::app);

if (! myFile) // Always test file open
    {
        std::cout << "Error opening output file\n";
        return -1;
    }
    
myFile << "Hello, World!\n";
myFile.close();
return 0;

generates a whole slew of errors.
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: help!! how do you save a variable to a...

  #7  
Jul 22nd, 2005
I'm sure you could.
However:
using namespace std gives you access to the C++ standard library.
According to this book(C++: The complete reference) in my lap, the namespace is a declaritive region (whatever that means) that localizes the names of identifiers to prevent collisions (read, confusion).

It allows you to use the appropriate header files is the jist of it. Without the namespace std, you have to switch back to the following:

#include<iostream.h>
#include<fstream.h>

to get the correct results...
Maybe. It might not be that simple. Anyone else know with more certainty?

For most applications, you probably won't need to deviate from the std namespace anyway.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,449
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help!! how do you save a variable to a...

  #8  
Jul 22nd, 2005
>generates a whole slew of errors.

What's your compiler?
What are the errors? (As in, please post them.)
Reply With Quote  
Join Date: Jul 2005
Posts: 26
Reputation: Rearden is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Rearden's Avatar
Rearden Rearden is offline Offline
Light Poster

Re: help!! how do you save a variable to a...

  #9  
Jul 22nd, 2005
compiler g++

errors:
file.C: In function `int main()':
file.C:7: `ios' undeclared (first use this function)
file.C:7: (Each undeclared identifier is reported only once for each function
it appears in.)
file.C:7: parse error before `::' token
file.C:9: warning: the address of `std::fstream myFile(...)', will always be
`true'
file.C:15: invalid operands of types `std::fstream ()(...)' and `const char[15]
' to binary `operator<<'
file.C:16: request for member `close' in `myFile(...)', which is of
non-aggregate type `std::fstream ()(...)'
Reply With Quote  
Join Date: Jul 2005
Posts: 244
Reputation: Drowzee is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 4
Drowzee Drowzee is offline Offline
Posting Whiz in Training

Re: help!! how do you save a variable to a...

  #10  
Jul 22nd, 2005
on VC++ 6, Running that code gives me two errors...
'ios' is not a class or namespace name
'app' is an undeclared identifier.

Try this:
std::fstream myFile("animals.txt",std::ios::app);
ios is part of the std namespace, so you were trying to reference something that didn't exist to your compiler.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:28 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC