c讀取文字文件

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   使用   ar   檔案   div   

 想數一下文字文件一共有多少行,寫了個小程式

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讀取文字文件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.