cant seem to read from this my file "d.txt", i know this is quite simple
code but its just not working, and yes file "d.txt" is in the same directory as the .cpp & .exe

#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
int main()

{
      char ch;
      //int check = 0;
      ifstream file;
      file.open("d.txt");
      if(file.is_open())
      {
            while (!file.eof())
            {
            //check = check + 1;
            file.get(ch);
            cout<<ch;
            }
            cout<<endl;
      }
      else
      {
            cout<<"file could not be found"<<endl;
      }
      system("PAUSE");
      return 0;
}

but its just not working

What's not working then? Does it compile? What's your output?

although Ive opened the file using object.open("") the condition to check that there is indeed an open file always fails and i get my else message "file could not be found"

cant seem to read from this my file "d.txt", i know this is quite simple
code but its just not working, and yes file "d.txt" is in the same directory as the .cpp & .exe

#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
int main()

{
      char ch;
      //int check = 0;
      ifstream file;
      file.open("d.txt");
      if(file.is_open())
      {
            while (!file.eof())
            {
            //check = check + 1;
            file.get(ch);
            cout<<ch;
            }
            cout<<endl;
      }
      else
      {
            cout<<"file could not be found"<<endl;
      }
      system("PAUSE");
      return 0;
}

Hello, when you use the code tags, code is not sufficient. For c source code you'd specify the code tag as

and for cplusplus code=cplusplus and both have the same closing tag code. Try to use them. :)

Where do you close the file in this program (i.e. file.close())? And you can specify the using namespace std;  after the includes to know which namespace is being used when you use cout, cin etc - if you compiler comforms to the ansi/iso c++ draft standard. I've modified the code to the following, let me know if you still have problems:

[code=cplusplus]
#include <iostream>
#include <fstream>
#include <stdlib>

using namespace std;
 
  int main()
  {
       char ch;
       //int check = 0;
       ifstream file;
       file.open("d.txt");
       if(file.is_open())
       {
             while (!file.eof())
             {
             //check = check + 1;
             file.get(ch);
             cout<<ch;
             }
            cout<<endl;
           file.close();
       }
       else
       {
             cout<<"file could not be found"<<endl;
       }
       system("PAUSE");
       return 0;
 }

although Ive opened the file using object.open("") the condition to check that there is indeed an open file always fails and i get my else message "file could not be found"

make sure the file d.txt is in the same directory (folder) as that the executable file is running. If they are not in the same directory then give the full path to the file in the open() function, for example if the file is located in c:\mydir then open the file like this: file.open("c:\\mydir\\d.txt"); the above assumes you are working on MS-Windows, if you are on *nix file.open("/mydir/d.txt");

Also I've noticed the "file" is colored. Is that a key word? That I'm not sure of. If it is, try to change the name to files to see if it'll work that way.

Good luck, LamaBot

make sure the file d.txt is in the same directory (folder) as that the executable file is running. If they are not in the same directory then give the full path to the file in the open() function, for example if the file is located in c:\mydir then open the file like this: file.open("c:\\mydir\\d.txt"); the above assumes you are working on MS-Windows, if you are on *nix file.open("/mydir/d.txt");

Yes my txt file d.txt is in the same directory as my .exe ive even changed the code to explicitly point to "d:\\c++\\d.txt" but still the program fails the check for an open file and displays "file not found"

by the way im using the dev c++ compiler.

thanks for the tip on will use it when next i post. Ive tried your suggestions but still come up with "file not found"[code=cplusplus] will use it when next i post.

Ive tried your suggestions but still come up with "file not found"

Thanks all i forgot that while i was try to debug this small piece of code that i had changed the file name to dx.txt, so how that i explicitly point to "d:\\c++\\d.txt" it works

Howerver when reverting back to "d.txt" it doesnt. I assumed that the C++ complier would read the text file from the same directory as the .exe but this does not seem to be the case and may need to be set under complier options


Thanks again ALL for your help

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.