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

Click For Summary

Discussion Overview

The discussion revolves around the use of string literals versus string variables in file input and output operations in C++. Participants explore the reasons behind the necessity of using string literals for file paths and whether string data types can be effectively used instead.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant reports difficulty in using a variable for a file path, suggesting that only string literals work.
  • Another participant challenges this by asking for the code and asserts that it should work with a variable.
  • A suggestion is made to use the .c_str() method on the string variable when passing it to ifstream.
  • A participant shares a code snippet demonstrating successful file operations using a string variable and confirms that using .c_str() resolved their issue.
  • There is a discussion about the ifstream constructor's limitations, with one participant attributing it to backward compatibility issues with earlier versions of C++.
  • Another participant questions the necessity of these limitations and expresses confusion over why they persist despite updates in later C++ standards.
  • It is noted that the issue is addressed in C++11, suggesting improvements in handling string objects.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of using string literals versus string variables for file paths. While some agree that using .c_str() resolves the issue, there is no consensus on the underlying reasons for the limitations of the ifstream constructor.

Contextual Notes

Participants mention the historical context of C++ standards and the evolution of the string data type, indicating that the discussion is influenced by the language's development over time.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
55
Views
7K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 118 ·
4
Replies
118
Views
10K