long blogs

进一步有进一步惊喜


  • Home
  • Archive
  • Tags
  •  

© 2025 long

Theme Typography by Makito

Proudly published with Hexo

C-IO

Posted at 2020-11-18 C linux 

方法一

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
void readFile(char *filename){
FILE *fp;
fp = fopen(filename,"r");
if (NULL == fp){
printf("open %s error\n",filename);
return;
}
uint8_t *str = NULL;
size_t calloc_size = 1024;
str = (uint8_t *)calloc(1,1024);
int c = getc(fp);
int index = 0;
while (c != EOF){
printf("%d\t",c);
if (index != 0 && index % 16 == 0){
printf("\n");
}
if (index > calloc_size){
uint8_t *tmp = calloc(1, calloc_size * 2);
memcpy(tmp, str, calloc_size);
calloc_size *= 2;
free(str);
str = tmp;
}
*(str + index) = c;
index ++;
c = getc(fp);
}
printf("\n");
printf("readsize: %d bytes\n", index);
printf("callocsize: %ld bytes\n", calloc_size);
printf("\n");
printf("%s\n", str);
free(str);
fclose(fp);
}

方法二

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
void readFile2(char *filepath) {
struct stat statbuf;
stat(filepath, &statbuf);
long size = statbuf.st_size;
FILE *fp;
fp = fopen(filepath, "r");
if (NULL == fp) {
printf("open %s error\n", filepath);
return;
}
uint8_t *str = NULL;
str = (uint8_t *)calloc(1, size);
if (NULL == str){
printf("calloc failed.\n");
return;
}
int c = getc(fp);
size_t index = 0;
while (c != EOF){
*(str + index) = c;
index ++;
c = getc(fp);
}
printf("%s\n",str);
free(str);
fclose(fp);
}

方法3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void readFile3(const char *filepath) {
FILE *fp;
fp = fopen(filepath, "r");
if (NULL == fp) {
printf("open %s failed.\n", filepath);
return;
}
struct stat statbuf;
stat(filepath, &statbuf);
long size = statbuf.st_size;
char *buf = calloc(1, size + 1);
size_t read_size = 0;
char *res = fgets(buf, size + 1,fp);
while (res != NULL) {
read_size += strlen(res);
res = fgets(buf + read_size, (size + 1 - strlen(res)), fp );

}
printf("%s\n", buf);
printf("filesize: %ld\n", size);
printf("readsize: %ld\n", read_size);
}

写入文件

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
void writeFile(char *filepath, const uint8_t *data, size_t data_len) {
FILE *fp;
fp = fopen(filepath, "w");
if (NULL == fp){
printf("open %s failed.\n", filepath);
return;
}
size_t input_size = 0;
while (input_size < data_len) {
int c = putc(*(data + input_size), fp);
if (c == EOF) {
printf("write finish...\n");
break;
}
input_size++;
}
fclose(fp);
}

void writeStringToFile(const char *filepath, const char *data) {
FILE *fp;
fp = fopen(filepath, "w");
if (NULL == fp) {
printf("open %s failed.\n", filepath);
return;
}
int ret = fputs(data, fp);
if(EOF == ret) {
printf("write string %s failed.\n", data);
fclose(fp);
return;
}
fclose(fp);
}

void writeStringToFile2(const char *filepath, const char *data) {
FILE *fp;
fp = fopen(filepath, "w");
if (NULL == fp) {
printf("open %s failed.\n", filepath);
return;
}
int ret = fprintf(fp, "%s", data);
if (EOF == ret) {
printf("write %s failed.\n", data);
fclose(fp);
return;
}
fclose(fp);
}

结构体,数组读写

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
typedef struct data_s {
int type;
char *description;
int num[10];
char str[1000];
struct data_s *next;
} data_t;

void writeStruct2File(const char *filename, const data_t *data, const size_t data_size){
FILE *fp;
fp = fopen(filename, "w");
if (NULL == fp) {
printf("open %s failed.\n", filename);
return;
}
size_t write_size = fwrite(data, data_size, 1, fp);
printf("write_size: %ld\n", write_size);
fclose(fp);
}

void readStructfromFile(const char *filename, data_t *data, size_t data_size) {
FILE *fp;
fp = fopen(filename, "r");
size_t read_size = fread(data, data_size, 1, fp);
printf("read_size: %ld\n",read_size);
fclose(fp);
}
int main() {
data_t data;

data_t *data2;
data2 = calloc(1, sizeof(data_t));

memset(&data, 0, sizeof(data));
strcpy(data2->str, "NEW DATA STRUCT");


data.type = 1000;
data.description = calloc(1, 30);
strcpy(data.description, "Hello World...");
strcpy(data.str,"this is data.str");
int num[10] = {1,2,3,4,5,6,7,8,8,9};
memcpy(data.num, num, sizeof(num));
data.next = data2;

writeStruct2File("Hello.bin",&data, sizeof(data) + 30);
free(data.description);

memset(&data, 0, sizeof(data));
readStructfromFile("Hello.bin", &data, sizeof(data) + 30);
printf("read data:::\n");
printf("data->type:%d\n", data.type);
printf("data->description:%s\n", data.description);
printf("data->num::\n");
for (int i = 0;i < 10;i ++) {
printf("data->num[%d]:%d\n", i, data.num[i]);
}
printf("data->str:%s\n",data.str);
printf("data->next: %p\n",data.next);
if (NULL != data.next){
printf("data->next->str:%s\n", data.next->str);
}
printf("finish!!!!");
printf("data struct size:%ld", sizeof(data_t));
return 0;
}

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
write_size: 1
read_size: 1
read data:::
data->type:1000
data->description:
data->num::
data->num[0]:1
data->num[1]:2
data->num[2]:3
data->num[3]:4
data->num[4]:5
data->num[5]:6
data->num[6]:7
data->num[7]:8
data->num[8]:8
data->num[9]:9
data->str:this is data.str
data->next: 0x55fea235c260
data->next->str:NEW DATA STRUCT
finish!!!!data struct size:1064

生成的文件大小为:1094字节, 为sizeof(data_t)+ 30;使用calloc的申请的内存块不会被存储.

Share 

 Previous post: C-printf显示字符颜色 Next post: linux安装微信 

© 2025 long

Theme Typography by Makito

Proudly published with Hexo