Why my file won't open?

This is a discussion on Why my file won't open? within the C++ Programming forums, part of the General Programming Boards category; I am trying to open a file with a program but it keeps failing. I typed the file into notepad ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    23

    Why my file won't open?

    I am trying to open a file with a program but it keeps failing. I typed the file into notepad and saved it as a .dat file. Does anyone have any ideas why this is happening?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string filename = "bank.dat";
    	ifstream inFile;
    
    	inFile.open(filename.c_str());
    
    	if(inFile.fail())
    	{
    		cout<<"\nThe file named "<<filename<<" was not successfully opened"
    			<<"\nPlease check that the file currently exists."
    			<<endl;
    		exit(1);
    	}
    	cout<<"\nThe file has been successfully opened for reading."
    		<<endl;
    
    	return 0;
    }

  2. #2
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Make sure that you didn't accidentally name the file "bank.dat.txt", that is most likely what happened.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    It's very hard to tell why because you haven't given us any information - such as WHY it fails. Every function allows you to get an error of some sort, telling what actually occoured and why it failed.
    As mikeman118 also mentioned, make sure the filename isn't actually bank.dat.txt (remember that Windows has a bad habit of hiding known extensions by default; thus hiding the ".txt" extension).

  4. #4
    A10
    A10 is offline
    Registered User
    Join Date
    Nov 2006
    Posts
    85
    Quote Originally Posted by Elysia View Post
    It's very hard to tell why because you haven't given us any information - such as WHY it fails. Every function allows you to get an error of some sort, telling what actually occoured and why it failed.
    As mikeman118 also mentioned, make sure the filename isn't actually bank.dat.txt (remember that Windows has a bad habit of hiding known extensions by default; thus hiding the ".txt" extension).
    if this is the problem then open up an explorer window and go to tools->folder options->view and make sure that the Hide extensions for known file types box is unchecked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21