LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2008, 11:41 AM   #1
CelticBlues
LQ Newbie
 
Registered: Jan 2005
Posts: 29

Rep: Reputation: 15
Copying files in c program


Is there a way to copy a file at run time in a c or c++ app in linux? I am assuming there is something where can execute a system level call such as 'cp' and pass it parameters, but I haven't found anything like that. I found exec, but only in reference to shell scripts.

thanks,
CB
 
Old 08-11-2008, 12:36 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by CelticBlues View Post
Is there a way to copy a file at run time in a c or c++ app in linux? I am assuming there is something where can execute a system level call such as 'cp' and pass it parameters, but I haven't found anything like that. I found exec, but only in reference to shell scripts.

thanks,
CB
This link may help you:

http://cboard.cprogramming.com/showthread.php?p=744616

This link:

http://www.elook.org/programming/c/system.html

Covers how to call system-commands from a C program. Very basic C programming, surprised it's not in your books.
 
Old 08-11-2008, 12:46 PM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Or if you need a quick solution, you can use system()

man 3 system
 
Old 08-11-2008, 01:01 PM   #4
CelticBlues
LQ Newbie
 
Registered: Jan 2005
Posts: 29

Original Poster
Rep: Reputation: 15
system("cp file1.ext file2.ext"); ?

I tried system, and it returned 0, which is what it should return if the command executed without error I think...

I tried this:

system("cp file1.ext file2.ext");

Is that right?

CB

Quote:
Originally Posted by Mr. C. View Post
Or if you need a quick solution, you can use system()

man 3 system
 
Old 08-11-2008, 01:27 PM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
That is correct.
 
Old 08-11-2008, 06:18 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Without system:
Code:
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>


#define BUFFER_SIZE 256

int main(int argc, char *argv[])
{
	//check number of arguments

	if (argc != 3)
	{
	fprintf(stderr, "%s [input file] [output file]\n", argv[0]);
	return 1;
	}


	//open the first file "read-only"

	int input = open(argv[1], O_RDONLY);
	if (input < 0)
	{
	fprintf(stderr, "%s: could not read file '%s'\n", argv[0], argv[1]);
	return 1;
	}


	//find the permissions of the first file so we can copy them

	int permissions = 0644;
	struct stat file_stats;

	if (fstat(input, &file_stats) != -1)
	permissions = file_stats.st_mode & 07777;


	//open the second file "write-only" using the first file's permissions
	//note: the permissions will only be used if the file doesn't exist

	int output = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, permissions);
	if (output < 0)
	{
	fprintf(stderr, "%s: could not write file '%s'\n", argv[0], argv[2]);
	return 1;
	}


	//create a modestly-sized buffer for holding data

	char buffer[BUFFER_SIZE];
	ssize_t read_size = 0;


	//read as much data as the first file contains

	while ((read_size = read(input, buffer, BUFFER_SIZE)) != 0)
	{
	if (read_size == (ssize_t) -1)
	 {
	fprintf(stderr, "%s: error reading file '%s'\n", argv[0], argv[1]);
	return 1;
	 }

	else if (write(output, buffer, read_size) != read_size)
	 {
	fprintf(stderr, "%s: error writing file '%s'\n", argv[0], argv[2]);
	return 1;
	 }
	}


	return 0;
}
ta0kira

Last edited by ta0kira; 08-11-2008 at 06:21 PM.
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copying compiled program to another computer? babag Linux - Newbie 2 05-28-2008 05:41 PM
Copying program for linux? like copy handler... randknu Ubuntu 3 02-10-2008 02:23 AM
C program for copying file in size of 1k blocks bhandu Linux - General 2 06-11-2007 07:01 AM
what cd iso copying/burning program should i use??? nenyo Linux - Software 2 08-24-2005 07:14 PM
File copying program alltime Programming 8 02-27-2005 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration