Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File handling in G++

Posted on 2004-04-26
8
Medium Priority
?
654 Views
Last Modified: 2010-04-01
Hi,
 I want to know the syntax for initializing a stream for input using g++.
What's wrong with:

#include<fstream>
....
ifstream fin;
fin.open("Myfile.txt");

0
Comment
Question by:combiofriends
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
8 Comments
 
LVL 6

Expert Comment

by:Mafalda
ID: 10918855
I would suggest

#include <fstream>
using namespace std; // probably you forgot to specify the namespace usage

fstream fin("Myfile.txt", ios_base::in);
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10918866
If you insist on using ifstream then

#include<fstream>
using namespace std;

....
ifstream fin;
fin.open("Myfile.txt");

or
#include<fstream>
using std::ifstream;

....
ifstream fin;
fin.open("Myfile.txt");

0
 
LVL 6

Accepted Solution

by:
Mafalda earned 75 total points
ID: 10918886
What error do you receive ?
A compilation error or a runtime error ?
For a runtime error look where you put the file ..
0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 75 total points
ID: 10922905
I recommend using prefix, instead of adding the type to the global namespace.

#include<fstream>


std::ifstream fin;  //Put std:: in front
fin.open("Myfile.txt");
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 11194431
I think that both axter and myself gave a solution.
I recommend a split
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question