Quantcast
Channel: Embarcadero Community - Embarcadero Community
Viewing all articles
Browse latest Browse all 1963

How do I provide a String file name to fopen()?

$
0
0

I need to construct a file name as a String, and then use fopen() to find out if that file exists. However, it seems that fopen() won't accept a String as the filename parameter. The following generates an error:

FILE *in;

String filename = "";

// The value of the integer variable 'type' below is already determined elsewhere in the program.

if (type == 1) filename = filename + "firstfile";

if (type == 2) filename = filename + "secondfile";

filename = filename + ".txt";

bool    recordfileexists = false;

if ( (in = fopen(filename, "r")) != NULL) {

        recordfileexists = true;

        fclose(in);

    }

The error is: Cannot convert 'UnicodeString' to 'const char *'

So, I think I need to "cast" or convert 'filename' somehow. I tried filename.c_str()

if ( (in = fopen(filename.c_str(), "r")) != NULL) {

because numerous places around the Web suggested that, but that still doesn't work. That gives the error: Cannot convert 'wchar_t*' to 'constant char*' , so apparently that's not the right conversion. Then I tried using _wfopen(), since Help seemed to say that took wchar_t as the file name parameter.

if ( (in = _wfopen(filename.c_str(), "r")) != NULL) {

but that gives the error: Cannot convert 'char const[2]' to 'const wchar_t*'

I apologize for what I expect is a simple question about moving among types: char, wchar_t, char arrays, wchar_t arrays, and String. All help is appreciated.

River_Forest


Viewing all articles
Browse latest Browse all 1963

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>