Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 16th, 2005, 4:59 PM   #1
jademan
Newbie
 
Join Date: Dec 2005
Posts: 6
Rep Power: 0 jademan is on a distinguished road
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.
jademan is offline   Reply With Quote
Old Dec 16th, 2005, 5:05 PM   #2
B3TA_SCR1PT3R
Hobbyist Programmer
 
B3TA_SCR1PT3R's Avatar
 
Join Date: Jul 2005
Location: Dallas, Texas
Posts: 101
Rep Power: 0 B3TA_SCR1PT3R is an unknown quantity at this point
Send a message via AIM to B3TA_SCR1PT3R
firstly:
 ofstream file("C:\\"<<var<<".txt);
and if its not letting you put variables in(not sure havent worked with C++ in a while)
do:
string thefile = "C:\\";
string p = ".txt";
string s = thefile += var += p;
ofstream file(s);
(not sure if itll work)
__________________
Hoes telling me to calm down but I'm like fuck that shit!
B3TA_SCR1PT3R is offline   Reply With Quote
Old Dec 16th, 2005, 5:14 PM   #3
jademan
Newbie
 
Join Date: Dec 2005
Posts: 6
Rep Power: 0 jademan is on a distinguished road
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
jademan is offline   Reply With Quote
Old Dec 16th, 2005, 5:16 PM   #4
B3TA_SCR1PT3R
Hobbyist Programmer
 
B3TA_SCR1PT3R's Avatar
 
Join Date: Jul 2005
Location: Dallas, Texas
Posts: 101
Rep Power: 0 B3TA_SCR1PT3R is an unknown quantity at this point
Send a message via AIM to B3TA_SCR1PT3R
ok if your on Windows XP you use: C:\ not C:/
__________________
Hoes telling me to calm down but I'm like fuck that shit!
B3TA_SCR1PT3R is offline   Reply With Quote
Old Dec 16th, 2005, 5:20 PM   #5
jademan
Newbie
 
Join Date: Dec 2005
Posts: 6
Rep Power: 0 jademan is on a distinguished road
yes i know that but that isnt the issue
jademan is offline   Reply With Quote
Old Dec 16th, 2005, 5:55 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
"C://"<<var<<".txt"
This does not concatenate strings in C++. Beta, please do not mislead new coders. Check your answers first. You may cause far more harm than good.

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
DaWei is offline   Reply With Quote
Old Dec 16th, 2005, 6:35 PM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Dec 16th, 2005, 7:10 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Dec 16th, 2005, 7:53 PM   #9
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Dec 16th, 2005, 9:26 PM   #10
jademan
Newbie
 
Join Date: Dec 2005
Posts: 6
Rep Power: 0 jademan is on a distinguished road
yea im not using .net so I dont think "string" works ( visual C++ btw ) but i will research that other suggestion. Thank you
jademan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC