【发布时间】:2013-09-03 14:42:01
【问题描述】:
在this Wikipedia 页面上有一个示例 C 程序从文件读取和打印前 5 个字节:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char buffer[5] = {0}; /* initialized to zeroes */
int i;
FILE *fp = fopen("myfile", "rb");
if (fp == NULL) {
perror("Failed to open file \"myfile\"");
return EXIT_FAILURE;
}
for (i = 0; i < 5; i++) {
int rc = getc(fp);
if (rc == EOF) {
fputs("An error occurred while reading the file.\n", stderr);
return EXIT_FAILURE;
}
buffer[i] = rc;
}
fclose(fp);
printf("The bytes read were... %x %x %x %x %x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]);
return EXIT_SUCCESS;
}
我不明白的部分是它使用了getc 函数,该函数返回一个int 并将其存储在chars 的数组中-如何将ints 存储在@987654327 中@数组?
【问题讨论】: