c读取文本文档

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

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

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

3.如果字符内容为' '换行符,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 == '
')
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:路径的时候注意使用转义字符

原文地址:https://www.cnblogs.com/luckygxf/p/3976386.html