1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
| #include <stdio.h>
#include <sys/stat.h>
#include <malloc.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
int i, j, k = 1;
char a[] = "";
for (i = 0; i < argc && argv[i + 1] != NULL; i++)
{
printf("\nargv[%d] is: %s\n", i + 1, argv[i + 1]);
fp = fopen(argv[i + 1], "r");
k = getFSSystemCall(argv[i + 1]);
printf("FileSize:%d\n", k);
// 请求动态内存
char *a_p = a;
a_p = (char *)malloc(sizeof(char) * k);
if (a_p == NULL)
{
puts("malloc null");
return 1;
}
if (fp == NULL)
{
printf("\e[1;31mNO SUCH FILE\e[0m\n\n");
printInfo();
return 1;
}
//size_t fread ( void *buffer, size_t size, size_t count, FILE *stream) ;
//返回值:实际读取的元素个数.如果返回值与count不相同,则可能文件结尾或发生错误.
for (j = 0; fread(a_p + j, sizeof(char), 1, fp) == 1; j++)
{
fseek(fp, j + 1, 0);
printf("%c", *(a_p + j));
}
fclose(fp);
// 释放
free(a_p);
}
return 0;
}
int getFileSize(FILE * fps)
{
fseek(fps, 0L, SEEK_END);
int size = ftell(fps);
// <stdio.h> long ftell( FILE *s )
// 返回s当前的文件位置
// 错误返回-1
return size;
}
// include <sys/stat.h>
int getFSSystemCall(char *strFileName)
{
struct stat temp;
stat(strFileName, &temp);
return temp.st_size;
}
printInfo()
{
printf("\e[1;33m1cat:simple cat Ver:0.0.3 20130528\e[0m\n");
printf("\e[1;33mAuthor:wuwenjie.tk\e[0m\n");
}
|