| | |
hopefully easy question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
I am trying to write a file that writes to a user specified folder using ofstream. The basics of the code looks like this:
Anyways, hopefully this is an easy question as I havn't programmed since highschool. Thanks in advance.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <conio.h> #include <stdlib.h> using namespace std; int write(int id) { ofstream myfile; myfile.open ("account/"id"/example.txt"); //this is where the error pops up myfile << "this is what I want written"; myfile.close(); } int Account() { int id; cout << "Please enter account" << endl; cin >> id; return id; } int main() { int id; id = Account(); write(id); return 0; }
Anyways, hopefully this is an easy question as I havn't programmed since highschool. Thanks in advance.
>>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++.
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.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
question was more of a statement but still there
at the point where I'm trying to access the file
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")
at the point where I'm trying to access the file
C++ Syntax (Toggle Plain Text)
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")
C++ Syntax (Toggle Plain Text)
int write(int id) { ofstream myfile; myfile.open ("account/"id"/example.txt"); //this is where the error pops up myfile << "this is what I want written"; myfile.close(); }
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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:
C++ Syntax (Toggle Plain Text)
#include <sstream> #include <string> #include <fstream> using namespace std; int main() { ofstream myfile; int id = 12345; // some id stringstream str; str << "account/" << id << "/example.txt"; myfile.open(str.str()); return 0; }
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.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
awesome that makes sense but now I've run into a new problem.
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.
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Feb 2008
Posts: 5
Reputation:
Solved Threads: 0
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.
C++ Syntax (Toggle Plain Text)
int write(string id) { ofstream myfile; myfile.open (id); myfile << "this is what I want written"; myfile.close(); }
Last edited by thecraig; Feb 24th, 2008 at 10:53 pm. Reason: clicked post before finishing
![]() |
Similar Threads
- Im new and have an easy (I think) question (C++)
- Basic ASP Access question (ASP)
- Hopefully easy question about linking... (C++)
- question about web hosting (Networking Hardware Configuration)
- EASY question? checking for line feed (return) in a html file with C++ (C++)
- IBM thinkpad t22 mobile processor question (Networking Hardware Configuration)
- A question of RAM (Windows NT / 2000 / XP)
- Completely new to C++ and have question about using char (C++)
- wireless, (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: Help with a code in C++ of stock management program that can?
- Next Thread: Creating an exe file
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






