hopefully easy question

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 5
Reputation: thecraig is an unknown quantity at this point 
Solved Threads: 0
thecraig thecraig is offline Offline
Newbie Poster

hopefully easy question

 
0
  #1
Feb 24th, 2008
I am trying to write a file that writes to a user specified folder using ofstream. The basics of the code looks like this:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5.  
  6. using namespace std;
  7.  
  8. int write(int id)
  9. {
  10. ofstream myfile;
  11. myfile.open ("account/"id"/example.txt"); //this is where the error pops up
  12. myfile << "this is what I want written";
  13. myfile.close();
  14. }
  15.  
  16. int Account()
  17. {
  18. int id;
  19. cout << "Please enter account" << endl;
  20. cin >> id;
  21. return id;
  22. }
  23.  
  24. int main()
  25. {
  26. int id;
  27. id = Account();
  28. write(id);
  29. return 0;
  30. }

Anyways, hopefully this is an easy question as I havn't programmed since highschool. Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: hopefully easy question

 
0
  #2
Feb 24th, 2008
>>hopefully this is an easy question
Ok, I'm not a mind reader so please state the question.

delete stdlib.h and conio.h because they are not needed in c++.
Last edited by Ancient Dragon; Feb 24th, 2008 at 10:04 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: thecraig is an unknown quantity at this point 
Solved Threads: 0
thecraig thecraig is offline Offline
Newbie Poster

Re: hopefully easy question

 
0
  #3
Feb 24th, 2008
question was more of a statement but still there

at the point where I'm trying to access the file

  1. myfile.open ("account/"id"/example.txt"); //this is where the error pops up

I want to have the program write to a file based on the account name (id). The actual questions are what is the proper way to do that and is there a better way?

stdlib is there out of habit, I use conio.h for system("CLS")
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,678
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: hopefully easy question

 
0
  #4
Feb 24th, 2008
  1. int write(int id)
  2. {
  3. ofstream myfile;
  4. myfile.open ("account/"id"/example.txt"); //this is where the error pops up
  5. myfile << "this is what I want written";
  6. myfile.close();
  7. }
Yep, there's definitely an error there. The double quote at the beginning of id closes off the string for the open command, then you have stuff the compiler does not know what to do with.

You need to convert the integer parameter to its string representation, then build the string you want to use for the open command's argument.

You might simplify it all if you'd originally taken the account ID as a string instead of an int, and it would make your input more bulletproof.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: hopefully easy question

 
0
  #5
Feb 24th, 2008
The error is because you have embedded the quote sysmble " within the path -- you can't do that. You will have to format the filename before calling the open function, something like this:
  1. #include <sstream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. ofstream myfile;
  9. int id = 12345; // some id
  10. stringstream str;
  11. str << "account/" << id << "/example.txt";
  12. myfile.open(str.str());
  13.  
  14. return 0;
  15. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: thecraig is an unknown quantity at this point 
Solved Threads: 0
thecraig thecraig is offline Offline
Newbie Poster

Re: hopefully easy question

 
0
  #6
Feb 24th, 2008
awesome that makes sense but now I've run into a new problem.

  1. myfile.open (id);

the compiler is saying no matching function or call to std::basic and then goes on to define the write part. I'm assuming that I need to tell it id is the path I want opened. Is this true or how else can I fix this.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: hopefully easy question

 
0
  #7
Feb 24th, 2008
how is id defined?
Last edited by Ancient Dragon; Feb 24th, 2008 at 10:48 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: thecraig is an unknown quantity at this point 
Solved Threads: 0
thecraig thecraig is offline Offline
Newbie Poster

Re: hopefully easy question

 
0
  #8
Feb 24th, 2008
This is the whole function as it stands at the moment, I basically changed everything to the strings and copied and pasted your string into the main.

  1. int write(string id)
  2. {
  3. ofstream myfile;
  4. myfile.open (id);
  5. myfile << "this is what I want written";
  6. myfile.close();
  7. }
Last edited by thecraig; Feb 24th, 2008 at 10:53 pm. Reason: clicked post before finishing
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: hopefully easy question

 
0
  #9
Feb 24th, 2008
open() wants char*, not a string, so you have to use the string's c_str() method
myfile.open(id.c_str());
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: thecraig is an unknown quantity at this point 
Solved Threads: 0
thecraig thecraig is offline Offline
Newbie Poster

Re: hopefully easy question

 
0
  #10
Feb 24th, 2008
awesome it works. thank you very much
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC