When im using fstream.h its giving an error with using namespace std, how ever when i use simple fstream without .h , it is not giving error but not reading the file, How can i use fstream object using fstream.h header file along with using namespace std? Your urgent response will be highly appreciated,

#include <iostream>

using namespace std;
#include<fstream.h> //giving error
fstream file;

char a[20];

int main()
{
    file.open("doc.txt",ios::in);

    while (file>>a)
    {
        cout<<a<<endl;
    }

    file.close();
}

Edited 4 Years Ago by AmerJamil

#include<fstream.h> //Instead of this
#include<fstream>   // use this.

thats all.. you program start working..:-) :-)

Once you change fstream.h to fstream the program reads the file (for me anyway). If it still doesn't read the file for you as you stated, then there is another problem unrelated to "using namespace std" and "fstream". Maybe your doc.txt file is not in the right path and so the file is not found? But the file reads and prints out for me just fine.

Depends on the compiler you are using. If you are using Turbo C++ then you can't code the using namespace std: option because that compiler doesn't understand it. You have to use fstream.h with that compiler.

If, on the otherhand, you are using one of the modern c++ compilers then you have to use <fstream> -- without the *.h extension.

i have the similar problem
if get the error, "ifstream undeclared, first use this function"
i got the code from internet, but i wonder why is it not working. can somebody please help.

void GetInput()
{
    ifstream in("input.txt");   // that is where the error is generated.
    in >> n; // read the number of vertices first
    for (int i=0; i < n; i++) // then read the adjacency matrix        
        for (int j=0; j < n; j++)
            in >> a[i][j]; // read the element one by one
    in.close();

i got the code from internet, but i wonder why is it not working. can somebody please help.

Read the thread you posted to, then follow the advice. Your problem is not unique.

This article has been dead for over six months. Start a new discussion instead.