Browse Source

Using fread and fwrite

Piotr Czajkowski 4 years ago
parent
commit
b5b26b2a07
1 changed files with 9 additions and 7 deletions
  1. 9 7
      bom.c

+ 9 - 7
bom.c

@@ -1,6 +1,7 @@
 #include "bom.h"
 
 #define BOMSIZE 3
+#define CHUNKSIZE 1024
 unsigned char bom[] = { 0xEF, 0xBB, 0xBF };
 
 int checkBOM(const char *filePath) {
@@ -29,13 +30,14 @@ int removeBOM(const char *filePath) {
 	if (fseek(inputFile, BOMSIZE, SEEK_SET)) return ERROR;
 
 	FILE *tempFile = fopen(tempFileName, "w");
-	if (tempFile== NULL) return ERROR;
-
-	int c = fgetc(inputFile);
-	while (c != EOF) {
-		if (EOF == fputc(c, tempFile)) return ERROR;
-		c = fgetc(inputFile);
-	}
+	if (tempFile == NULL) return ERROR;
+
+	char buffer[CHUNKSIZE];
+	do {
+		int read = fread(buffer, 1, CHUNKSIZE, inputFile);
+		fwrite(buffer, 1, read, tempFile);
+	} while (!feof(inputFile));
+	
 	if (fclose(tempFile)) return ERROR;
 	if (fclose(inputFile)) return ERROR;