Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

File handling in G++

Posted on 2004-04-26
8
Medium Priority
?
657 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
  • 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

886 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