how to open file from c++

guys, how to open file from c++
this is my coding, can you help me to fix it??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 	{
		cout<<"Find subject that you want: "<<endl;
		cin>>b;
		strcat("D:/TUGAS/1/1",b);
		ifstream data;
		data.open(b);
		while(data)
		{
			data.getline(b,sizeof(b));
			cout<<b<<endl;
		}
		data.close();
	}
	system("pause");
}


if i execute it, it always can't open the file...
1) if your files will be in program execution folder, there is no need to specify full path.
2) strcat("D:/TUGAS/1/1",b); You cannot do that. It will try to append b string to const literal, what is forbidden but will work because of compatibility conversion to char* (Should give you a warning).
3) Even it it would work, you forgot another slash at the end of line, so if you enter "test", you wil be trying to open "D:/TUGAS/1/1test"
so the strcat("D:/TUGAS/1/1",b); won't work? what the solution for this? must we replace the code?
If file in same folder your program is, you can use just:
1
2
std::cin >> b;
std::istream data(b);

If not:
1
2
3
4
5
char path[40] = "D:/TUGAS/1/1/"; //Copies string into array.
char fliename[20];
std::cin >> filename;
std::strncat(path, filename, 20);
std::ifstream data(path);
here's something about windows:
1)- windows -unlike linux- uses a backslash, not a slash.
2)- and in c++, you can't write a backslash directly, to write a backslash in a string literal you should write two backslashes.
replace line 6 with this:
 
data.open("D:\\TUGAS\\1\\1");


i think that would work.
windows -unlike linux- uses a backslash, not a slash.
From at least Vista it supports forward slashes internally.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
		char path[40] = "D:\\TUGAS\\1\\1\\"; //Copies string into array.
		char filename[20];
		cout<<"Masukkan nama mata kuliah yang dicari: "<<endl;
		cin >> filename;
		strncat(path, filename, 20);
		ifstream data(path);
		data.open("D:\\TUGAS\\1\\1\\");
		while(data)
		{
			data.getline(b,sizeof(b));
			cout<<b<<endl;
		}
		data.close();
	}
	system("pause");
}


ehm, it's not wrong, but the file just can't open... -___-" i'm so confused
1
2
		ifstream data(path);
		data.open("D:\\TUGAS\\1\\1\\");//Terribly wrong. Do not do this. 

Also if you want to use backslashes, use raw string literals:
1
2
3
4
5
6
7
8
char path[40] = R"(D:\TUGAS\1\1\)"; //Copies string into array.
char fliename[20];
std::cin >> filename;
std::strncat(path, filename, 20);
std::ifstream data(path);
while(data.getline(b,sizeof(b))) { 
    std::cout<<b<<endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
		char path[40] = "(D:\TUGAS\1\1\)"; 
		char filename[20];
		cout<<"find subject: "<<endl;
		cin >> filename;
		strncat(path, filename, 20);
		ifstream data(path);
		while(data.getline(b,sizeof(b)))
		{ 

			cout<<b<<endl;}

		data.close();
	}
	system("pause");
}


guys, it's the same work, but the file can't open, i'm hopeless T_T
Does such file exist?
Show a screenshot of that folder.
What do you input exactly?
You do not forget file extension, do you?
i give you my full code, sorry if it's in my language
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

int maen()
{
	ofstream myfile;
	myfile.open("Algolanjut.txt");
	myfile<<"NIM		Nama		Program Studi	Tugas		UTS		UAS		TOTAL	HURUF	"<<endl;
	myfile<<"============================================================================================================="<<endl;
	myfile<<"535050056	Budi		TI		90		80		70		77	B	"<<endl;
	myfile<<"535090065	Wati		TI		100		100		99		99.5	A	"<<endl;
	myfile<<"835100077	Tina		SI		90		70		50		64	C	"<<endl;
	myfile.close();
	return 0;
}

void main()
{
	char b[30], c[30],nama[30];
	int a;
	cout<<" 1. Menambah mata kuliah baru " <<endl;
	cout<<" 2. Mencari mata kuliah " <<endl;
	cout<<" Masukkan input 1 atau 2 : ";
	cin>>a;
	if (a==1)
	{	
		cout<<"Masukkan nama mata kuliah baru: "<<endl;
		cin>>c;
		ofstream myfile;
		myfile.open(strcat(c,".txt"));
		myfile.close();
	}
	if (a==2)
	{
		char path[40] = "(D:\TUGAS\1\1\)"; //Copies string into array.
		char filename[20];
		cout<<"Masukkan nama mata kuliah yg dicari: "<<endl;
		cin >> filename;
		strncat(path, filename, 20);
		ifstream data(path);
		while(data.getline(b,sizeof(b)))
		{ 

			cout<<b<<endl;}

		data.close();
	}
system("pause");
}


try my coding
1
2
char path[40] = R"(D:\TUGAS\1\1\)";
char path[40] = "(D:\TUGAS\1\1\)";
Find the difference

Also show how you call your file and what file you actually have. I'm supposing you are not providing file extension.
Last edited on
Don't call open/close on a stream.
You specify \ in a string literal with \\
Use std::string when you need a string.

Applying those, that code becomes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
	std::string path = "D:\\TUGAS\\1\\1\\"; 
//	std::string path = "D:/TUGAS/1/1/";  // or this 
	std::string filename;
	std::cout<<"find subject: ";
	std::cin >> filename;
	path += filename;

	std::string line;
	ifstream data(path);
	while (std::getline(data, line))
		std::cout << line << std::endl;

	system("pause");
}
what the use of "R", if i add it, the program error, it's say call 1 or 2, if i call 2, we type the file that we search, if it in the folder, the file will open, if not, then it's end of program
what the use of "R", if i add it, the program error
Oh, you have an outdated compiler which doesn't support C++11.
Ok, use "D:\\TUGAS\\1\\1\\"
I want to see, what exactly you are typing, and screenshot of folder with actual files.
Example
I am typing "Velocities.xls"
My folder looks like http://gm4.in/i/du0.png
Last edited on
kbw, it won't work --"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
	std::string path = "D:/TUGAS/1/1/"; 
	std::string filename;
	std::cout<<"find subject: ";
	std::cin >> filename;
	path += filename;

	std::string line;
	ifstream data(path);
	while (std::getline(data, line))
		std::cout << line << std::endl;

	system("pause");
}
http://imageshack.us/photo/my-images/571/20696473.png/
that's my file, i want to open kalku or kalku2, but it become
http://imageshack.us/photo/my-images/443/97196496.png/

but the kalku or kalku2 text document, won't open.
As I supposed you are missing extension. It is actually kalku.txt . Windows hides it by default, but it is still part of the file.
http://windows.microsoft.com/en-in/windows-vista/show-or-hide-file-name-extensions
Last edited on
kbw, it won't work --"
Why do you say that?
kbw, beacuse the program can't open the file
thanks to MiiNiPaa, you really help me, but the file still can't open,thank you :')
Topic archived. No new replies allowed.