来自 .dat 文件的 fread()
fread() from .dat file
我有一个 .dat
文件,其中包含姓名、城镇和电话号码等数据。我确实设法打开文件并读取了 a while
循环中的第一行条目,但在那之后,它只会一遍又一遍地重复同样的事情。这是代码的一部分:
while(fread(&customer.town,1,sizeof(customer.town), fopen(input,"r" "b")) ==1 )
{
printf("%i. Town...........: %s\n",i,&customer.town);
}
编辑:
截至目前,代码如下所示:
customer pp[3];
...
fread(pp, sizeof(customer), 9, fp);
for (i=0;i<=5;i++)
{
printf("%i\t%s\n",++j,pp[i].town);
}
getch();
我遇到的唯一问题是它显示第一个条目很好,但之后 "eats" 前几个字母。
编辑 2
详细信息:每个结构变量都有多个条目。我现在也只关注城镇。
问题:只有输出 1 个城镇是正确的,其他条目要么是空白字符,要么是随机字符。
当前代码:
#include <conio.h>
#include <stdio.h>
#define FILENAME "customer.dat"
int main()
{
typedef struct Satz
{
char town[11];
int costumerid[20];
}test __attribute__((packed));
test output[20];
FILE *fp;
int size=sizeof(output[20].town);
int i=0;
int j=0;
int limit=0;
fp=fopen(FILENAME,"rb");
if (fp==NULL)
{
printf("Couldnt open the file:%s!\n", FILENAME);
getch();
return(-1);
}
printf("Limit: %i \n\n", limit=fread(output,1,size, fp));
for (i=0;i<limit;i++)
{
printf("%i\t%s\n",++j,output[i].town);
}
getch();
}
控制台输出:
Limit: 11
1 Leipzig
2
3
4 ░w
5
6 hðv
7 J
8 \
9 o
10 i
11 i
2-11个随机
编辑 3
int main()
{
typedef struct Satz
{
char town[11];
}__attribute__((packed))test;
test output[30];
FILE *fp;
int size=sizeof(output[20].town);
int i=0;
int j=0;
int limit=0;
fp=fopen(FILENAME,"rb");
if (fp==NULL)
{
printf("Couldnt open the file:%s!\n", FILENAME);
getch();
return(-1);
}
printf("Limit: %i \n\n", limit=fread(output,sizeof(test),11, fp));
for (i=0;i<=limit;i++)
{
printf("%i\t%s\n",++j,output[i].town);
}
getch();
}
输出:
Limit: 5
1 Leipzig
2 alle
3 rlin
4 tock
5 erin
6 us
预期输出:
1 Leipzig
2 Halle
3 Berlin
4 Rostock
5 Schwerin
6 Cottbus
您的代码在每个循环中重新执行 fopen()
。有关正确使用的示例,请参阅 http://www.cplusplus.com/reference/cstdio/fread/,将 fopen()
放在 while
之前,然后您的循环应该可以正常工作。
具体来说,您需要分配一个 FILE*
,从 fopen()
中给它一个值,检查它是否失败,然后在您的 fread()
调用中使用那个 FILE*
.
你发给我的输入文件有一些额外的记录由 xxd
在最后一个合法条目转换回二进制后导致 fread()
读取 14 个空值的额外记录。
这是在我的测试中有效的代码。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
struct __attribute__((packed)) wetter
{
char ort[11];
float temp[30];
unsigned short niederschalg[30];
unsigned short luftdruck[30];
float luftfeuchte[30];
};
struct wetter output[20];
int limits, i;
printf("The size of record is %d\n", sizeof(struct wetter));
FILE *fp=fopen("orig","r");
printf("The limit is %i\n", limits=fread(output,sizeof(struct wetter),20,fp));
for(i=0;i<limits;i++)
printf("%d\t%s\n", i+1, output[i].ort);
}
这是代码的输出。
The size of record is 371
The limit is 20
1 Leipzig
2 Halle
3 Berlin
4 Rostock
5 Schwerin
6 Cottbus
7
8
9
10
11
12
13
14
15
16
17
18
19
20
如果没有打包属性,输出显示与您的类似。
The size of record is 372
The limit is 20
1 Leipzig
2 alle
3 rlin
4 tock
5 erin
6 us
7
8
9
10
11
12
13
14
15
16
17
18
19
20
设置属性时的一个注意事项。此 typedef 将导致编译器忽略 'packed' 属性。
typedef struct wetter
{
char ort[11];
float temp[30];
unsigned short niederschalg[30];
unsigned short luftdruck[30];
float luftfeuchte[30];
}test __attribute__((packed));
此 typedef 不会导致编译器忽略 'packed' 属性。
typedef struct __attribute__((packed)) wetter
{
char ort[11];
float temp[30];
unsigned short niederschalg[30];
unsigned short luftdruck[30];
float luftfeuchte[30];
}test;
对于未 typedef
ed 的结构,问题不会显示。
我有一个 .dat
文件,其中包含姓名、城镇和电话号码等数据。我确实设法打开文件并读取了 a while
循环中的第一行条目,但在那之后,它只会一遍又一遍地重复同样的事情。这是代码的一部分:
while(fread(&customer.town,1,sizeof(customer.town), fopen(input,"r" "b")) ==1 )
{
printf("%i. Town...........: %s\n",i,&customer.town);
}
编辑: 截至目前,代码如下所示:
customer pp[3];
...
fread(pp, sizeof(customer), 9, fp);
for (i=0;i<=5;i++)
{
printf("%i\t%s\n",++j,pp[i].town);
}
getch();
我遇到的唯一问题是它显示第一个条目很好,但之后 "eats" 前几个字母。
编辑 2
详细信息:每个结构变量都有多个条目。我现在也只关注城镇。 问题:只有输出 1 个城镇是正确的,其他条目要么是空白字符,要么是随机字符。
当前代码:
#include <conio.h>
#include <stdio.h>
#define FILENAME "customer.dat"
int main()
{
typedef struct Satz
{
char town[11];
int costumerid[20];
}test __attribute__((packed));
test output[20];
FILE *fp;
int size=sizeof(output[20].town);
int i=0;
int j=0;
int limit=0;
fp=fopen(FILENAME,"rb");
if (fp==NULL)
{
printf("Couldnt open the file:%s!\n", FILENAME);
getch();
return(-1);
}
printf("Limit: %i \n\n", limit=fread(output,1,size, fp));
for (i=0;i<limit;i++)
{
printf("%i\t%s\n",++j,output[i].town);
}
getch();
}
控制台输出:
Limit: 11
1 Leipzig
2
3
4 ░w
5
6 hðv
7 J
8 \
9 o
10 i
11 i
2-11个随机
编辑 3
int main()
{
typedef struct Satz
{
char town[11];
}__attribute__((packed))test;
test output[30];
FILE *fp;
int size=sizeof(output[20].town);
int i=0;
int j=0;
int limit=0;
fp=fopen(FILENAME,"rb");
if (fp==NULL)
{
printf("Couldnt open the file:%s!\n", FILENAME);
getch();
return(-1);
}
printf("Limit: %i \n\n", limit=fread(output,sizeof(test),11, fp));
for (i=0;i<=limit;i++)
{
printf("%i\t%s\n",++j,output[i].town);
}
getch();
}
输出:
Limit: 5
1 Leipzig
2 alle
3 rlin
4 tock
5 erin
6 us
预期输出:
1 Leipzig
2 Halle
3 Berlin
4 Rostock
5 Schwerin
6 Cottbus
您的代码在每个循环中重新执行 fopen()
。有关正确使用的示例,请参阅 http://www.cplusplus.com/reference/cstdio/fread/,将 fopen()
放在 while
之前,然后您的循环应该可以正常工作。
具体来说,您需要分配一个 FILE*
,从 fopen()
中给它一个值,检查它是否失败,然后在您的 fread()
调用中使用那个 FILE*
.
你发给我的输入文件有一些额外的记录由 xxd
在最后一个合法条目转换回二进制后导致 fread()
读取 14 个空值的额外记录。
这是在我的测试中有效的代码。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
struct __attribute__((packed)) wetter
{
char ort[11];
float temp[30];
unsigned short niederschalg[30];
unsigned short luftdruck[30];
float luftfeuchte[30];
};
struct wetter output[20];
int limits, i;
printf("The size of record is %d\n", sizeof(struct wetter));
FILE *fp=fopen("orig","r");
printf("The limit is %i\n", limits=fread(output,sizeof(struct wetter),20,fp));
for(i=0;i<limits;i++)
printf("%d\t%s\n", i+1, output[i].ort);
}
这是代码的输出。
The size of record is 371
The limit is 20
1 Leipzig
2 Halle
3 Berlin
4 Rostock
5 Schwerin
6 Cottbus
7
8
9
10
11
12
13
14
15
16
17
18
19
20
如果没有打包属性,输出显示与您的类似。
The size of record is 372
The limit is 20
1 Leipzig
2 alle
3 rlin
4 tock
5 erin
6 us
7
8
9
10
11
12
13
14
15
16
17
18
19
20
设置属性时的一个注意事项。此 typedef 将导致编译器忽略 'packed' 属性。
typedef struct wetter
{
char ort[11];
float temp[30];
unsigned short niederschalg[30];
unsigned short luftdruck[30];
float luftfeuchte[30];
}test __attribute__((packed));
此 typedef 不会导致编译器忽略 'packed' 属性。
typedef struct __attribute__((packed)) wetter
{
char ort[11];
float temp[30];
unsigned short niederschalg[30];
unsigned short luftdruck[30];
float luftfeuchte[30];
}test;
对于未 typedef
ed 的结构,问题不会显示。