Teen Programmers Unite  
 

 

Return to forum top

i need c++ codes

Posted by whizkide [send private reply] at July 19, 2002, 09:34:57 AM

could someone pls give 2 pieces of code. 1 to copy a file( any type) and another to delete a file from its directory.
Thanx for the help.
whizzzzzzzzzzz

Posted by RedX [send private reply] at July 19, 2002, 12:03:51 PM

Simple:


#include <iostream>
#include <fstream>
#include <cstdlib>

void error(const char* p, const char *p2 = " ")
{
   std::cerr << p << " " << p2 << '\n';
   std::exit(1);
}

int main(int argc, char *argv[])
{
   if (argc != 3) error("wrong number of arguments");

   std::ifstream from(argv[1]);  // open input file stream
   if (!from) error("cannot open input file", argv[1]);

   std::ofstream to(argv[2]);     // open output file stream
   if (!to) error("cannot open output file", argv[2]);

   char ch;
   while (from.get(ch) ) { to.put(ch); }

   if (!from.eof() || !to) error("something strange happened");
}



This is the perfect C++ style. (as it is an example from "The C++ programming language".)

The other one is easy:

#include <stdio.h>


int main()
{
   remove("temp.tmp");
}



RedX
Posted by Zandalf [send private reply] at July 19, 2002, 01:10:56 PM

the other thing you could try is the system() function, but that requires knowledge of your operating system.

Posted by Psion [send private reply] at July 19, 2002, 02:58:27 PM

That looks dangerous. Do ofstream's really use the 0 character to stand for both end of file and for ASCII character 0 actually present in a file?

Posted by CodeRed [send private reply] at July 19, 2002, 09:55:58 PM

EOF has it's own ascii code doesn't it?

Posted by Zandalf [send private reply] at July 19, 2002, 11:36:39 PM

it's not an ascii code when you're using the from.eof() function. I'm not quite sure what it is, but I think that the stream knows how long the file should be and reads until the iterator hits that point.

Posted by buzgub [send private reply] at July 20, 2002, 12:02:10 AM

EOf is a value, guranteed to fit in an int (but not in a char). This is why getchar and friends return ints.

Posted by vladimir_l [send private reply] at July 20, 2002, 01:31:00 PM

EOF is the worst thing I encountered when I started programming , I understand it now , so watch out for it - it can be nasty if you dont know what it is.

-Vlad "byte blighter"

Posted by CViper [send private reply] at July 21, 2002, 03:55:27 AM

Well anyway.. reading a file byte for byte... urgh!
I'd say there are better ways to do that. Something like

FILE *in, *out;
UCHAR buff[1024];
int read;

// Open stuff etc

do
{
    read = fread( buff, 1024, 1, in );
    fwrite( buff, read, 1, out );
} while( read == 1024 );

Note: you maybe got to swap 1024 and 1 in the fread() for this to work...
Maybe to as nice c++ but... whatever.. it's faster and there's no EOF stuff :)
Posted by Zandalf [send private reply] at July 21, 2002, 07:34:48 PM

you can do basically the same thing in C++ using ifstream.get() (or some function like that, I'm not going to go look it up right now)

Posted by CViper [send private reply] at July 22, 2002, 06:27:58 AM

probably. It's just that i don't know the c++ streams very well. Never bothered about them - there's nothing one could do with them that you can't do with the "old" c functions...

Posted by Zandalf [send private reply] at July 22, 2002, 02:49:36 PM

cvip - true, but if you're including bits of the STL in C++, there's no reason to include the old functions just becasue you aren't willing to learn the new stuff ^_^. different progging styles, s'all

Posted by CViper [send private reply] at July 22, 2002, 03:16:12 PM

I use STL now and then - it's just the iostream part i dont like very much.. But it's true, everyone got his/her own style :)

Edit: i'm sure there are some other messy parts in the stl... i just never stumbled across them

Posted by Zandalf [send private reply] at July 22, 2002, 03:20:05 PM

hehe, I remember one: trying to use the map in VC6... always ended up with an identifier name longer than 255 chars because of all the templating in the STL. thing with VC6 was, you can't turn off that warning, so the output would be full of all this warning -
identifier:"<really long POS>" is longer than 255 characters and may cause problems while debugging...

fun times

Posted by CViper [send private reply] at July 22, 2002, 03:37:50 PM

yeah... i know that one, but you're sure you can't turn it off with #pragma warning( disable: XXX ) ? I don't remember but i thought it worked...

Posted by Zandalf [send private reply] at July 24, 2002, 11:50:51 PM

You can not turn it off in VC++ 6. it's actually quite funny, I spent a long time learning about the #pragma warning(disable: C####), put it in the code, and it still didn't do anything. Went online, searched for that warning to see if anyone else had gotten the same problem, first link was a knowledge base article. It said (quite simply) that you cannot disable that warning, and that microsoft had acknowledged that problem.

I laughed for a long time ^_^

Posted by whizkide [send private reply] at July 25, 2002, 10:56:08 AM

i tried using the following code and it worked but i need something more efficient
#include<stdlib.h>
int main()
{system("copy source.ext dest.ext")
system("del source.ext")
return 0;
}

Posted by RedX [send private reply] at July 25, 2002, 12:05:34 PM

>system("copy source.ext dest.ext")
>system("del source.ext")

is the same as

system("ren source.ext dest.ext");

Posted by Psion [send private reply] at July 25, 2002, 12:07:47 PM

LOL.... I don't think he was looking for that.

As to the request for "something more efficient", I think CViper already gave it above....

Posted by CViper [send private reply] at July 25, 2002, 04:22:36 PM

well if all you want to do is rename/move a file, there's a function called rename() in stdio.h. don't know if it's standart though...

Posted by RedX [send private reply] at July 25, 2002, 04:28:01 PM

My code was twice as efficient as Whizkide's. :-p

Posted by whizkide [send private reply] at July 25, 2002, 04:34:06 PM

yeah yeah redx
But unfortunately, that piece of code is too confusing. Thanx anyway

Posted by Psion [send private reply] at July 25, 2002, 04:59:49 PM

If you think CViper's copying code is too confusing, then you are not yet ready to do anything this complicated in C.

Posted by whizkide [send private reply] at July 25, 2002, 07:02:02 PM

not cvipers code
i meant redx's code @psion

Posted by whizkide [send private reply] at July 25, 2002, 07:05:37 PM

how abt something like this


#include<stdio.h>
int main(){
FILE* inf=fopen("source.ext","rb");
FILE* outf=fopen("dist.ext","w+b");
int byte;
for(;;){
byte=fgetc(inf);
if(byte==EOF) break;
fputc(byte,outf);
}
fclose(inf);
fclose(outf);
return 0;
}

Posted by Psion [send private reply] at July 25, 2002, 07:44:09 PM

You are really confusing me, whizkide. The code you just gave is the most inefficient we have seen yet. Why don't you use CViper's?

Posted by gian [send private reply] at July 25, 2002, 07:56:25 PM

But he is the best programmer ever, so who are we to question him?

Posted by Zandalf [send private reply] at July 26, 2002, 06:20:59 PM

whizkide (please change your profile so that we don't have to hear gian complain _every_ time you post) - you do realize that you are playing with you hard drive, right? yes. and you do realize that hard drive access speeds are one of the most significant bottlenecks of computing today, right?

Now, I'm only trying to mock you a little, since you seem to be mocking everyone who is trying to help you here, but also consider that making calls to the built in system functions is acutally _the_ most efficient thing you can do, because thousands of expert programmers have spent countless hours perfecting those simple commands. I doubt you can do better through a compiled language.

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.