473,394 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

file input problem

string fileName;

cout<<"Enter filename of datafile you wish to open \n";
cin>>fileName;

ifstream fin;

fin.open(fileName, ifstream::ios);
if(!fin)
{
cerr<<"blah blah blah";
exit(1);
}
i get this error message in VC++ .....Error C2664...on the
fin.open(fileName, ifstream::ios); line...cannot convert parameter 1 from
std::string to const char *
i cannot put a certain filename down such as fin.open("textfile.txt"), i
need the user to enter its name, for it will change.

Thanks alot in ahead!
Jul 19 '05 #1
4 7255

"pentiumPunk" <Br***@triad.rr.com> wrote in message
news:wp*********************@twister.southeast.rr. com...
string fileName;

cout<<"Enter filename of datafile you wish to open \n";
cin>>fileName;

ifstream fin;

fin.open(fileName, ifstream::ios);
if(!fin)
{
cerr<<"blah blah blah";
exit(1);
}
i get this error message in VC++ .....Error C2664...on the
fin.open(fileName, ifstream::ios); line...cannot convert parameter 1 from
std::string to const char *
i cannot put a certain filename down such as fin.open("textfile.txt"), i
need the user to enter its name, for it will change.

Thanks alot in ahead!


fin.open(fileName.c_str(), ifstream::ios);

As the error message says, open requires a const char*, c_str returns a
const char* from a string.

Its just one of the quirks of the standard library.

john
Jul 19 '05 #2
"John Harrison" <jo*************@hotmail.com> wrote in message news:<bj************@ID-196037.news.uni-berlin.de>...
"pentiumPunk" <Br***@triad.rr.com> wrote in message
news:wp*********************@twister.southeast.rr. com...
string fileName;

cout<<"Enter filename of datafile you wish to open \n";
cin>>fileName;

ifstream fin;

fin.open(fileName, ifstream::ios);
if(!fin)
{
cerr<<"blah blah blah";
exit(1);
}
i get this error message in VC++ .....Error C2664...on the
fin.open(fileName, ifstream::ios); line...cannot convert parameter 1 from
std::string to const char *
i cannot put a certain filename down such as fin.open("textfile.txt"), i
need the user to enter its name, for it will change.

Thanks alot in ahead!

When we declare any string variable it is not null terminated ie not
appended with '\0' .Since in the above program u r using fstream which
requires filename to be null terminated so we can make it null
terminated by using c_str() which convert it to null terminated string
..ie use
fin.open(filename.c_str(),ifstream::ios) instead of fin.open(fileName,
ifstream::ios)

Enjoy c++ programming........
Jul 19 '05 #3
pentiumPunk wrote:
string fileName;

cout<<"Enter filename of datafile you wish to open \n";
cin>>fileName;

ifstream fin;

fin.open(fileName, ifstream::ios);
if(!fin)
{
cerr<<"blah blah blah";
exit(1);
}
i get this error message in VC++ .....Error C2664...on the
fin.open(fileName, ifstream::ios); line...cannot convert parameter 1 from
std::string to const char *
i cannot put a certain filename down such as fin.open("textfile.txt"), i
need the user to enter its name, for it will change.

Thanks alot in ahead!


1. As others have stated:
fin.open(filename.c_str());
or
ifstream fin(filename.c_str());

2. What kind of file mode is "ifstream::ios"?
Try a combination of: "ios_base::in", "ios_base::out",
or "ios_base::binary". The default mode for ifstream
is "ios_base::in" as a text file.

3. Don't use the exit() function. It has some nasty quirks to it.
Use the return statement instead.

4. Instead of returning a '1' from main() to the operating system,
use the predefined, portable constants EXIT_SUCCESS or
EXIT_FAILURE as defined in <cstdlib>.

5. You may want to change:
cin >> fileName;
to
getline(cin, filename, '\n');
The getline() function will read in the whole line. The other
expression _may_ not read in spaces or other characters.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 19 '05 #4
Sandeep wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:<bj************@ID-196037.news.uni-berlin.de>...
"pentiumPunk" <Br***@triad.rr.com> wrote in message
news:wp*********************@twister.southeast.rr. com...
> string fileName;
>
> cout<<"Enter filename of datafile you wish to open \n";
> cin>>fileName;
>
> ifstream fin;
>
> fin.open(fileName, ifstream::ios);
> if(!fin)
> {
> cerr<<"blah blah blah";
> exit(1);
> }
>
>
> i get this error message in VC++ .....Error C2664...on the
> fin.open(fileName, ifstream::ios); line...cannot convert parameter
> 1 from std::string to const char *
> i cannot put a certain filename down such as
> fin.open("textfile.txt"), i need the user to enter its name, for it
> will change.
>
> Thanks alot in ahead!
>

When we declare any string variable it is not null terminated ie not
appended with '\0' .


What do you mean by "any string variable"? If you're talking about
std::string, then it's not defined (and shouldn't matter) how the data
is stored internally. The only thing that matters is that c_str()
returns a null terminated array of the string's content.
An implementation of std::string that works on null terminated
characters internally (and thus doesn't need any extra work for
c_str() would be valid, too.
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: cipher | last post by:
ok, im gonna try and explain this as best i can... i have an AS/400 program that generates big, ugly, messy reports. i need to read these ASCII reports in and output meaningful data. the good news...
5
by: Alon Zilkha | last post by:
Hi All, I just noticed a problem with Internet Explorer on Service Pack 2 of Windows XP and it is driving me insane!! I would greatly appreciate it if someone could help me out... I have a...
1
by: pentiumPunk | last post by:
string fileName; cout<<"Enter filename of datafile you wish to open \n"; cin>>fileName; ifstream fin; fin.open(fileName, ifstream::ios); if(!fin) {
6
by: a | last post by:
(I've reached that familiar place where I've got a nagging little problem in a program I'm writing but I've been staring at code for too long and I probably wouldn't be able to recognize the answer...
7
by: garthb | last post by:
Hello, In Mozilla (Firefox 1.0.7) I can cloneNode a file input element that has a selected file value and appendChild it to another form without a problem. IE 6 loses the selected file value....
6
by: tshad | last post by:
I have an upload file input as: <input id="MyFile" style="width:300px" type="File" runat="Server"> This works fine, but I find that if my page doesn't pass validation during postback, the page...
7
by: ljuljacka | last post by:
I'm just trying to run a fileupload script from the manual, just to see how it works, and it won't. I've checked if file upload is enabled and it is. Also, the file I'm trying to upload is smaller...
3
by: John Williams | last post by:
I'm writing a stagenography program to experiment with how it works. The algorithm I'm using appears to be producing the correct result...however I'm struggling with the file input. I never...
3
by: Rambaldi | last post by:
Wassup!!! <tr> <td> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <!-- Name of input element determines name...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.