You are not logged in.

  • Login

1

Saturday, October 15th 2011, 3:32pm

ANSI C I/O Write/Read

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?

2

Saturday, October 15th 2011, 6:43pm

Irgendwie stehe ich gerade auf dem Schlauch. ANSI C?

Welche Header nutzt du unter welchem Compiler?
Wie lautet die Endung deines Quelltextes?

MfG bcc-fan

3

Saturday, October 15th 2011, 7:50pm

open öffnet die Dateien standardmäßig im Textmodus (O_TEXT). Deswegen funktioniert das mit Textdateien auch problemlos. Für Binärdaten musst du noch das Flag O_BINARY hin-odern ;)

4

Saturday, October 15th 2011, 8:12pm

Die Lösung wurde schon von Rondrer gesagt.

Des Weiteren, wenn du schon malloc verwendest dann solltest du auch free verwenden!

Mfg Rushh0ur

5

Saturday, October 15th 2011, 9:36pm

Danke für die Antworten! Was merkwürdig ist das es unter Linux auf einmal klappte aber unter Windows nicht. (also ohne das o_binary flag)

Naja gut dann guck ich mal mit dem Flag und probiers dann aus.


Und mit free: Ich weis, ich versuchs mir zu merken :D!



@bcc-fan gnu compiler collection c compiler, .c

6

Sunday, October 16th 2011, 10:52am

Ich kann mich irren, aber open ist nicht Bestandteil von ANSI C?
Viele C-Compiler werden das trotzdem beherrschen.

MfG bcc-fan

7

Sunday, October 16th 2011, 3:02pm

Danke für die Antworten! Was merkwürdig ist das es unter Linux auf einmal klappte aber unter Windows nicht. (also ohne das o_binary flag)


Ich zitiere mal eben:
"In many environments, such as most UNIX-based systems, it makes no difference to open a file as a text file or a binary file; Both are treated exactly the same way, but differentiation is recommended for a better portability."

Similar threads

Social bookmarks