[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

file.open(

Mark Casternoff

10/17/2008 1:26:00 AM

I'm playing around with opening files but have run into a wall.

Here's a stripped down, simple version of what I have. The problem seems
to be the datatype of filename passed into openfile( ). The error message
alludes to wchar_t, so I changed filename from string to wchar_t to see what
would happen. Then to const wchar_t. Neither option worked.

I'm sure it's something simple. Any ideas?


#include <iostream>
#include <fstream>
#include <string>

ifstream infile;
ofstream outfile;

int openfile(string filename, char mode) {
if (mode == 'i') {
infile.open(filename, ios::in);
}

if (mode == 'o') {
outfile.open(filename, ios::out);
}

return 0;
}

int main() {
openfile("foo.txt", 'i');
return 0;
}

2 Answers

Ian Collins

10/17/2008 1:39:00 AM

0

Mark Casternoff wrote:
> I'm playing around with opening files but have run into a wall.
>
> Here's a stripped down, simple version of what I have. The problem seems
> to be the datatype of filename passed into openfile( ). The error message
> alludes to wchar_t, so I changed filename from string to wchar_t to see
> what
> would happen. Then to const wchar_t. Neither option worked.
>
> I'm sure it's something simple. Any ideas?
>
>
> #include <iostream>
> #include <fstream>
> #include <string>
>
> ifstream infile;
> ofstream outfile;
>
> int openfile(string filename, char mode) {
> if (mode == 'i') {
> infile.open(filename, ios::in);

The first parameter of open in const char*, so you want
filename.c_str(). Daft, but that's the way it is.

--
Ian Collins

Mark Casternoff

10/17/2008 10:52:00 PM

0

On 2008-10-16 21:39:18 -0400, Ian Collins <ian-news@hotmail.com> said:

> Mark Casternoff wrote:
>> I'm playing around with opening files but have run into a wall.
>>
>> infile.open(filename, ios::in);
>
> The first parameter of open in const char*, so you want
> filename.c_str(). Daft, but that's the way it is.

Wow. That worked. Thanks much.