Thread: Unable to locate txt file for ifstream opening

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Unable to locate txt file for ifstream opening

    Hi,

    I am trying to read from a txt file but when I use :-

    Code:
    	ifstream StreamName;
    
    	StreamName.open("Dieters.txt");
    It fails to locate the txt file, which is what is holding the data I wish to use.

    I believe my actual code is correct, and so believe it is just that Visual Studio doesn't know where to locate the file.

    Here is my code:-

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    const int MAXPATIENTS = 10;
    
    struct patient
    {
    	int id;
    	double weight;
    	double height;
    	bool ismale;
    };
    
    int main()
    {
    	patient patientstruct[MAXPATIENTS];//array of structs
    
    	ifstream StreamName;
    
    	StreamName.open("Dieters.txt");
    
    	if(!StreamName)
    	{
    		cout << "phail " << endl;
    	}
    
    	else
    	{
    		cout << "opened" << endl;
    	}
    
    
    	system("pause");
    
    	return 0;
    }
    This is for some practice on a test we had last week, and we were given a pre-made Microsoft Visual Studio Solution file, which fired straight up and contained the txt file, but we use a strange college specific library.

    So basically I'm asking how I can use the StreamName.open("Dieters.txt"); line to point to the location of the txt file so that V.Studio can find it, as the code I have posted above works fine when I don't have to use the weird library.

    Many thanks for any help!

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Scranton, Pa
    Posts
    252
    As it stands, your program will only find the text file if it's residing in the same directory as your executable.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thank you Oldman47

    Appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in opening a txt file
    By arian in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2009, 01:17 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Problems in opening a txt file in a loop
    By anilk in forum C Programming
    Replies: 1
    Last Post: 02-05-2006, 03:35 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM