Witam,
mam do napisania w języku C program, który przy użyciu read oraz write będzie kopiował pliki. Nie działa on jednak prawidłowo, gdyż kopiuje zawsze kilka linijek pliku tekstowego więcej, niż w pliku kopiowanym. Gdzie popełniam błąd. Oto mój kod
#include<sys/mman.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#define STR_LENGTH 255
void getPaths(char source[], char direction[] )
{
printf ("Podaj sciezke do pliku zrodlowe: ");
scanf ("%s",source);
printf ("Podaj sciezke do zapisu: ");
scanf ("%s",direction);
}
int openFile (char path[])
{
int descriptor = open(path,O_RDONLY,0600);
if(descriptor==-1) {
perror("open");
return descriptor;
}
return descriptor;
}
int createFile (char path[])
{
int descriptor = creat(path,0600);
if(descriptor==-1) {
perror("creat :");
return descriptor;
}
return descriptor;
}
int main(void)
{
struct stat fileInformation;
struct stat newFileInformation;
char source [STR_LENGTH] = "";
char direction [STR_LENGTH] = "";
char buff [50];
getPaths(source, direction);
int sourceDescriptor = openFile(source);
if(sourceDescriptor == -1)
perror ("openFile: ");
int directionDescriptor = createFile(direction);
if(directionDescriptor == -1)
perror ("createFile: ");
if(fstat(sourceDescriptor, &fileInformation) == -1)
perror("fstat: ");
if(fstat(directionDescriptor, &newFileInformation) == -1)
perror("fstat: ");
while (newFileInformation.st_size < fileInformation.st_size)
{
if(fstat(directionDescriptor, &newFileInformation) == -1)
perror("fstat: ");
if (read(sourceDescriptor, buff, sizeof(buff)) == -1)
perror("read: ");
if(write(directionDescriptor, buff, sizeof(buff)) == -1)
perror ("write: ");
}
if(close(sourceDescriptor)==-1)
perror("close");
if(close(directionDescriptor)==-1)
perror("close");
return 0;
}
Pozdrawiam.