demo_3_10


  1 #define _CRT_SECURE_NO_WARNINGS 1
  2 #include 
  3 #include 
  4 #include 
  5 #include 
  6 
  7 struct S
  8 {
  9     int n;
 10     float score;
 11     char arr[10];
 12 };
 13 int main()
 14 {
 15     struct S s = { 100, 3.14f, "bit" };
 16     FILE* pf = fopen("test.txt", "w");
 17     if (pf == NULL)
 18     {
 19         return 0;
 20     }
 21     //格式化的形式写文件
 22     fprintf(pf, "%d %f %s", s.n, s.score, s.arr);
 23 
 24     //关闭文件
 25     fclose(pf);
 26     pf = NULL;
 27     system("pause");
 28     return 0;
 29 }
 30 
 31 
 32 struct S
 33 {
 34     int n;
 35     float score;
 36     char arr[10];
 37 };
 38 int main()
 39 {
 40     struct S s = { 100, 3.14f, "bit" };
 41     FILE* pf = fopen("test.txt", "w");
 42     if (pf == NULL)
 43     {
 44         return 0;
 45     }
 46     //格式化输入数据
 47     //scanf("%d %f %s", &(s.n), &(s.score), &(s.arr));
 48     fscanf(pf, "%d %f %s", s.n,s.score,s.arr);
 49     //关闭文件
 50     fclose(pf);
 51     pf = NULL;
 52     system("pause");
 53     return 0;
 54 }
 55 
 56 
 57 struct S
 58 {
 59     int n;
 60     float score;
 61     char arr[10];
 62 };
 63 int main()
 64 {
 65     struct S s = { 0 };
 66     fscanf(stdin, "%d %f %s", &(s.n), &(s.score), s.arr);
 67     fprintf(stdout, "%d %f %s", s.n, s.score, s.arr);
 68     system("pause");
 69     return 0;
 70 }
 71 
 72 
 73 struct S
 74 {
 75     int n;
 76     float score;
 77     char arr[10];
 78 };
 79 int main()
 80 {
 81     struct S s = { 100, 3.14f, "abcdefg" };
 82     struct S tmp = { 0 };
 83     char buf[1024] = { 0 };
 84     //把格式化的数据转换成字符串存储到buf
 85     sprintf(buf, "%d %f %s", s.n.s.score, s.arr);
 86     //从buf中读取格式化的数据到字符串中
 87     sscanf(buf, "%d %f %s", &(tmp.n), &(tmp.score), tmp.arr);
 88     printf("%d %f %s\n", tmp.n, tmp.score, tmp.arr);
 89     system("pause");
 90     return 04;
 91 }
 92 
 93 
 94 struct S
 95 {
 96     char name[20];
 97     int age;
 98     double score;
 99 };
100 int main()
101 {
102     struct S s = { "张三", 20, 80.5 };
103 
104     FILE* pf = fopen("test.txt", "w");
105     if (pf == NULL)
106     {
107         return 0;
108     }
109     //二进制文件写文件
110     fwrite(&s, sizeof(struct S), 1, pf);
111     //关闭文件
112     fclose(pf);
113     pf = NULL;
114     system("pause");
115     return 0;
116 }
117 
118 struct S
119 {
120     char name[20];
121     int age;
122     double score;
123 };
124 int main()
125 {
126     struct S s = { "张三", 20, 80.5 };
127     struct S tmp = { 0 };
128 
129     FILE* pf = fopen("test.txt", "rb");
130     if (pf == NULL)
131     {
132         return 0;
133     }
134     //二进制文件读文件
135     fread(&tmp, sizeof(struct S), 1, pf);
136     printf("%s %d %lf\n", tmp.name, tmp.score, tmp.age);
137 
138     //关闭文件
139     fclose(pf);
140     pf = NULL;
141     system("pause");
142     return 0;
143 }
144 
145 
146 int main()
147 {
148     FILE* pf = fopen("test.txt", "r");
149     if (pf == NULL)
150     {
151         return 0;
152     }
153     //1.定位文件指针
154     fseek(pf, 2, SEEK_CUR);//从当前位置偏移2个字符
155     //fseek(pf, 2, SEEK_END);//从末尾位置偏移2个字符
156     //fseek(pf, 2, SEEK_SET);//从开始位置偏移2个字符
157     //2.读取文件
158     int ch = fgetc(pf);
159     printf("%c\n", ch);
160     //关闭文件
161     fclose(pf);
162     pf = NULL;
163     system("pause");
164     return 0;
165 }
166 
167 int main()
168 {
169     FILE* pf = fopen("test.txt", "r");
170     if (pf == NULL)
171     {
172         return 0;
173     }
174     //读取一个
175     fgetc(pf);
176     //1.定义文件指针
177     int pos = ftell(pf);
178     printf("%d\n", pos);
179     
180     //关闭文件
181     fclose(pf);
182     pf = NULL;
183     system("pause");
184     return 0;
185 }
186 
187 //rewind - 让文件指针的位置回到文件的起始位置
188 int main()
189 {
190     FILE* pf = fopen("test.txt", "r");
191     if (pf == NULL)
192     {
193         return 0;
194     }
195     //定位文件指针
196     //fseek(pf,-2,SEEK_END);
197     int ch = fgetc(pf);
198     printf("%c\n", ch);
199     rewind(pf);
200     fgetc(pf);
201     fclose(pf);
202     pf = NULL;
203     system("pause");
204     return 0;
205 }
206 
207 int main()
208 {
209     //EOF - end of file - 文件结束标志
210     feof();
211     FILE* pf = fopen("test.txt", "r");
212     if (pf == NULL)
213     {
214         return 0;
215     }
216     int ch = fgetc(pf);
217     printf("%d\n", ch);//-1
218     fclose(pf);
219     pf = NULL;
220     system("pause");
221     return 0;
222 }
223 
224 int main()
225 {
226     //strerror - 把错误码对应的错误信息的字符串地址返回
227     printf("%s\n",strerror(errno));
228     //perror - 输出对应的错误码并提示找不到的文件
229     FILE* pf = fopen("test2.txt", "r");
230     if (pf == NULL)
231     {
232         perror("open file test2.txt");
233         return 0;
234     }
235     //读文件
236 
237     //关闭文件
238     fclose(pf);
239     pf = NULL;
240     system("pause");
241     return 0;
242 }
243 
244 int main()
245 {
246     //strerror - 把错误码对应的错误信息的字符串地址返回
247     printf("%s\n", strerror(errno));
248     //perror - 输出对应的错误码并提示找不到的文件
249     FILE* pf = fopen("test2.txt", "r");
250     if (pf == NULL)
251     {
252         perror("open file test2.txt");
253         return 0;
254     }
255     //读文件
256     int ch = 0;
257     while ((ch=fgetc(pf))!=EOF)
258     {
259         putchar(ch);
260     }
261     if (ferror(pf))
262     {
263         printf("error\n");
264     }
265     else if (feof(pf)){
266         printf("end of file\n");
267     }
268     //关闭文件
269     fclose(pf);
270     pf = NULL;
271     system("pause");
272     return 0;
273 }
274 
275 int main()
276 {
277     int arr[10] = { 0 };
278     int i;
279     for (i = 0; i < 10; i++)
280     {
281         arr[i] = i;
282     }
283     for (i = 0; i < 10; i++)
284     {
285         printf("%d ", arr[i]);
286     }
287 
288     system("pause");
289     return 0;
290 }
291 
292 
293 int main()
294 {
295     //printf("%s\n", __FILE__);//代码所在的地址
296     //printf("%d\n", __LINE__);//代码所在的行号
297     //printf("%s\n", __DATE__);//代码所在的日期
298     //printf("%s\n", __TIME__);//代码所在的时间
299     //写日志文件
300     int i;
301     int arr[10] = { 0 };
302     FILE* pf = fopen("log.txt", "w");
303     for (i = 0; i < 10; i++)
304     {
305         arr[i] = i;
306         fprintf(pf, "file:%s line:%d date:%s time:%s i=%d\n", __FILE__, __LINE__, __DATE__, __TIME__, i);
307         printf("%s\n", __FUNCTION__);//打印函数的预定义符号
308     }
309     fclose(pf);
310     pf = NULL;
311     for (i = 0; i < 10; i++)
312     {
313         printf("%d ", arr[i]);
314     }
315     system("pause");
316     return 0;
317 }
318 
319 //int main()
320 //{
321 //    printf("%d\n", __STDC__);
322 //    system("pause");
323 //    return 0;
324 //}
325 
326 #define MAX 100
327 #define STR "hehe"
328 #define reg register
329 #define do_forever for(;;)
330 int main()
331 {
332     do_forever;
333     reg int a;
334     register int a;
335     int max = MAX;
336     printf("%d\n", max);
337     printf("%s\n", STR);
338     system("pause");
339     return 0;
340 }
341 
342 int main()
343 {
344     int a = 10;
345     int b = a + 1;
346     int b = ++a;
347     system("pause");
348     return 0;
349 }
350 
351 
352 #define MAX(X,Y) ((X)>(Y)?(X):(Y))
353 int main()
354 {
355     int a = 10;
356     int b = 11;
357     int max = MAX(a++, b++);//带有副作用
358     //等价于int max = MAX((a++) > (b++) ? (a++) : (b++));
359     printf("%d\n", max);
360     printf("%d\n", a);
361     printf("%d\n", b);
362     system("pause");
363     return 0;
364 }
365 
366 #define MAX(X,Y) ((X)>(Y)?(X):(Y))
367 int main()
368 {
369     int a = 10;
370     int b = 11;
371     int max = MAX(a+3, b+3);
372     printf("%d\n", max);
373     printf("%d\n", a);
374     printf("%d\n", b);
375     system("pause");
376     return 0;
377 }
378 
379 
380 //函数的形式
381 int Max(int x, int y)
382 {
383     return (x > y ? x : y);
384 }
385 //宏的形式
386 #define MAX(X,Y) ((X)>(Y)?(X):(Y))
387 int main()
388 {
389     int a = 10;
390     int b = 20;
391     //函数在调用的时候
392     //会有函数调用和返回的开销
393     //函数调用需要一定的时间,效率低一些
394     int max = Max(a, b);
395     //宏-在预处理阶段就完成了替换,没有函数的调用和返回的开销,效率更高一些
396     max = MAX(a, b);
397     printf("%d\n", max);
398     printf("%d\n", max);
399     system("pause");
400     return 0;
401 }
402 
403 
404 #define TEST(x,y) printf("test\n")
405 int main()
406 {
407     TEST();
408     TEST();
409     TEST();
410     system("pause");
411     return 0;
412 }
413 
414 #define SIZEOF(type) sizeof(type)
415 int main()
416 {
417     //宏可以传类型
418     int ret = SIZEOF(int);
419     int ret = sizeof(int);
420     printf("%d\n", ret);
421     system("pause");
422     return 0;
423 }
424 
425 #define MALLOC(num,type) (type*)malloc(num*sizeof(type))
426 int main()
427 {
428     int* p = (int*)malloc(10 * sizeof(int));
429     int* p = MALLOC(10, int);
430 }
431 
432 
433 #define MAX 100
434 int main()
435 {
436     printf("MAX=%d\n", MAX);
437 #undef MAX //移除一个宏定义
438     system("pause");
439     return 0;
440 }
441 
442 
443 #define DEBUG
444 int main()
445 {
446     int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
447     int i = 0;
448     for (i = 0; i < 10; i++)
449     {
450         arr[i] = 0;
451 #ifdef DEBUG //某个条件满足就编译,不满足就不编译
452         printf("%d ", arr[i]);
453 #endif 
454     }
455 }
456 
457 
458 //单分支
459 int main()
460 {
461     int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
462     int i = 0;
463     for (i = 0; i < 10; i++)
464     {
465         arr[i] = 0;
466 #if 1-1
467         printf("%d ", arr[i]);
468 #endif
469     }
470 }
471 
472 
473 //多分支
474 int main()
475 {
476 #if 1==1
477     printf("haha\n");
478 #elif 2==1
479     printf("hehe\n");
480 #else 
481     printf("heihei\n");
482 #endif
483     return 0;
484 }
485 
486 
487 #define DEBUG 0
488 int main()
489 {
490 #if defined(DEBUG) //定义了就打印hehe,否则什么也不打印
491     printf("hehe\n");
492 #endif
493     return 0;
494 }
495 //等价于
496 #define DEBUG 0
497 int main()
498 {
499 #ifdef DEBUG //定义了就打印hehe,否则什么也不打印
500     printf("hehe\n");
501 #endif
502     return 0;
503 }
504 
505 int mian()
506 {
507 #if !defined(DEBUG) //没有定义就打印,否则就不打印
508     printf("hehe\n");
509 #endif
510     return 0;
511 }
512 
513 int main()
514 {
515 #ifdef DEBUG //没有定义就打印,否则就不打印
516     printf("hehe\n");
517 #endif
518     return 0;
519 }
520 
521 
522 
523 struct S
524 {
525     char c1;
526     int a;
527     char c2;
528 };
529 #define OFFSETOF(struct_name,member_name) (int)&(((struct_name*)0)->member_name)
530 int main()
531 {
532     struct S s;
533     printf("%d\n", OFFSETOF(struct S, c1));
534     printf("%d\n", OFFSETOF(struct S, a));
535     printf("%d\n", OFFSETOF(struct S, c2));
536     system("pause");
537     return 0;
538 }