Please support our C++ advertiser: Programming Forums
Views: 3449 | 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.)
High Plains Blogger #plains #lounge ## I, for one, welcome our new socialist overlords.
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
"Capitalism is the unequal distribution of wealth. Socialism is the equal distribution of poverty."
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)




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

Hybrid Mode