There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijackthis internet internet explorer itunes laptop mac malware motherboard mouse network networking outlook 2007 power printer problem ram router screen slow sound trojan usb virus vista vista 32-bit windows windows vista windows xp winxp wireless wmp
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
simple C++ question from a beginner


Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
kevin_af's Avatar
Junior Member with 11 posts.
 
Join Date: Jan 2004
Location: Virginia
28-Feb-2004, 11:01 PM #1
simple C++ question from a beginner
disclaimer: C++ newbie here...


I want to be able to have my code delete a file from the current directory. For example:


char x[9]="file.exe";
system("del" x);



The reason I dont just put system("del file.exe") is because I want to be able to come back and change the file name later without having to search my code for every instance of it.

Anyone know how I can do this?

Thanks!
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
29-Feb-2004, 02:09 AM #2
Deleted.

Last edited by coderitr : 29-Feb-2004 02:33 AM.
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
29-Feb-2004, 06:37 AM #3
Code:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string del = "del file.exe";
system(del.c_str());
return 0;
}
or you could split it up like this

Code:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string del = "del ";
string file = "file.exe";
string delcommand = del + file;
system(delcommand.c_str());
return 0;
}

Last edited by Shadow2531 : 29-Feb-2004 06:43 AM.
MustBNuts's Avatar
Distinguished Member with 2,017 posts.
 
Join Date: Aug 2003
Location: Nevada
29-Feb-2004, 03:00 PM #4
I am curious, kevin, why a new programmer would want his first project to be about deleting files...?

When one is learning, there are a lot of things you could be doing that are more productive...this looks a bit fishy....I hope we are not assisting someone in their quest to create malicious code.
__________________
I'll burn that bridge when I get to it!
kevin_af's Avatar
Junior Member with 11 posts.
 
Join Date: Jan 2004
Location: Virginia
29-Feb-2004, 05:13 PM #5
Of course I am not trying to create malicious code. I am writing a program for a homework assignment that reads in some values a user types in and stores them in a file. The values are taken from the file later on for other use in the program. I only want to use the file as a storage place for those values, and do not want the file to be left over in the directory when I am finished with it. So therefore I must delete it! Thanks for your responses so far! They are helpful!
MustBNuts's Avatar
Distinguished Member with 2,017 posts.
 
Join Date: Aug 2003
Location: Nevada
01-Mar-2004, 01:04 AM #6
See, now it comes out. Around here, we frown upon doing people's homework.
Guy's Avatar
Guy Guy is offline
Senior Member with 260 posts.
 
Join Date: Feb 1999
Location: abby, BC, canada
02-Mar-2004, 03:19 PM #7
Doing homework is one thing, but helping him with a specific problem about one piece of his code is another. I think this is acceptable to answer since he just wants to know how to delete a file.

Anyway, Shadow's second example is probably your best bet. If you need to use character arrays (C strings) then try this example (untested):
Code:
#include <string.h>

int main ()
{
  char str[20];
  strcpy (str,"del  ");
  strcat (str,"file.exe");
  return 0;
}
__________________
There are only 10 types of people in the world; Those who understand binary, and those who dont.
MustBNuts's Avatar
Distinguished Member with 2,017 posts.
 
Join Date: Aug 2003
Location: Nevada
02-Mar-2004, 08:38 PM #8
nope, disagree with ya on that one. He did not offer code that did not work, he put in a request for code to be written for him....and just as important, was not forthcoming about the fact that it was homework.

Further, it's a new member with a whopping three posts to his name...

Add to that the fact that we are now living at a time when a lot of young kids with too much time on their hands think its fun to write malicious code and send it out.

Sorry, this sort of post gives me the heebee-jeebees and I don't think the answer should have been given.

MBN
__________________
I'll burn that bridge when I get to it!
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
02-Mar-2004, 09:11 PM #9
I have to second MBN's opinion. (See post #2 above.) After a lengthy discussion with MBN via PM in the wee hours of the morning I deleted my answer to his question. Too many viruses are being coded and released to take chances. I'll be more careful in the future.
MustBNuts's Avatar
Distinguished Member with 2,017 posts.
 
Join Date: Aug 2003
Location: Nevada
02-Mar-2004, 09:35 PM #10
Thanks coderitr, I appreciate the backup!

MBN
kevin_af's Avatar
Junior Member with 11 posts.
 
Join Date: Jan 2004
Location: Virginia
02-Mar-2004, 10:52 PM #11
Well I feel the need to defend myself. Not meaning to be rude or anything, but I didn't realize I had to developed a reputation around here before I was allowed to ask questions. But you are right, I am a new member with only a few posts, but that is because I just started my first C++ class three weeks ago, before which I never had a reason to get onto a C++ forum. If there are certain guidelines for posting questions to homework (like mentioning it is a homework question in the first post) the I apologize because I didn't see them anywhere. I offered a couple lines of code to try and express what I want to do, but am pretty limited in knowledge because I have just begun. My instructor's advice to the class was to go out and join several forums and try to get help with our problems from other coders with experience, so we can get used to speaking to others about programming. I wasn't asking anyone to do my homework for me, just shed some light on a tiny part I was having a problem with. I would even take a reference to certain commands that I can go look up myself. All I need is a place to start. But if you feel uncomfortable still because of uncertainty concerning my intentions, then I respect that. I don't see how someone who can't do something as (apparently) simple as what I've asked could create anything remotely dangerous.

In any manner, I appreciate the responses. At the very least, could you give me some advice on how to be more trustworthy when asking questions on forums? I want to keep using this forum because you guys are pretty responsive (which I have found to be rare). I'd hate to think I am going to be scrutinized and forced to defend myself whenever I try to post a question for a problem in my code.
MustBNuts's Avatar
Distinguished Member with 2,017 posts.
 
Join Date: Aug 2003
Location: Nevada
03-Mar-2004, 12:51 AM #12
kevin, you must realize, some of the posts that end up on the forum are, at best, questionable. We get a bit untrusting sometimes... it isn't personal, but we need to be careful...you'd be amazed at some of the requests that get posted!

Having said that, welcome to the forum and I really do hope you become a regular member/contributor! If you are having trouble with code, just explain where you are having difficulties, explain what code you've tried, and where you're getting stuck....

An example of how to do it:

http://forums.techguy.org/t208426/s.html

what NOT to do:

http://forums.techguy.org/showthread...light=homework

http://forums.techguy.org/showthread...light=homework

http://forums.techguy.org/showthread...light=homework

...and I could go on.....

MBN
__________________
I'll burn that bridge when I get to it!
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
03-Mar-2004, 07:11 AM #13
Yeh, you should definitely ID it as homework in your first post.

Besides that, I liked the way you asked.

You just wanted to know how to perform an operation.

You posted code to relay that you knew what you wanted to do, and actually knew how to do it with a static file name.

From that, I myself got the idea that in general, you wanted to know how to combine 2 strings and then pass it as a paramater for the system function.

We could have kept it vague and just said to search google for adding strings. However, I belived a general example was warranted, which is why one is posted.

Now if you would have posted code for a small app and said, "i can't figure it. Could you fix it?", then that would be ill.

Anyways, at the Hardforum, they have a sticky at the top of the programming forum.

http://www.hardforum.com/showthread.php?threadid=670457

I myself had a problem with a program a while back. I knew that I needed to use arrays to make my program work, but to get it right, I needed to be able to visualize the arrays, so I asked them to help me visualize the arrays etc. Once they did that, I got my program working and posted it. They then offered suggestions to make the program better.

Point being, I didn't ask for help on the program specifically, I asked on how to do something in general.

Usually a lame example is warranted to give you an idea and then once they see that you are getting the hang of it, they'll show you more.

Sometimes, they'll show you more than warranted only because they know that some instructors do things oddly and they just want you to get it right.
__________________
10 ? "a line as the unending horizon"
20 ? "a curve as the rolling hillside"
30 ? "a point as a distant bird"
40 ? "a ray as the rising sun"
run
kevin_af's Avatar
Junior Member with 11 posts.
 
Join Date: Jan 2004
Location: Virginia
03-Mar-2004, 10:16 PM #14
Thanks guys, I appreciate the advice! I'm sure I'll get jammed up again in the not so distant future, and so you'll hear from me again. But thank you for showing me how things are done around here!
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 09:05 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.