Solved

File handling in G++

Posted on 2004-04-26
8
642 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 25 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 25 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now