标签:
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 }
标签:
原文地址:http://www.cnblogs.com/jiu0821/p/5654436.html