473,789 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

file input problem

string fileName;

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

ifstream fin;

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

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

"pentiumPun k" <Br***@triad.rr .com> wrote in message
news:wp******** *************@t wister.southeas t.rr.com...
string fileName;

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

ifstream fin;

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

Thanks alot in ahead!


fin.open(fileNa me.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>...
"pentiumPun k" <Br***@triad.rr .com> wrote in message
news:wp******** *************@t wister.southeas t.rr.com...
string fileName;

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

ifstream fin;

fin.open(fileNa me, ifstream::ios);
if(!fin)
{
cerr<<"blah blah blah";
exit(1);
}
i get this error message in VC++ .....Error C2664...on the
fin.open(fileNa me, ifstream::ios); line...cannot convert parameter 1 from
std::string to const char *
i cannot put a certain filename down such as fin.open("textf ile.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(filena me.c_str(),ifst ream::ios) instead of fin.open(fileNa me,
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(fileNa me, ifstream::ios);
if(!fin)
{
cerr<<"blah blah blah";
exit(1);
}
i get this error message in VC++ .....Error C2664...on the
fin.open(fileNa me, ifstream::ios); line...cannot convert parameter 1 from
std::string to const char *
i cannot put a certain filename down such as fin.open("textf ile.txt"), i
need the user to enter its name, for it will change.

Thanks alot in ahead!


1. As others have stated:
fin.open(filena me.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::bina ry". 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.l earn.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>...
"pentiumPun k" <Br***@triad.rr .com> wrote in message
news:wp******** *************@t wister.southeas t.rr.com...
> string fileName;
>
> cout<<"Enter filename of datafile you wish to open \n";
> cin>>fileName;
>
> ifstream fin;
>
> fin.open(fileNa me, ifstream::ios);
> if(!fin)
> {
> cerr<<"blah blah blah";
> exit(1);
> }
>
>
> i get this error message in VC++ .....Error C2664...on the
> fin.open(fileNa me, ifstream::ios); line...cannot convert parameter
> 1 from std::string to const char *
> i cannot put a certain filename down such as
> fin.open("textf ile.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
3897
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 is that the raw reports are sequential files with fixed length fields. im having only a few problems but its driving me nuts. here is a small sample of what the file might look like: (im simplifying of course, and adding line numbers) 1...
5
1616
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 basic asp file (test.asp) with the following code: ---------------------------------------- <%
1
967
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
2922
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 even if I was staring right at it.) I'm trying to design a function that reads input from my data file into (a pair of) arrays. Simple enough? However, each line of the file is either going to be 4 integers or a character (then a carriage...
7
3507
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. Is this a bug with IE that it loses the value, or with FF that it keeps it? I'd really like to be able to maintain the selected file value in IE.
6
4138
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 comes back with all the data intact, except for the upload object. The text box for "MyFile" (my example) is always cleared. Why is that and is there a way to stop that from happening? Thanks,
7
441
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 then max. The folder I'm trying to upload files to is ./ispit1. I always get the "wrong" echo from the script when I run debug in my editor and when I run it in my browser (firefox) I get the following message: "The requested URL /ispit/__URL__...
3
6115
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 learned file input/output very well (I self taught all the programming I know...and my c++ book was not good) and so I'm not sure what's wrong with this. The problem is the function void encodemsg(fstream *img, fstream *msg, fstream *out, char key) ...
3
1266
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 in $_FILES array --> <font size='3' face='Tahoma' color="black"> Localização:&nbsp;&nbsp; <input type="file" name="userfile" /><br> </td>
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10142
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3703
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.