954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can seem to open my file

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;
}
proxystub
Newbie Poster
5 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 
but its just not working



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

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

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"

proxystub
Newbie Poster
5 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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[code=c] 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:

#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;
 }
Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 
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");

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 

Sorry my mouse is sticky.

Lazaro Claiborn
Junior Poster
171 posts since Jan 2007
Reputation Points: 11
Solved Threads: 13
 
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.

proxystub
Newbie Poster
5 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

thanks for the tip on [code=cplusplus] will use it when next i post.

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

proxystub
Newbie Poster
5 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

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

proxystub
Newbie Poster
5 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You