标准IO
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define READ_LEN 1024
int main(int argc, char **argv)
{
if(argc < 2)
{
fprintf(stderr,"%s:Missing parameters\n",argv[0]);
exit(1);
}
FILE * file = NULL;
file = fopen(argv[1],"r");
if(file == NULL)
{
fprintf(stderr,"fopen():%s",strerror(errno));
exit(2);
}
char read_buf[READ_LEN] = {0};
int read_ret = 0;
while(1)
{
read_ret = fread(read_buf,1,READ_LEN,file);
if(read_ret <= 0)
{
if(ferror(file) != 0)
fprintf(stderr,"fread error!\n");
break;
}
printf("%s",read_buf);
}
fclose(file);
exit(0);
}
文件IO
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#define READ_LEN 1024
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "%s:Missing parameters\n", argv[0]);
exit(1);
}
int fd = 0;
fd = open(argv[1], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "open():%s", strerror(errno));
exit(2);
}
char read_buf[READ_LEN] = {0};
int read_ret = 0;
while (1) {
read_ret = read(fd, read_buf, READ_LEN);
if (read_ret > 0) {
printf("%s", read_buf);
}
if (read_ret == 0)
break;
if (read_ret == -1) {
fprintf(stderr, "read():%s", strerror(errno));
break;
}
}
close(fd);
exit(0);
}