Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 659
  • Last Modified:

File handling in G++

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
combiofriends
Asked:
combiofriends
  • 4
2 Solutions
 
MafaldaCommented:
I would suggest

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

fstream fin("Myfile.txt", ios_base::in);
0
 
MafaldaCommented:
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
 
MafaldaCommented:
What error do you receive ?
A compilation error or a runtime error ?
For a runtime error look where you put the file ..
0
 
AxterCommented:
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
 
MafaldaCommented:
I think that both axter and myself gave a solution.
I recommend a split
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now