Re: 用C++如何很快知道一個檔案有幾行
※ 引述《sorryChen.bbs@ptt.cc (陳揚和)》之銘言:
> 真是感謝版友的指導..
> 其實不太懂I/O的機制..如果很低階I/O 會每讀一個字母就讀一次嗎
> 還是會自動Buffer, 因為我之前的I/O太慢, 還自己寫了一個buffer的class
> 做batch read write, 但copy 到 buffer在一個個看'\n'會比 fgetc 然後檢查慢嗎
> 另外 有些OS有提供AP來判斷檔案大小, 比如說我們 ls就時可看到
> 那個不應該是去數出來而是os 記得的大小吧 但是換行就沒記 只有檔案大小有記
> 應該是這樣吧
> ※ 引述《sorryChen (陳揚和)》之銘言:
> : 一定要讀過每個字元然後看有幾個\n嗎, 因為檔很大時還滿慢的
> : call system call wc 應該就更慢了
> : 要怎麼知道檔案有多大我也不確定 這個file system 應該有紀錄
> : 不用一個個字元數.. 但有os independent的函數可回傳檔案大小嗎
如果只是要知道檔案的大小,可以用 stat() 檔案的相關資訊,
檔案大小記錄在結構的 .st_size 中。
NAME
stat, lstat, fstat -- get file status
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
int
stat(const char *path, struct stat *sb);
int
lstat(const char *path, struct stat *sb);
int
fstat(int fd, struct stat *sb);
DESCRIPTION
The stat() system call obtains information about the file pointed to by
path. Read, write or execute permission of the named file is not
required, but all directories listed in the path name leading to the file
must be searchable.
--
╭╢師大資訊。白色情迷╟┬─ ⊙ Origin : snow.ice.ntnu.edu.tw
├╨→ 140.122.77.49←╜╰─ ☆ From : pc8.csie.ntnu.edu.tw
推
02/25 14:06, , 1F
02/25 14:06, 1F
討論串 (同標題文章)