User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,070 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,270 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 357 | Replies: 21
Reply
Join Date: Jun 2008
Posts: 71
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

Using a variable for filename

  #1  
32 Days Ago
Hello, I am having a small problem because I have to have to have it so the user can enter a number and that number is stored in a variable and that variable becomes the name of the file to be created with the extension .asw. It is going to be a custom encrypted file. Just need to know what is wrong because I am getting an error right now.

 cout << "Enter the Item ID: \n";
 string ItemID;
 cin >> ItemID;
 ofstream ItemFile;
 ItemFile.open(ItemID.c_str()".asw");
 ItemFile.close();

The error is
error: expected `)' before string constant|
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Using a variable for filename

  #2  
32 Days Ago
You formed the string incorrectly
ItemFile.open((string)(ItemID+".asw").c_str());
Last edited by Ancient Dragon : 32 Days Ago at 10:41 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Sep 2004
Posts: 6,022
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is online now Online
Expert Meanie

Re: Using a variable for filename

  #3  
32 Days Ago
Originally Posted by Ancient Dragon View Post
You formed the string incorrectly
ItemFile.open((string)(ItemID+".asw").c_str());

The result of operator+ for std::string is always going to be a new std::string of the same type, so your cast is redundant. This is cleaner:
  1. ItemFile.open((ItemID + ".asw").c_str());
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Jun 2008
Posts: 71
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

Re: Using a variable for filename

  #4  
32 Days Ago
ok thanks guys
Reply With Quote  
Join Date: Jun 2008
Posts: 71
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

Re: Using a variable for filename

  #5  
31 Days Ago
ok still getting errors on both suggestions

Ancient Dragon Error
error: no matching function for call to `std::basic_ofstream<char, std::char_traits<char> >::open(std::string)'|

Narue Error

error: invalid operands of types `void ()()' and `const char[5]' to binary `operator+'|
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Using a variable for filename

  #6  
31 Days Ago
You must have mistyped something -- Narue's version works ok, mine doesn't. So post code so we can see what you did
Last edited by Ancient Dragon : 31 Days Ago at 12:18 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jun 2008
Posts: 71
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

Re: Using a variable for filename

  #7  
31 Days Ago
I think I mistyped one of my variables I changed because it seems to be working now, thanks. Ill post if I have any problems
Reply With Quote  
Join Date: Jun 2008
Posts: 71
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

Re: Using a variable for filename

  #8  
31 Days Ago
now I have only 1 error left and its from this part of the code

ofstream file;
file.open((ItemIDd + ".asw").c_str());

it says that 'file' was not declared in the scope. Any help appreciated.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,562
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 860
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Using a variable for filename

  #9  
31 Days Ago
>>ItemIDd
That's misspelled.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Jun 2008
Posts: 71
Reputation: cam875 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
cam875 cam875 is offline Offline
Junior Poster in Training

Re: Using a variable for filename

  #10  
31 Days Ago
no its not the problem is the word 'file' i just added a d to ItemID because I needed to change the word quick.

error: `file' was not declared in this scope|
Last edited by cam875 : 31 Days Ago at 1:35 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC