C/C++ File Input & Output in C++: Why Use a String Literal?

AI Thread Summary
The discussion centers on file input and output in C++, specifically the issue of using a string variable for file paths instead of string literals. A user encountered problems when trying to open a file using a string variable, which only worked when the path was a string literal. The solution provided involved using the `c_str()` method to convert the string to a C-style string, allowing it to be accepted by the `ifstream` constructor. The conversation highlights that this limitation stems from backward compatibility issues with earlier versions of C++. It is noted that while this issue persisted through C++03, it was finally addressed in C++11, allowing for more modern handling of string objects in file operations.
Whovian
Messages
651
Reaction score
3
I felt like learning a bit about file input and output in C++. Tried some code, couldn't get it to work unless I made the path to the file a string literal, not a variable type. Why is this, and is there any way to get a string data type to be inputted as a path?
 
Technology news on Phys.org
You should be able to do it with the path stored in a variable. Show us the code you're talking about.
 
Whovian said:
I felt like learning a bit about file input and output in C++. Tried some code, couldn't get it to work unless I made the path to the file a string literal, not a variable type. Why is this, and is there any way to get a string data type to be inputted as a path?

I'm assuming you tried something like:
Code:
std::string filename("/path/to/file");
std::ifstream ifs(filename);
Did you?

If so, try:
Code:
std::string filename("/path/to/file");
std::ifstream ifs(filename.c_str());
 
I tried

Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string file = "Documents/Programming/C++.txt";
	string contents;
	ifstream in;
	ofstream out;
	out.open(file);
	out << "This is a C++ program here." << endl;
	out.close();
	in.open(file);
	getline(in,contents);
	in.close();
	cout << contents << endl;
	return 0;
	}

Note that I'm on a MAC, so that is a valid path. Using file.c_str seemed to work, thanks!
 
Whovian said:
Using file.c_str seemed to work, thanks!

You're welcome. :smile:
 
You'd think that the C++ ifstream constructor could take a C++ string as its argument. My understanding is that it's a backward-compatibility issue with ancient versions of C++ that pre-date the first standardized version (in which the 'string' data type was introduced).
 
You'd still think you could create another constructor that takes a string object; I don't see how any backwards compatibility issues would occur.
 
jhae2.718 said:
You'd still think you could create another constructor that takes a string object; I don't see how any backwards compatibility issues would occur.

Agreed.

You'd think they would have fixed this in C++03, but they didn't.
I wonder why, since they must have discussed this.

Anyway, apparently it is finally fixed in C++11.
 
Back
Top