C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2007, 11:50 PM
dellthinker's Avatar
dellthinker dellthinker is offline
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Location: /etc/hosts.allow
Posts: 245 dellthinker Negative: is most likely a SPAMMER and a traitor to the cause. 
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!

Reply With Quote
  #2  
Old November 6th, 2007, 12:00 AM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jun 2005
Posts: 5,964 sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 47 m 19 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 4851
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?
__________________
Write no code whose complexity leaves you wondering what the hell you did.
Politically Incorrect DaWei on Pointers Grumpy on Exceptions

Reply With Quote
  #3  
Old November 6th, 2007, 10:27 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 17 m 19 sec
Reputation Power: 4080
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

Reply With Quote
  #4  
Old November 6th, 2007, 10:35 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,785 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 18 h 21 m 8 sec
Reputation Power: 1569
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.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Last edited by Schol-R-LEA : November 6th, 2007 at 10:51 AM.

Reply With Quote
  #5  
Old November 6th, 2007, 04:40 PM
dellthinker's Avatar
dellthinker dellthinker is offline
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Location: /etc/hosts.allow
Posts: 245 dellthinker Negative: is most likely a SPAMMER and a traitor to the cause. 
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.

Reply With Quote
  #6  
Old November 6th, 2007, 05:24 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jun 2005
Posts: 5,964 sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 47 m 19 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 4851
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > File path

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap