•
•
•
•
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
![]() |
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
Try this link, it's a pretty good explanation of File I/O.
http://cplus.about.com/od/beginnerct.../aa051802a.htm
http://cplus.about.com/od/beginnerct.../aa051802a.htm
I read the about tutorial but is there a way to open a file without the using namespace std; declaration?
example:
generates a whole slew of errors.
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.
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
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.
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.
>generates a whole slew of errors.
What's your compiler?
What are the errors? (As in, please post them.)
What's your compiler?
What are the errors? (As in, please post them.)
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 ()(...)'
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 ()(...)'
•
•
Join Date: Jul 2005
Posts: 244
Reputation:
Rep Power: 4
Solved Threads: 4
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:
ios is part of the std namespace, so you were trying to reference something that didn't exist to your compiler.
'ios' is not a class or namespace name
'app' is an undeclared identifier.
Try this:
std::fstream myFile("animals.txt",std::ios::app);![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Similar Threads
- pascal save problem (Pascal and Delphi)
- variable environment (*nix Software)
- loop to create arrays when reading a file (Java)
Other Threads in the C++ Forum
- Previous Thread: is dave sinkula a little boy?
- Next Thread: require a little help with first program with class's

how do i save a variable to a file on the hard drive of the computer running it 
any one... please?

Linear Mode