#include <stdio.h>
#include <stdlib.h>
typedef struct filedata
{
char data[100];
}data_t;
data_t * fname=NULL;
//IS AN ARRAY OF the structure filedata REQUIRED HERE
void quit()
{
printf("\nPress enter to exit");
fflush(stdin);
getchar();
}
int main()
{
char ch;
fname=(data_t *)malloc(sizeof(data_t));
FILE *fptr=NULL;
atexit(quit);
printf("Please enter the file name to read : ");
fflush(stdin);
scanf("%s",fname->data);
fptr=fopen(fname->data,"rb");
if(fptr == NULL)
{
perror("Could not open the file ");
return;
}
printf("\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("Contents of the file %s are : ",fname->data);
while(fread(&fname, sizeof(data_t), 1,fptr) == 1)
{
// what do I put here?
}
fclose(fptr);
return 0;
}
我想讀取任何包含我的計算機中的一些文本或數字的二進制文件,並將其顯示在標準輸出中。閱讀二進制文件的文本內容並將其顯示到標準輸出
我該怎麼做?
我應該聲明一個結構文件數據的數組,如data_t data[100]
?
我應該在上面的while循環中顯示內容?
一個例子:當然如果我知道像名字stucture裏面的屬性,年齡等那麼我可以這樣做
while (fread (&e, sizeof (e), 1, fp) == 1)
printf ("\n%s %d %f", e.name, e.age, e.bs) ;
但我怎麼看任何二進制文件的任何文字內容,顯示它到標準輸出?
仍然我無法得到它,是否有任何其他的方式,並將此線爲二進制文件工作,而((c = fgetc(f))!= EOF)仍然有問題幫助?????? ?? – mukesh 2011-04-03 10:13:59
我從一個名叫jim的人那裏得到了我的問題的回覆,他說:*爲了讀取二進制文件,您必須知道該文件是如何寫入的。你必須知道如何寫入變量,它們的大小,以字節爲單位。它們的類型,double,float,int,char,用戶定義的類型等等。這些變量被寫入文件的順序。並持續保存每個變量所需的字節數。 所以通常不可能取「任何」二進制文件並讀取內容。你可以閱讀一個精確的二進制文件,你知道確切的佈局,但不是一個二進制文件,你不知道佈局。** Jim – mukesh 2011-04-04 05:48:43