I can't seem to find how to achieve this with fstream

I am trying to open a file that the user specifies and then read from that file. Here is my code. I cannot seem to get fstream to work with the cin file name.
Any help will be much appreciated.
Thanks!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

int main()
{
char file[25];
std::cout << "***********************" << std::endl;
std::cout << "*  G R A D E B O O K  *" << std::endl;
std::cout << "***********************" << std::endl;
std::cout << "Please enter the name of the grade file: ";
std::cin >> file;

if (file)
std::cout << "Grade information read successfully..." << std::endl;
else
	std::cout << "Grade information  not read correctly..." << std::endl;

std::cout << "Menu" << std::endl;
std::cout << "----" << std::endl;

}
http://www.cplusplus.com/doc/tutorial/files/

use std::string file;
check the value of file in a debugger, maybe you need to write "\\" for each new folder
Last edited on
i have tried, but cannot get it to work with a file determined by the user.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>

int main()
{
char file[25];
std::cout << "***********************" << std::endl;
std::cout << "*  G R A D E B O O K  *" << std::endl;
std::cout << "***********************" << std::endl;
std::cout << "Please enter the name of the grade file: ";
std::cin >> file;
std::ofstream file;

file.open();

if (file)
std::cout << "Grade information read successfully..." << std::endl;
else
	std::cout << "Grade information  not read correctly..." << std::endl;

std::cout << "Menu" << std::endl;
std::cout << "----" << std::endl;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::string filepath, output;
	filepath="D:\\test.txt";
	std::ifstream file;
	file.open(filepath.c_str());

	if(file.is_open()){
		while(!file.eof()){
		file >> output;
		std::cout << output;
		}
	}
	file.close();
}



next time read the tutorial ;)
ofstream is for output, that's mentioned in the first line
Last edited on
on line 10 of your code is the same in mine, but eclipse is telling me that "statement cannot resolve address of overloaded function"
yes, its the same but my file is an ifstream and yours an ofstream
What compiler do you use?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
#include <string>
int main()
{

std::string file;
std::cout << "***********************" << std::endl;
std::cout << "*  G R A D E B O O K  *" << std::endl;
std::cout << "***********************" << std::endl;
std::cout << "Please enter the name of the grade file: ";
std::cin >> file;

std::ifstream inFile;
inFile.open(file.c_str());



if (inFile)
	std::cout << "Grade information read successfully..." << std::endl;
else
	std::cout << "Grade information  not read correctly..." << std::endl;

}


I am getting the error here:
 
inFile.open(file.c_str());


scratch that, i'm not getting an error now...weird, but every time it says my file is not read correctly, what could cause this?
Last edited on
What is the filename and what compiler are you using?
linux g++
the file name is input.txt, it's all in my eclipse workspace together
Ah - ok. In Visual Studio when I debug it uses a directories relative to the project file, maybe your compiler is having a similar issue?
I don't see how it could since it's all in the workspace
Sorry - I am not really familiar IDE s.

Try changing the if statement from if (inFile) to if (inFile.is_open())
it still prints out the else portion every time
Ok, try sending the whole pathname as the filename: something like "C:\Users\...Additional Directories...\input.txt"
ok, so it works that way. Now to figure out why it won't work with the user input...hmmm
Its likely an issue where the program tries to find the path or file relative to the program itself, or relative to the workspace.
Last edited on
Topic archived. No new replies allowed.