GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 15-Dec-2007, 11:42
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 582
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Using a string as filename to open a file


Hi, new to C++ no flamingos to me plz!!!
I decided to see if I could get into trouble over here now. Going through the cplusplus.com tutorial mentioned a few threads back, I was looking into file manipulation and came accross this mystery.
What is the difference between the string and a char[] that causes this compiling error?
CPP / C++ / C Code:
#include <iostream>    // basic file operations
#include <fstream>
#include <string>
using namespace std;

int main ()
{
  char str[] = "myfile.txt";
  string mystring;
  ofstream myfile;

  mystring = "myfile.txt";

  myfile.open(mystring);  // gets an error for some reason (error below)
  //myfile.open(str);         // but this doesn't... and works as expected.

/*********   This is from mingw:  ******************/
  g++ -Wall -W -pedantic filex1.cpp -o filex1.exe
filex1.cpp: In function `int main()':
filex1.cpp:14: error: no matching function for call to `
   std::basic_ofstream<char, std::char_traits<char> >::open(std::string&)'
C:/cs1300/include/c++/3.3.1/fstream:691: error: candidates are: void
   std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode)
   [with _CharT = char, _Traits = std::char_traits<char>]
Now I KNOW I SHOULD be able to tell from the messsage what the Blatently Obvious problem is but it escapes me at this time, maybe someday... not in Kansas anymore...
Any Ideas Guys? I'm waiting patiently for you to help ME plz...
Howard();
  #2  
Old 15-Dec-2007, 11:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: prob using a string as filename to open a file


Quote:
Originally Posted by Howard_L
Any Ideas

Specifically:

CPP / C++ / C Code:
  myfile.open(mystring.c_str()); 

More generally: http://www.yolinux.com/TUTORIALS/Lin...ringClass.html

I know it says "Linux Tutorial," but it's all about std::string stuff, so anyone is allowed read it, even non-Linux guys (and even non-guys)!

Regards,

Dave
  #3  
Old 15-Dec-2007, 19:48
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 582
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: prob using a string as filename to open a file


Gone awhile.... sorry... Ok, I understand a little more , still a long way to go:
CPP / C++ / C Code:
#include <iostream>    // basic file operations
#include <fstream>
#include <string>
using namespace std;

int main ()
{
  char str[] = "myfile.txt";
  string mystring;
  ofstream myfile;

  mystring = "myfile.txt";

  myfile.open(mystring.data() ); // ah! I see 
  myfile.open(mystring);         // gets an error for some reason (error below)
  //myfile.open(str);            // but this doesn't... why?

  cout << str      <<endl;
  cout << mystring <<endl;

  myfile << "Writing this to a file 1 using mystring.data() to return a pointer \n\
to a type char representation of the string as explained \n\
    here: [url]http://www.cplusplus.com/reference/string/string/[/url] \n\
and here: http://www.cplusplus.com/reference/string/string/data.html";
  myfile.close();

  return 0;
}
If everything is a file in Linux , everything is sure like a structure in C++ !
Are all these kinds of tools available to all datatypes? Guess I'll read on and see...
Thanks Dave;
Howard();
  #4  
Old 15-Dec-2007, 23:12
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: prob using a string as filename to open a file


Quote:
Originally Posted by Howard_L
CPP / C++ / C Code:
    myfile.open(mystring.data() ); // ah! I see 

No, no, no! That's undefined behavior.

The data() member function does not (that is, it is not guaranteed to) append a terminating zero byte as is required for the argument for open(). That is: It may or may not appear to work but using this way is a big no-no. (That's five no's so far.)

You even gave the reference: http://www.cplusplus.com/reference/s...ring/data.html Read it again:

"Notice that no terminating null character is appended (see member c_str for such a functionality)."

For the argument to open() (and anywhere else you need a C-style string from a std::string), use c_str(), as I showed in my previous post, to make sure it returns a pointer to a zero-byte terminated sequence of chars.


Regards,

Dave
  #5  
Old 15-Dec-2007, 23:35
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 582
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: prob using a string as filename to open a file


Quote:
Read it again:
Right... from the string/data.html
Quote:
Notice that no terminating null character is appended (see member c_str for such a functionality).
Duh,, read right over that before...
Don't know how I got sidetracked but in my search path on this page down at the bottom:
http://www.cplusplus.com/reference/string/string/
Quote:
String operations:
c_str Get C string equivalent (public member function)
data Get string data (public member function)
...
...There they are right next to one another. Why I didn't even try c_str() is beyond me. Been a long day here too. I should just shut up... Thanks again, howard--;
  #6  
Old 16-Dec-2007, 08:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,821
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: prob using a string as filename to open a file


Quote:
Originally Posted by Howard_L
Right... from the string/data.html

RTFP (Read the Fine Print):

The library function definition:

data() ==> pointer to sequence of chars in the string
c_str() ==> pointer to zero-byte terminated sequence of chars in the string

The not-so-subtle subtleties are easily lost since many people never actually think about what a "string" is in C (why is "string" in quotes? because there is no "string" data type in C), and important details can slip our notice.

Details like:

1. Using the terminating zero byte to cause end of "string" processing and copying the zero byte automatically with strcat(), strcpy(), etc.

2. Using the terminating zero byte to cause end of "string" processing but not actually considering it part of the "string" with strlen(), printf("%s",...), etc.


There is no "string" data type in C++ either. There is a string class that is part of the C++ standard library that is a poster boy for the power of that feature of the language that allows definition of new "stuff" that acts like new data types, complete with handy, dandy functions and overloaded operators.

Regards,

Dave
  #7  
Old 17-Dec-2007, 09:45
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 582
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: Using a string as filename to open a file


Quote:
There is no "string" data type in C++... There is a string class... a poster boy for the power of that feature of the language that allows definition of new "stuff" that acts like new data types
Exactly... I'm seeing that now. The tutorial has string in datatype section:
http://www.cplusplus.com/doc/tutorial/variables.html
and while mentioning that string is not a fundemental data type and that it is a class object, with an example like this:
CPP / C++ / C Code:
  string mystring;
  mystring = "This is the initial string content";
  cout << mystring << endl;
I really didn't get the full of impact of just what this ""string" class object" is all about.
But at the bottom it does say: for more info: http://www.cplusplus.com/reference/string/string/
much to look at there...so the adventure continues... Thanks
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 09:14
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
c++/cgi program can't open file in user dir mikenowo Apache Web Server Forum 2 04-Mar-2004 20:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:25.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.