看板Programming
标 题Re: 用C++如何很快知道一个档案有几行
发信站白色情迷 (Mon Feb 23 18:23:48 2009)
转信站ptt!news.ntu!mars.scc.ntnu!snowBBS
※ 引述《[email protected] (陈扬和)》之铭言:
> 真是感谢版友的指导..
> 其实不太懂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
1F:推 sorryChen:非常感谢大家的指导 128.125.87.33 02/25 14:06