[英]Fwrite function cuts off data in output .tar file
我有一段代碼旨在復制 a.tar 文件的創建,盡管它是一個更簡單的版本。 它將接收 2.txt 文件並生成 2 個文件的 .tar 文件。 下面是我這樣做的代碼。 然而,當打開 .tar 文件時,它確實已損壞,通過在十六進制編輯器中查看數據,數據確實被截斷了。
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define RECORDSIZE 512
#define NAMSIZ 100
#define TUNMLEN 32
#define TGNMLEN 32
struct header {
char name[NAMSIZ];//needed
char size[12];//needed
};
int main(int argc, char** argv) {
//argv[1] file1 argv[2] file2
char* file1 = argv[1];
char* file2 = argv[2];
FILE* f;
int lSize;
char temp_length[10];
char* file1_data, * file2_data;
int result;
//char* output_str = (char*)malloc(sizeof)
f = fopen(file1, "rb");
if (f == NULL) {
printf("File error!");
return 1;
}
fseek(f, 0, SEEK_END);
lSize = ftell(f);
fseek(f, 0, SEEK_SET);
file1_data = (char*)malloc(sizeof(char) * lSize);
if (file1_data == NULL) {
printf("Memory error!");
return 1;
}
result = fread(file1_data, 1, lSize, f);
file1_data[result] = '\0';
fclose(f);
sprintf(temp_length, "%d", lSize);
struct header* h1 = malloc(sizeof(struct header));
strcpy(h1->name, file1);
strcpy(h1->size, temp_length);
printf("Name:%s Value:%s\n", h1->name, h1->size);
printf("File 1 data:%s\n", file1_data);
f = fopen(file2, "rb");
if (f == NULL) {
printf("File error!");
return 1;
}
fseek(f, 0, SEEK_END);
lSize = ftell(f);
fseek(f, 0, SEEK_SET);
file2_data = (char*)malloc(sizeof(char) * lSize);
if (file2_data == NULL) {
printf("Memory error!");
return 1;
}
result = fread(file2_data, 1, lSize, f);
file2_data[result] = '\0';
fclose(f);
sprintf(temp_length, "%d", lSize);
struct header* h2 = malloc(sizeof(struct header));
strcpy(h2->name, file1);
strcpy(h2->size, temp_length);
printf("Name:%s Value:%s\n", h2->name, h2->size);
printf("File 2 data:%s\n", file2_data);
//allocate mem for output buffer
int total = sizeof(struct header) + sizeof(struct header) + sizeof(file1_data) + sizeof(file2_data);
printf("total length %d\n", total);
f = fopen("Result.tar", "wb");
//fwrite(input,length,no of ele,output buffer)
fwrite(h1, sizeof(struct header), 1, f);
fwrite(file1_data, sizeof(file1_data), 1, f);
fwrite(h2, sizeof(struct header), 1, f);
fwrite(file2_data, sizeof(file2_data), 1, f);
if (fwrite != 0)
printf("Contents to file written successfully !\n");
else
printf("Error writing file !\n");
fclose(f);
}
第一個文件名:File1.txt 內的數據:
This is File 1.
第二個文件名:File2.txt 內的數據:
This is File 2.
File 2 has more data inside.
解碼文本 output:
File1.txt�ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ31�ÍÍÍÍÍÍÍÍÍThis is File1.txt�ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ63�ÍÍÍÍÍÍÍÍÍThis is
據觀察,文件 2 的名稱以及后續數據已被截斷。 我一直在調試,但我不確定我哪里出錯了,因為 fwrite 不返回 0 因此,我假設它是成功的。
由於打印語句正是我所期望的,我不認為數據的讀取是問題,而是 fwrite function。因此我想就此尋求建議。
錯誤在於您計算總長度的方式。 實際上,您不想使用sizeof(file1_data)
作為文件的長度。 相反,您想使用讀取result
時返回的值。
創建兩個變量file1_length
和file2_length
。 然后用各自文件的大小填充它們:
...
size_t f1_len, f2_len;
...
f = fopen(file1, "rb");
if (f == NULL) {
printf("File error!");
return 1;
}
fseek(f, 0, SEEK_END);
f1_size = ftell(f);
fseek(f, 0, SEEK_SET);
file1_data = (char*)malloc(sizeof(char) * f1_len);
...
int total = sizeof(struct header) + sizeof(struct header) + f1_len + f2_len;
...
fwrite(h1, sizeof(struct header), 1, f);
fwrite(file1_data, f1_len, 1, f);
fwrite(h2, sizeof(struct header), 1, f);
fwrite(file2_data, f2_len, 1, f);
...
最后,使用這些變量作為文件內容的長度。
注意: sizeof(file1_data)
得到的值表示類型的大小。 在這里,因為file1_data
是char *
類型,所以你得到4
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.