Quantcast

Maximum PC

It is currently Tue Sep 16, 2014 4:58 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 16 posts ] 
Author Message
 Post subject: C++ command line arguments, determine type?
PostPosted: Wed Jul 07, 2010 6:11 pm 
Little Foot
Little Foot

Joined: Mon Jan 16, 2006 11:54 am
Posts: 143
Hello,

I'm writing a program that does DES encryption on a file passed in via command line. If the file is a .des (encrypted) file, I need to unencrypt it. If it is a .txt file, I need to encrypt it.

I've looked around but found nothing so I figured the solution was either sort of complicated or really easy and I was just missing it.

Any help is appreciated, so thanks in advance.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2010 3:52 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
:roll:

You are writing a program that performs encryption and you can't figure this out?

First, figure out command line arguments. http://tinyurl.com/2ccvp46

Then find the needle in the haystack... http://www.cplusplus.com/reference/string/string/find/

I hate to sound condescending, but writing a program that performs encryption might be beyond your abilities at this point.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2010 9:19 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
Summer school?


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2010 12:37 pm 
Little Foot
Little Foot

Joined: Mon Jan 16, 2006 11:54 am
Posts: 143
Uh,

Thanks for the help.

I know how command line arguments work.

The trouble is that the program doesn't know what the text file should be holding, so I need another way to determine the file type.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2010 12:47 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
At some point, the program needs to open the file to be able to read it. At this point, the program must know the name of the file. At that point, you can determine what the file contains.

Alternatively, you could easily write some code that reads the first 10 lines of the file and determines whether it is encrypted. That is a messy hack but certainly not difficult.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2010 4:44 pm 
Little Foot
Little Foot

Joined: Mon Jan 16, 2006 11:54 am
Posts: 143
Jipstyle wrote:
At some point, the program needs to open the file to be able to read it. At this point, the program must know the name of the file. At that point, you can determine what the file contains.

Alternatively, you could easily write some code that reads the first 10 lines of the file and determines whether it is encrypted. That is a messy hack but certainly not difficult.


Yes! that's all I need, the name of the file...


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2010 5:22 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
slave_25637 wrote:
Jipstyle wrote:
At some point, the program needs to open the file to be able to read it. At this point, the program must know the name of the file. At that point, you can determine what the file contains.

Alternatively, you could easily write some code that reads the first 10 lines of the file and determines whether it is encrypted. That is a messy hack but certainly not difficult.


Yes! that's all I need, the name of the file...
You get it from the command line argument! If you know how they work, then you don't have a problem.

Code:
c:\myappfolder\myapp.exe -f file.txt


The second command line argument, is the file name, you use the function I linked you to to look for either txt or whatever extension you need. This isn't hard I gave you everything you need already.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2010 6:06 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
Hilarious thought: what if he isn't opening the file when launching the app but rather from within the app? :lol:


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2010 6:39 pm 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Jipstyle wrote:
Hilarious thought: what if he isn't opening the file when launching the app but rather from within the app? :lol:
Wait... that doesn't make sense...

He still has to know the file name... so either he is getting user input, hard coding it, or using the cli arguments... one way or another he has to know the file name. lol


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2010 9:25 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24224
Location: Granite Heaven
CrashTECH wrote:
Jipstyle wrote:
Hilarious thought: what if he isn't opening the file when launching the app but rather from within the app? :lol:
Wait... that doesn't make sense...


Exactly. :P

Quote:
He still has to know the file name... so either he is getting user input, hard coding it, or using the cli arguments... one way or another he has to know the file name. lol


Yeah .. that's why I find this kinda funny.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Jul 08, 2010 9:42 pm 
Little Foot
Little Foot

Joined: Mon Jan 16, 2006 11:54 am
Posts: 143
set phazers on condescending because I still can't get this.

int main(int argc, char **argv)
{
//say i want to cout the name of the file, so i put
cout << argv[0];
//right? but nothing shows.

}


Top
  Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2010 3:59 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
I admit, it was a little condescending, but I already gave you the answer....

Google: "c++ command line arguments example"

Result #1: http://www.learncpp.com/cpp-tutorial/71 ... arguments/

If you are just running your program, there won't be any arguments, so argv[0] will be empty.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2010 11:59 am 
Little Foot
Little Foot

Joined: Mon Jan 16, 2006 11:54 am
Posts: 143
CrashTECH wrote:
I admit, it was a little condescending, but I already gave you the answer....

Google: "c++ command line arguments example"

Result #1: http://www.learncpp.com/cpp-tutorial/71 ... arguments/

If you are just running your program, there won't be any arguments, so argv[0] will be empty.


its cool. I'm not a very good programmer and I know it.

I'll play around with the page at that link.

Thanks for your help.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Jul 09, 2010 12:47 pm 
Little Foot
Little Foot

Joined: Mon Jan 16, 2006 11:54 am
Posts: 143
EFFF YES!

I got it!

I knew it would be crazy simple.

thanks for all your help!


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jul 10, 2010 5:55 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
I know it sound cool to write something like a DES or 3DES program, but maybe you should put your enthusiasm on hold for a bit and write a program that does the classic ciphers like the monoalphabetic substitution cipher, polyalphabetic substitution cipher, Bacon's cipher, block cipher, etc.

Of course, if you this would require a slightly different cli interface. Instead of the filename arg, you'd probably want to do something more like...

myapp -enc -mono -file filename.txt
... OR maybe ...
enc -mono filename.txt
dec -block filename.txt

At least this way, you won't be killing yourself trying to implement an algorithm that appears to be quite a ways beyond your programming experience, and you'll benefit from having to compose all of these pieces together into a program(s). Just a thought.

Of course, the real fun begins when you start writing algorithms to break the classic ciphers. =)


Top
  Profile  
 
 Post subject:
PostPosted: Mon Jul 12, 2010 3:46 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
+1.

Much in favor of you sticking with some simpler programs. Especially since you admitted you aren't very good yet and you had trouble with cli arguments.

Trying a hard program like that to start is a good way to get pissed and quit and give up on a really good skill.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group