![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Dec 2005
Posts: 6
Rep Power: 0
![]() |
ofstream insert variable
So iv got a code like this...
ofstream file("C:/"+var+".txt); I know this isnt right. I used PHP syntax just to deminstrate what I want to do. Basically I want to insert a variable into the file string. Im sure its something simple but I just couldnt figure it out. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
firstly:
ofstream file("C:\\"<<var<<".txt);do: string thefile = "C:\\"; string p = ".txt"; string s = thefile += var += p; ofstream file(s);
__________________
Hoes telling me to calm down but I'm like fuck that shit!
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Dec 2005
Posts: 6
Rep Power: 0
![]() |
not working
ofstream file("C://"<<var<<".txt");
That gave me this error - error C2296: '<<' : illegal, left operand has type 'char [64]' I tried this ... char prepath[100] = "C:/Documents and Settings/All Users/Desktop/"; char extension[6] = ".html"; char path[150] = prepath + var + extension; ofstream rep(path); "var" is an integer btw I got this error error C2110: cannot add two pointers |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
ok if your on Windows XP you use: C:\ not C:/
__________________
Hoes telling me to calm down but I'm like fuck that shit!
|
|
|
|
|
|
#5 |
|
Newbie
Join Date: Dec 2005
Posts: 6
Rep Power: 0
![]() |
yes i know that but that isnt the issue
|
|
|
|
|
|
#6 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
If you wish to concatenate C strings (character arrays terminated by nul characters), use the strcpy/strcat family of functions. If you wish to use C++ strings, you may use the '+' operator. Arguments requiring C strings will not accept C++ strings directly, however. You may specify the C string portion with myString.c_str (). That is not modifiable, only readable. If you read the documentation for the things covered and still have questions, post back.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Why are you using C strings and not C++ strings?
This should work for you: #include <iostream>
#include <string>
using namespace std;
int main()
{
string prepath = "C:/Documents and Settings/All Users/Desktop/";
string extension = ".html";
string var = "bla";
string path = prepath + var + extension;
cout << path << endl;
//ofstream rep(path);
return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
He can't use the string directly in the open statement. He has to use a C string, if only by using filename.c_str ().
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
David as usual, you are correct. I'm not wanting to state the obvious
.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Dec 2005
Posts: 6
Rep Power: 0
![]() |
yea im not using .net so I dont think "string" works ( visual C++ btw ) but i will research that other suggestion. Thank you
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|