Solved

File handling in G++

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 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