Hallo,
ich hab ne Aufgabe wo ich eine beliebige Datei byteweise kopieren soll.
Nun, Textdateien klappen super. Egal ob die Datei ein Sektor belegt oder mehrere. Wenn ich nun allerdings n Pdf mit einer größe von 28KB (die txt war 5KB) kopieren will hört er nach einem Lesen und Schreiben auf.
|
C/C++ Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#define MAXBYTES 1024
int main(int argc, char **argv) {
if(argc < 3) {
(void) printf("usage of mybcp: mybcp [file to read] [file to write]");
return EXIT_SUCCESS;
}
/* create a buffer variable */
int *buffer = malloc(MAXBYTES);
int readBytes = 1;
int writtenBytes = 0;
/* get the two file args */
const char *readFile = *(argv + 1);
const char *writeFile = *(argv + 2);
/* open both files, create write file if it doesn't exists, set permissions */
int openReadFile;
if((openReadFile = open(readFile, O_RDONLY)) == -1) {
(void) printf("error at opening reading file!");
return EXIT_FAILURE;
}
int openWriteFile;
if((openWriteFile = open(writeFile, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR)) == -1) {
(void) printf("error at opening writing file!");
return EXIT_FAILURE;
}
/* read and write until read bytes are less or equal 0 */
while((readBytes = read(openReadFile, buffer, MAXBYTES)) > 0) {
writtenBytes = write(openWriteFile, buffer, readBytes);
(void) printf("Anzahl Bytes gelesen: %d, Anzahl Bytes geschrieben: %d\n", readBytes, writtenBytes);
}
/* close file descriptor */
close(openReadFile);
close(openWriteFile);
return EXIT_SUCCESS;
}
|
Versteh ich irgendwie die Funktion von von write und read falsch oder weswegen geht es nur bei Textdateien?