Solved

Easy --- File Copy -- but urgent

Posted on 1998-07-16
9
199 Views
Last Modified: 2010-04-15
Can anyone show me some code that will copy a file from one directory into another?
0
Comment
Question by:SteveWood
9 Comments
 
LVL 1

Expert Comment

by:saneesh100
ID: 1251737
In which OS do u want it ...
 in windows theer is an API call CopyFile which will do it for u
In dos  in TC u can use SYSTEM comand to do that,
please give more explanation to u'r question please..
0
 
LVL 2

Expert Comment

by:mlev
ID: 1251738
/* Error handling omitted */
#include <stdio.h>

FILE *file1, *file2;
int c;

file1 = fopen(<source filename>, "r");
file2 = fopen(<dest filename>, "w");
while ((c = getc(file1)) != EOF)
  putc(c, file2);
fclose(file1);
fclose(file2);
0
 
LVL 2

Expert Comment

by:mlev
ID: 1251739
Err, those filenames actually meant pathnames.
0
 
LVL 1

Author Comment

by:SteveWood
ID: 1251740
mlev -- I Think this will only work for text files.  I need something that will copy any type of file.

Will be in a console app that needs to run on NT and 95/98.
I am using VC++ 5
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 1

Author Comment

by:SteveWood
ID: 1251741
If anyone can provide me with an example CopyFile program that might be usefull to me, I could see if that does the trick.

mlev - If your previous answer proves correct I will give you the points.  I can't test it as it won't compile on my machine.
0
 
LVL 16

Expert Comment

by:imladris
ID: 1251742
There you are!

What is the status on the strstr problem?

0
 

Accepted Solution

by:
chrisbill earned 70 total points
ID: 1251743
// A program to copy a file
//

#include <windows.h>
#include <stdio.h>

int main( int argc, char *argv[] )
{
   if( argc != 3 ) {
      fprintf(stderr, "usage: MCP SRCFILE DESTFILE\n");
      return -1;
   }

   char *pSrcFile  = argv[1];
   char *pDestFile = argv[2];

   // If you want the copy to fail if the destination file
   //  exists, set the next bool to true.
   bool bFailIfFileExists = false;

   // Copy the file
   bool bResult = CopyFile(pSrcFile,pDestFile,bFailIfFileExists);

   // You can do some error handling here... (check bResult)
   //  That is, bResult=false if an error occurred.  If so,
   //  use GetLastError()
   return 1;
}

0
 
LVL 6

Expert Comment

by:alamo
ID: 1251744
mlev's example is as straightforward and simple as you can get, and should work or be very close. You mkight need to make the "r" and "w" into "rb" and "wb" to handle binary files, not certain which way it defaults. And of course you need to replace <source filename> with a path and filename string. Why doesn't it compile?
0
 
LVL 1

Author Comment

by:SteveWood
ID: 1251745
Cheers chrisbill this works a treat.  
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now