file does not open .

Hi ,
I am not able to open the file ..the file is located in the folder where the output.exe is located .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string> 
#include <map>

using namespace std ;


int _tmain(int argc, _TCHAR* argv[])
{
	
	string line , str1;  
	map<string , string > mymap ; 
	size_t position  = 0 ; 
	map<string , string>::iterator it ; 
	
	ifstream file1("test01", ios::in);
	if(file1.is_open())
	{
		while( !file1.eof() )
		{
			getline( file1, line) ; 
			position   = line.find(" " ) ; 		
			mymap.insert ( pair<string,string>(line.substr(0 , position   ), line.substr(position) ) );
		};
		for( it = mymap.begin() ; it != mymap.end(); it++)
			cout<<(*it).first<<"\t"<<(*it).second<<endl;
	}
	else
		return 0;

	file1.close();

	return 0;
}


2) can i put the file to other location .by giving the full path of the file. using ifstream.
Are you sure the file name is "test01" and not "test01.txt"? Remember that Windows likes to make people ignorant by hiding file extensions.

2. Yes, you can use \\ or / for the slashes.
exactly ...its test01.txt but hidden .. shows only "test01" but even i put "test01.txt" it does not open.
1
2
3
4
5
6
7
#include <iostream>
#include <fstream>

int main()
{
    std::cout << (std::ifstream("test01.txt").is_open() ? "Opened" : "Didn't Open") << std::endl;
}
Does this work for you?
Last edited on
getting "Didn't Open" using both the option test01.txt and test01
Just guessing: the file must be on your working directory, the directory from where you run your program:

1
2
3
$ ls
program test01.txt
$ ./program

Will work, but

1
2
3
4
5
$ ls
some_folder test01.txt
$ ls some_folder
program
$ some_folder/program

won't. (I know you're using Windows, but I hope you understand).
Are you running the built executable directly or are you running it from your IDE?
bbgst ) its in the working directory ..

LB ) I am running from IDE VS 2005
Ah, I'll bet then that the EXE is in some subfolder (like Debug) and that that is where you have the file01.txt? If you run it from your IDE it needs to be in the project directory, not the EXE directory.
Thanks a lot L B ... :) it worked like a magic ..
Topic archived. No new replies allowed.