Hello guys i need some help,
i need a program to read a text file. I write this:

#include <iostream.h>
#include <fstream.h>
#include <cstring.h>
void main () {
int is_open;
  string line;
  ifstream myfile;
  myfile.open ("a.txt");
  if (myfile.is_open())
  {
	 while (! myfile.eof() )
	 {
		getline (myfile,line);
		cout << line << endl;
	 }
	 myfile.close();
  }

  else cout << "Unable to open file";


}

and now i have a error " 'is_open' is not a member of 'ifstream' in function main()"

Dani AI

Generated

Short diagnosis and fixes (for ): the error usually means your compiler/library does not expose the modern is_open() member on std::ifstream (old Turbo/Borland iostreams predate the standardized API), or your code is mixing obsolete headers and identifiers. was right to suspect an ancient toolchain. In modern C++ std::ifstream::is_open() exists and simply tells whether the stream is associated with a file. (en.cppreference.com)

A small, safe modern rewrite that avoids eof()-based loops and uses the stream state test instead:

#include <fstream>
#include <iostream>
#include <string>

int main()
{
    std::ifstream in("a.txt");
    if (!in) {
        std::cerr << "Unable to open file\n";
        return 1;
    }

    std::string line;
    while (std::getline(in, line)) {
        std::cout << line << '\n';
    }
    return 0;
}

Troubleshooting notes: remove the stray int is_open; in your function (it is unnecessary and confusing). If you cannot upgrade the compiler, test success with if (myfile) or if (!myfile.fail()) instead of is_open() and consult your compiler docs for the exact <fstream> API. and pointed toward alternatives (filebuf/open checks and fail()), which are valid workarounds on older runtimes. Upgrading to a modern compiler (GCC/Clang/MSVC or a recent C++Builder) is the cleanest long-term fix.

Recommended Answers

All 3 Replies


Maybe it's something to do with your obsolete compiler, with its obsolete headers.

Compilers which accept "#include <iostream.h>" and void main are really not good anymore.

commented: Yup +27

You have to make a file buffer and use file descriptor such as:

 int fd;
 fd=open("d:\\or.txt",O_RDWR | O_CREAT);
 filebuf iofile(fd);

 if(!iofile.is_open())
 {
   cerr<<"The filebuf is not open ";
   return(1);
 }

 iofile.sputn("KaVi International", 21);

As you see when you use is_open it corresponds to file buffer and thus it would show you error of not supported type if you not use it with file buffer. Try this

This is simple.. try this...

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
 ofstream f1;
 f1.open("Sen.txt",ios::out|ios::trunc);
 if(!f1.fail())
 {
   f1<<"Hello Senthil";
   cout<<"Data written successfully!";
 }
 else
  cout<<"Error";
 getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.