码迷,mamicode.com
首页 > 其他好文 > 详细

fgets、fgetc读取文件

时间:2016-07-08 21:34:28      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 using namespace std;
 5 
 6 int main ()
 7 {
 8     FILE *pf = fopen("D:\\input.in","r");
 9     char str[1000];
10     fgets(str, 1000, pf);
11     int len = strlen(str);
12     for (int i = 0; i < len; i++)
13     {
14         putchar(str[i]);
15     }
16     fclose(pf);
17     return 0;
18 }

需要注意的是,路径的斜杠要转义,即双斜杠,否则打开文件失败。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3  
 4 int main()
 5 {
 6     FILE *pf = fopen("a.txt", "r");
 7     if(pf == NULL) {
 8         printf("open a.txt file failed!\n");
 9         exit(0);
10     }
11      
12     FILE *pf2 = fopen("b.txt", "w");
13     if(pf2 == NULL) {
14         printf("open b.txt file failed!\n");
15         fclose(pf);
16         exit(0);
17     }
18      
19     char ch;
20     while(!feof(pf)) {
21         ch = fgetc(pf);
22         putchar(ch);
23         fputc(ch, pf2);   
24     }
25  
26     fclose(pf2);
27     fclose(pf);
28  
29     return 0;
30 }

 

fgets、fgetc读取文件

标签:

原文地址:http://www.cnblogs.com/jiu0821/p/5654436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!