首页 > 其他 > 详细

c读取文本文档

时间:2014-09-17 10:14:22      阅读:367      评论:0      收藏:0      [点我收藏+]

 想数一下文本文档一共有多少行,写了个小程序

1.用fopen()以只读方式打开文件

2.用fgetc()获取文件流中的字符内容

3.如果字符内容为‘\n‘换行符,count++

最后输出count的值

 1 #include <iostream>
 2 #include <string>
 3 #include <stdlib.h>
 4 #include <stdio.h>
 5 
 6 
 7 using namespace std;
 8 
 9 //void swap(int, int);
10 
11 int main(){
12     FILE *fp = NULL;
13     char a;
14     fp = fopen("C:\\Documents and Settings\\Administrator\\桌面\\isotree.txt", "r");
15     int count = 0;
16 
17     if(NULL != fp){
18         do{
19             a = fgetc(fp);
20     //        printf("%c", a);
21             if(a == \n)
22             {
23                 count ++;
24             }
25         }while(a != EOF);
26     }
27     
28     fclose(fp);
29     
30     cout<<"count line:"<<count<<endl;
31 
32     return 0;
33 }

这里主要是打开文件,读取文件内容的方法使用

Ps:路径的时候注意使用转义字符\

c读取文本文档

原文:http://www.cnblogs.com/luckygxf/p/3976386.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!