2015-03-01 3 views
0

Как вы пишете программу на C, которая может скопировать файл (заданный по пути исходного файла, например, /input/input.txt) в существующий каталог? Файл копии в каталоге должен иметь то же имя, что и входной файл.Скопируйте файл в каталог в C

Это кусок кода, который я до сих пор:

int copyfile1(char* infilename, char* outfileDir) { 
FILE* infile; //File handles for source and destination. 
FILE* outfile; 
DIR* outfileDir; 

infile = fopen(infilename, "r"); // Open the input and output files. 
if (infile == NULL) { 
    open_file_error(infilename); 
    return 1; 
} 

outfileDir = opendir(outfilename); 
if (outfile == NULL) { 
    open_file_error(outfilename); 
    return 1; 
} 

outfile = fopen(infilename, "w"); 

Я застрять здесь. Я не уверен, как обрабатывать выходной файл сейчас, так как он должен находиться в каталоге. Если я использую fopen(), он будет создан только в текущей директории.

Любая помощь будет оценена по достоинству.

Спасибо!

ответ

1

Вы можете использовать базовое (3) - http://linux.die.net/man/3/dirname

int copyfile1(char* infilename, char* outfileDir) { 
    FILE* infile; //File handles for source and destination. 
    FILE* outfile; 
    char outfilename[PATH_MAX]; 

    infile = fopen(infilename, "r"); // Open the input and output files. 
    if (infile == NULL) { 
     open_file_error(infilename); 
     return 1; 
    } 
    sprintf(outfilename, "%s/%s", outfileDir, basename(infilename)) 

    outfile = fopen(outfilename, "w"); 
Смежные вопросы