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

Nov 12, 2012 at 12:11am
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;

}
Nov 12, 2012 at 12:29am
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 Nov 12, 2012 at 12:32am
Nov 12, 2012 at 12:38am
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;

}
Nov 12, 2012 at 12:49am
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 Nov 12, 2012 at 12:51am
Nov 12, 2012 at 6:31pm
on line 10 of your code is the same in mine, but eclipse is telling me that "statement cannot resolve address of overloaded function"
Nov 12, 2012 at 6:36pm
yes, its the same but my file is an ifstream and yours an ofstream
Nov 12, 2012 at 6:38pm
What compiler do you use?
Nov 12, 2012 at 6:40pm
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 Nov 12, 2012 at 6:43pm
Nov 12, 2012 at 6:43pm
What is the filename and what compiler are you using?
Nov 12, 2012 at 6:43pm
linux g++
Nov 12, 2012 at 6:47pm
the file name is input.txt, it's all in my eclipse workspace together
Nov 12, 2012 at 6:50pm
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?
Nov 12, 2012 at 6:52pm
I don't see how it could since it's all in the workspace
Nov 12, 2012 at 6:55pm
Sorry - I am not really familiar IDE s.

Try changing the if statement from if (inFile) to if (inFile.is_open())
Nov 12, 2012 at 7:02pm
it still prints out the else portion every time
Nov 12, 2012 at 7:10pm
Ok, try sending the whole pathname as the filename: something like "C:\Users\...Additional Directories...\input.txt"
Nov 12, 2012 at 7:24pm
ok, so it works that way. Now to figure out why it won't work with the user input...hmmm
Nov 12, 2012 at 7:28pm
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 Nov 12, 2012 at 7:29pm
Topic archived. No new replies allowed.