比如一个文件中存储了结构体数组code[100],结构体里有三个整型变量,现在我打开这个文件希望将code[N]读入到同样定义的结构体test中,我该如何读入?
FILE* pRF;
pRF = fopen("hbin.txt","rb");
instruction i; // instruction register
int num=0;
while (1)
{
fread(&i,sizeof(instruction),1,pRF);
if ( ferror(pRF) )
{
fclose(pRF);
perror("读文件过程中失败!");
return ;
}
if ( feof(pRF) )
{
break;
}
code[num] =i;
num++;
}
找了个简单的办法
åå³äºæ件ä¸æ°æ®çåå¨æ¹å¼ã
1 å¦ææ件ä¸åå¨çæ¹å¼ä¸ºäºè¿å¶å½¢å¼æ°æ®ï¼
éè¦ä½¿ç¨freadï¼Cè¯è¨é£æ ¼ï¼æifsteamçreadæåå½æ°(C++é£æ ¼ã)ä»æ件ä¸è¯»åç»æä½æ°æ®å°å¯¹åºçç»æä½æéä¸ã
å¦
struct test
{
int a;
};
struct test t;
fread(&t, 1,sizeof(t), fp);
æ
file.read(&t, sizeof(t));
2 å¦ææ件ä¸ï¼æ¯ä»¥ææ¬æ¹å¼åå¨çå¯è¯»çç»æä½æ°æ®ï¼
éè¦æ ¹æ®æ件ä¸æ°æ®çåå¨æ ¼å¼ï¼éè¿fscanfï¼Cè¯è¨é£æ ¼ï¼æifstreamç>>æåå½æ°ï¼è¯»åå个å¼å°å¯¹åºçç»æä½æååéä¸ã
å¦ struct test t;
fscanf(fp, "%d",&t.a);
æ
file>>t.a;
1 å¦ææ件ä¸åå¨çæ¹å¼ä¸ºäºè¿å¶å½¢å¼æ°æ®ï¼
éè¦ä½¿ç¨freadï¼Cè¯è¨é£æ ¼ï¼æifsteamçreadæåå½æ°(C++é£æ ¼ã)ä»æ件ä¸è¯»åç»æä½æ°æ®å°å¯¹åºçç»æä½æéä¸ã
å¦
struct test
{
int a;
};
struct test t;
fread(&t, 1,sizeof(t), fp);
æ
file.read(&t, sizeof(t));
2 å¦ææ件ä¸ï¼æ¯ä»¥ææ¬æ¹å¼åå¨çå¯è¯»çç»æä½æ°æ®ï¼
éè¦æ ¹æ®æ件ä¸æ°æ®çåå¨æ ¼å¼ï¼éè¿fscanfï¼Cè¯è¨é£æ ¼ï¼æifstreamç>>æåå½æ°ï¼è¯»åå个å¼å°å¯¹åºçç»æä½æååéä¸ã
å¦ struct test t;
fscanf(fp, "%d",&t.a);
æ
file>>t.a;
温馨提示:答案为网友推荐,仅供参考
第1个回答 2012-07-10
#include <iostream>
using namespace std;
typedef struct hardware{
int num;
int amount;
int price;
}instruction;
int main()
{
instruction code[100] = {0};
for ( int j=0; j < 100; ++j )
{
code[j].amount += j*2;
code[j].num += j*4;
code[j].price += j*8;
}
FILE* hbin = fopen("hbin.txt", "w");
for (j = 0; j < 100; j++)
fwrite(&code[j], sizeof(instruction), 1, hbin);
fclose(hbin);
ifstream iFile("hbin.txt", ios::binary);
if ( !iFile )
{
cout<<"open file error!"<<endl;
return -1;
}
iFile.seekg(0,ios::end);
int iFileSize = iFile.tellg();
iFile.seekg(0,ios::beg);
char* psbBuf = new char[iFileSize];
memset(psbBuf, 0, iFileSize);
iFile.read(psbBuf,iFileSize);
instruction* pware = (instruction*)psbBuf;
instruction ac[100] = {0};
for ( int i =0; i < iFileSize/sizeof(instruction); ++i)
{
ac[i].amount=pware->amount;
ac[i].num=pware->num;
ac[i].price=pware->price;
cout<<ac[i].amount<<endl;
++pware;
}
return 0;
}本回答被提问者和网友采纳
using namespace std;
typedef struct hardware{
int num;
int amount;
int price;
}instruction;
int main()
{
instruction code[100] = {0};
for ( int j=0; j < 100; ++j )
{
code[j].amount += j*2;
code[j].num += j*4;
code[j].price += j*8;
}
FILE* hbin = fopen("hbin.txt", "w");
for (j = 0; j < 100; j++)
fwrite(&code[j], sizeof(instruction), 1, hbin);
fclose(hbin);
ifstream iFile("hbin.txt", ios::binary);
if ( !iFile )
{
cout<<"open file error!"<<endl;
return -1;
}
iFile.seekg(0,ios::end);
int iFileSize = iFile.tellg();
iFile.seekg(0,ios::beg);
char* psbBuf = new char[iFileSize];
memset(psbBuf, 0, iFileSize);
iFile.read(psbBuf,iFileSize);
instruction* pware = (instruction*)psbBuf;
instruction ac[100] = {0};
for ( int i =0; i < iFileSize/sizeof(instruction); ++i)
{
ac[i].amount=pware->amount;
ac[i].num=pware->num;
ac[i].price=pware->price;
cout<<ac[i].amount<<endl;
++pware;
}
return 0;
}本回答被提问者和网友采纳
第2个回答 2012-07-10
和这个差不多啊,主要是把fwrite换成fread
二进制,fopen的参数,要加上b
写成FILE *fp = fopen("hbin.txt", "rb");
否则读不完整
二进制,fopen的参数,要加上b
写成FILE *fp = fopen("hbin.txt", "rb");
否则读不完整
第3个回答 2012-07-10
这个没有捷径..
1.你可以用正则表达直接匹配。
2.需要你自己按行解析了。
1.你可以用正则表达直接匹配。
2.需要你自己按行解析了。
第4个回答 2012-07-10
1.
FILE *hbin = fopen("hbin.txt", "r");
fread(code, sizeof(instruction), 100, hbin);
fclose(hbin);
FILE *hbin = fopen("hbin.txt", "r");
fread(code, sizeof(instruction), 100, hbin);
fclose(hbin);