fstream charFile.open(string)

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1082 Views - Last Post: 20 August 2012 - 03:27 PM Rate Topic: -----

#1 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

fstream charFile.open(string)

Posted 18 August 2012 - 12:39 AM

So I have been working on an rpg to prepare for an upcoming college class (I'm finally starting college!! :bananaman: ) but I'm basically creating a save file based on the name you give your character. So if I create a person named ticon, I can find him under ticon.txt However when writing the code I get a bit of a problem.. I get
C:\Users\Ticon\Documents\C++\myRPG\gameSearch.h|20|error: no matching function for call to 'std::basic_ofstream<char, std::char_traits<char> >::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'|


with another error of
c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\fstream|696|note: candidates are: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]|


My code looks like this
//This is to see if the player has a saved game or not. if they do not, it sends them to "initiation"
void startMessage()
{
    cout << "Do you have an existing game? (Y/N)";
    string response;
    cin >> response;
    if (response == "Y")
    {
        cout << "Excellent! what is your characters name?";
        string name;
        cin >> name;
        //open characters file
        ofstream charFile;
        charFile.open(name + ".txt");

        charFile.close();

    }
}


Any advice? And thanks for reading!!

Is This A Good Question/Topic? 0
  • +

Replies To: fstream charFile.open(string)

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: fstream charFile.open(string)

Posted 18 August 2012 - 12:54 AM

I think that only C++11 supports open() taking a std::string. Previous versions only take C-style strings. That means you'll need to append the file extension and then use the std::string::c_str() accessor to get a C-style string.
Was This Post Helpful? 0
  • +
  • -

#3 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Re: fstream charFile.open(string)

Posted 18 August 2012 - 01:02 AM

Well, seeing as I'd rather upgrade C++ than use the old clunky brainf*ck C you gave, is it possible to use C++ 11 with codeblocks? A google gave me next to no useful results
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: fstream charFile.open(string)

Posted 18 August 2012 - 01:44 AM

You'll probably need somebody more savvy with Code::Blocks and gcc to help you. I do know that I can tell Code::Blocks to use C++0x standards and it will compile, but not link so it won't really do you a too much good.

VS2010, which I wasn't expecting to support this considering how far behind it is with regards to several aspects of the C++ standards, somehow supports taking the string parameter. If you can't wait, download and install VS2010 Express Edition. (VS2012 Express should be out or available soon as well.)
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: fstream charFile.open(string)

Posted 18 August 2012 - 05:00 AM

What version of gcc/g++ are you using?

You may just need to add either std=c++0x or std=c++11 to your project settings.

Jim
Was This Post Helpful? 1
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: fstream charFile.open(string)

Posted 18 August 2012 - 08:45 AM

For Windows, Code::Block 10.05 w/ mingw32 has gcc 4.4.1.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: fstream charFile.open(string)

Posted 18 August 2012 - 09:15 AM

There are at least two different versions of gcc available for Windows. The latest version is 4.7.0, which is close to if not the same version as available for Linux. See this link: MinGW. If you don't have this version I suggest you look at updating.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3575
  • View blog
  • Posts: 11,117
  • Joined: 05-May 12

Re: fstream charFile.open(string)

Posted 18 August 2012 - 09:31 AM

View Postjimblumberg, on 18 August 2012 - 09:15 AM, said:

There are at least two different versions of gcc available for Windows. The latest version is 4.7.0, which is close to if not the same version as available for Linux. See this link: MinGW. If you don't have this version I suggest you look at updating.

Jim


Yup, and like the OP, I've also failed to find instructions on how to upgrade the compiler that comes with Code::Blocks. The MinGW instructions say it wants to be installed out to "c:\MinGW32", but the current packaged installed with Code::Blocks lives down in "C:\Program Files". Doesn't sound like an upgrade to me if I install to a different location.

Additionally, the Getting Started page says:

Quote

MinGW may have problems with paths containing spaces, and if not, usually other programs used with MinGW will experience problems with such paths. Thus, we strongly recommend that you do not install MinGW in any location with spaces in the path name reference; i.e. you should avoid installing into any subdirectory of "Program Files" or "My Documents", or the like.


And hence my hesitance to upgrade my compiler and my deferring to other experts who can tell the OP and me how to get a newer version of GCC to work with our current installation of Code::Blocks.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: fstream charFile.open(string)

Posted 18 August 2012 - 10:05 AM

I myself would recommend that you install this "upgrade" to a different directory, then if you want you can switch between the two versions by just selecting the proper version from the compiler selection list.

Do this by selecting Settings, Compiler and debugger from the menu. Then if you already have the original version of gcc installed copy that information using the copy button. Next select your new compiler and then go to the Toolchain executables tab and change the Compiler's installation directory in that tab. Verify that the rest of the information has valid values. This link seems to have some good pictures showing this information, excepting the copy step: Code Blocks Compiler Setup.

Edit: If you plan to use the new compiler from the command line you will need to insure your search path is correct for this compiler.

Jim

This post has been edited by jimblumberg: 18 August 2012 - 10:12 AM

Was This Post Helpful? 1
  • +
  • -

#10 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Re: fstream charFile.open(string)

Posted 18 August 2012 - 11:29 AM

After doing some additional looking into it, I found a solution! in
settings -> compiler and debugger -> compiler tab -> other options
-std=c++0x -U__STRICT_ANSI__ or -std=gnu++0x -U__STRICT_ANSI__

after I add that it works! thanks for pointing me around guys :) I wouldn't be anywhere without you

This post has been edited by Ticon: 18 August 2012 - 12:45 PM

Was This Post Helpful? 1
  • +
  • -

#11 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Re: fstream charFile.open(string)

Posted 20 August 2012 - 01:20 PM

So I opened a thread the other day about fixing an error I had with using a string for a paremeter and was told of 3 things I could do. 1, upgrade to c++ 11. I tried that and it worked, but not really since It wouldn't compile anymore. 2, switch to visual studio..but this ide has me wanting to rip my hair out. and 3

View PostSkydiver, on 18 August 2012 - 01:54 AM, said:

I think that only C++11 supports open() taking a std::string. Previous versions only take C-style strings. That means you'll need to append the file extension and then use the std::string::c_str() accessor to get a C-style string.

I would like to use the third method, since it doesn't seem that big of a deal, although it is a big deal to me because I have no idea how to use it :/ any help?

        charFile.open(std::string::c_str(name));
was the most logical I could come up with
...

--edit-- just realized it would be wise to show I have tried around with the code.
--edit-- I could have sworn I made this a different topic O.o

Posted Image

This post has been edited by Ticon: 20 August 2012 - 01:40 PM

Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: fstream charFile.open(string)

Posted 20 August 2012 - 01:38 PM

Did you try Googling "std::string.c_str()"? Did you read the documentation for this standard function?

Quote

1, upgrade to c++ 11. I tried that and it worked, but not really since It wouldn't compile anymore.

Why didn't it compile, show your code where you are having problems, also post the complete error messages exactly as they appear in your development environment. A C++11 compiler should be able to compile all legal standard C++ programs, so just changing the compiler switches to compile to C++11 should not affect your previous code, is should just allow you to correctly compile things in the current standard.

Jim
Was This Post Helpful? 0
  • +
  • -

#13 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Re: fstream charFile.open(string)

Posted 20 August 2012 - 01:47 PM

Thanks for the response! I did google std::string.c_str() but I unfortunately did not find a usage for it in my situation.

so addressing question 1, I use -std=c++0x -U__STRICT_ANSI__ or -std=gnu++0x -U__STRICT_ANSI__ in other options under the compiler. It generates this error.
Compiling: main.cpp
mingw32-g++.exe: or: No such file or directory
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings
 
I remove the tag and its fine, but i know mingw32-g++ exists :/

the error I get when using the C method is
C:\Users\Ticon\Documents\C++\myRPG\gamesearch.h||In function 'void startMessage()':|
C:\Users\Ticon\Documents\C++\myRPG\gamesearch.h|23|error: no matching function for call to 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str(std::string&)'|
c:\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\basic_string.h|1615|note: candidates are: const _CharT* std::basic_string<_CharT, _Traits, _Alloc>::c_str() const [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]|
||=== Build finished: 1 errors, 0 warnings ===|

Sorry for wall of text

the code in my header
Spoiler

This post has been edited by Ticon: 20 August 2012 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,562
  • Joined: 25-December 09

Re: fstream charFile.open(string)

Posted 20 August 2012 - 02:03 PM

For the problems compiling post the contents of your .cbp file. To compile as C++11 you should not need any thing in the other options section. To set the compiler to use C++11 right click on your project in the Management Window, select project options. You should then see quite a few check boxes including a box that should read "Have g++ follow the comming C++0x Language Standard". Also in this section are the different warning level options. I recommend checking most of these boxes.

Now for the third option you need an instance of a std::string to convert to a C-string in order to use the c_str() member function. So if your string is named fileName the you would use it like:
fileStream.open(fileName.c_str());
. Also note you will only rarely find an explanation that totally matches your desired usage. You will need to read and understand the purpose of the function in order to be able to redo the sample for your exact usage.


Also please don't open a new topic for the same problem.

Jim

This post has been edited by jimblumberg: 20 August 2012 - 02:03 PM

Was This Post Helpful? 0
  • +
  • -

#15 Ticon  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 320
  • Joined: 20-August 09

Re: fstream charFile.open(string)

Posted 20 August 2012 - 03:17 PM

Sorry I didn't want to risk a "necro" did that once and people got PISSSSED. although you have completely solved my problem with the C, I don't see the checkbox you mentioned to change it to 11, however I didn't realize that c_str was a function of the string class.. Thank you for your help I absolutely appreciate it with utmost gratitude.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2