The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
File path
Discuss File path in the C Programming forum on Dev Shed. File path C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 5th, 2007, 11:50 PM
|
 |
|
|
Join Date: Oct 2006
Location: /etc/hosts.allow
Posts: 245
Time spent in forums: 2 Days 20 h 35 m 40 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
|
|
|
File path
Hi all. Im trying to get this random number generated file to be saved into a specific file path. But i cant figure out how. Any suggestions?
Here's the code:
Code:
#include <fstream>
#include <ctime>
#include <windows.h>
using namespace std;
char file[9000];
void writefile(void)
{
srand((unsigned)time(0));
int randomnumber;
randomnumber=rand();
sprintf(file,"%d.txt",randomnumber);
ofstream out;
ExpandEnvironmentStrings("%systemroot%\\system32\\", file, 900);
out.open(file, ios::app);
out << "file created " << endl;
out.close();
}
int main(){
writefile();
return 0;
}
Any ideas? Been trying to figure it out myself for hours. Thanx in advance!
|

November 6th, 2007, 12:00 AM
|
|
|
|
Ideas about what? You don't say what, in particular, isn't meeting your expectations.
Incidentally, have you ever considered testing things like file opens to see if they actually succeeded?
|

November 6th, 2007, 10:27 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Have you tried checking the contents of file after your ExpandEnvironmentStrings() call.
Better still, start learning to use your debugger. Instead of spending hours scratching your head, you could figure it out yourself in a couple of minutes if only you read the documentation on how to use your debugger.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

November 6th, 2007, 10:35 AM
|
 |
Commie Mutant Traitor
|
|
Join Date: Jun 2004
Location: Norcross, GA (again)
|
|
Especially given where you're trying to write to... Have you tried changing the program to create the file in a different folder (one you actually have permissions for, perhaps?) and see if it works?
Checking that ExpandEnvironmentStrings() worked would be a wise move as well (it returns zero on an error).
If it isn't too much to ask, why are you trying to create an empty file with a random number for its name in a protected system folder, anyway?
One more piece of advice: magic numbers are a really bad idea. Your code here seems to illustrate why. Can you see why this would be the case? (Hint: what is the size of the buffer you're using, again?) Watch out for off-by-one errors in these things, too; I don't think you have one here, but I'd have to check more carefully to be sure.
The main question remains, though: what is actually happening, and how is it going wrong? Without knowing that, we can only give general answers, which probably won't be the ones you need.
Last edited by Schol-R-LEA : November 6th, 2007 at 10:51 AM.
|

November 6th, 2007, 04:40 PM
|
 |
|
|
Join Date: Oct 2006
Location: /etc/hosts.allow
Posts: 245
Time spent in forums: 2 Days 20 h 35 m 40 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
|
|
Quote: | Originally Posted by Scorpions4ever Have you tried checking the contents of file after your ExpandEnvironmentStrings() call.
Better still, start learning to use your debugger. Instead of spending hours scratching your head, you could figure it out yourself in a couple of minutes if only you read the documentation on how to use your debugger. |
Thanx for your replies. I've figured it out. It wasnt the ExpandEnvironmentStrings() API that i needed to change file names, it was another function. All is well. However im not as experienced as you to utilize a debugger as proficient as you would. To be quite honest, i work better running the program in my head instead of using a debugger. Because honestly i've gotten very very frustrated to the point that i almost broke my keyboard trying to figure out how to use a debugger. Maybe one of these days i'll actually dedicate some time to learn how to use a debugger.
|

November 6th, 2007, 05:24 PM
|
|
|
Quote: | To be quite honest, i work better running the program in my head instead of using a debugger. |
Frankly, the evidence indicates otherwise. If your time is worth zero, then you can afford to spend as many hours as you like on a problem. When you rope in others, who's time is worth more, it's a different story.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|