#include <stdio.h>
2 #include "lines.h" 3 #include <stdlib.h> 4 5 char *readLines(const char *file, size_t *longest) //file 文件名 6 { 7 FILE *fp; 8 char *p; 9 char ch; 10 int log=0,i=0,lg=1,j=1; //最長 i表示個數 j表示第幾行 lg表示第幾行 11 if((fp=fopen(file,"r"))==NULL) // open file 12 { 13 *longest=0; 14 return NULL; 15 } 16 while((ch=fgetc(fp))!=EOF) //判斷文件是否結束 17 { 18 if(ch=='/n') 19 { 20 if(log<i) 21 { 22 log=i; 23 lg=j; 24 } 25 j++; 26 i=0; 27 } 28 else 29 { 30 i++; 31 } 32 } 33 //目前確定了長度 和 第幾行 34 //printf("log=%d lg=%d/n",log,lg); 35 if(log==0) 36 { 37 *longest=0; 38 p=NULL; 39 } 40 else 41 { 42 p=(char *)malloc(sizeof(char)*log); 43 *longest=log; 44 rewind(fp); 45 i=0; 46 while((ch=fgetc(fp))!=EOF && log>0) 47 { 48 if(lg==1) 49 { 50 p[i++]=ch; 51 log--; 52 } 53 else 54 { 55 if(ch=='/n') 56 { 57 lg--; 58 } 59 } 60 } 61 } 62 fclose(fp); 63 return p; 64 } 65 /* 66 int main() 67 { 68 size_t b=0; 69 char *e; 70 int i; 71 e=readLines("/root/kt/unit5/U5E1/jjk.txt",&b); 72 for(i=0;i<b;i++) 73 putchar(*(e+i)); 74 printf("b=%u",b); 75 return 0; 76 } 77 */LINUX C實現讀取一個文本文件並返回其中最長的行的內容
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
Visual Studio中的四款代碼格式化工具
追逐時光
2024-05-13 14:21:59
幹了 2 年多 Java 外包,終於脫離了!
Java技術棧
2024-05-13 14:21:19
mysql 存json數據會自動亂序的解決方案
張博的博客
2024-05-13 14:19:49
【Python】保存gym截圖
Dsp Tian
2024-05-13 14:11:08
win10 22H2
ChuckLu
2024-05-13 14:06:58
【譯】使用 GitHub Copilot 作爲你的編碼 GPS
MeteorSeed
2024-05-13 14:06:38
兩個有趣的AI項目
咖啡機(K.F.J)
2024-05-13 14:05:57
從油猴腳本管理器的角度審視Chrome擴展
WindrunnerMax
2024-05-13 14:05:17
一文學會 Kubernetes Pod 的生命週期管理(轉載)
PowerCoder
2024-05-13 14:03:07
python: Binary Number
def binary_to_decimal(binary_number): """ :param binary_number: :return: """ decimal = 0 for
®Geovin Du Dream Park™
2024-05-13 14:00:36
ChatGPT助您打造個性化簡歷:個人品牌升級攻略
霍格沃茲測試學院
2024-05-13 13:52:55