If you don't know the lengths of the lines in advance, there is no shortcut for finding the place of the \n. That is, unless you use a readily implemented function for splitting the lines, but that would probably just do the same thing somewhere down the line.
That is, yes, you have to loop through the chars :)
You can replace getc(file) with read(file,&c,1): just write a function which accepts the file as an argument and return a character. Read the character into a local ariable and return it.
Then implement a getline(file): Make a buffer, starting reading characters into it one at a time, stop when you come to '\n' (what will you do if you run out of room?), and return a pointer to the buffer (How can you allocate the buffer for this to work?).
Use you getline to read one line at a time, then write the line number and the line.
Loop until you run out of file...
To make fewer system calls:
Read the whole file into a large buffer in one go---that's pretty easy if you have the file size---and walk it one character at a time to find the line starts and ends (use two pointers).
You don't actually need to know the location of the '\n' to number the lines. You just print out the line number (starting with 1), and read characters until you hit a newline '\n'. Copy each character back to stdout with write(), and if the character is a newline, increment the count of current lines and print that out, too.
You will have to search the buffer for \n, there is no way around it. Don't worry about the performance, the progam will spend most of the time in read and write.
Also, make sure you make the buffer sufficiently large. Don't call read(file,&c,1) as it will be incredibly slow.
The only way to find a '\n' is to search for it. If you can't use strchr(3), you've got to iterate over the buffer to find what you're looking for (read: implement it).
发布评论
评论(5)
如果您事先不知道行的长度,则没有找到 \n 位置的捷径。 也就是说,除非您使用易于实现的函数来分割线,但这可能只是在某处做同样的事情。
也就是说,是的,您必须循环遍历字符:)
If you don't know the lengths of the lines in advance, there is no shortcut for finding the place of the \n. That is, unless you use a readily implemented function for splitting the lines, but that would probably just do the same thing somewhere down the line.
That is, yes, you have to loop through the chars :)
一个非常粗略的轮廓:
您可以将
getc(file)
替换为read(file,&c,1)
:只需编写一个将文件接受为一个参数并返回一个字符。 将字符读入本地变量并返回。然后实现一个
getline(file)
:创建一个缓冲区,开始一次一个地向其中读取字符,当遇到'\n'
时停止(什么如果你的空间用完了,你会怎么做?),并返回一个指向缓冲区的指针(如何分配缓冲区以使其工作?)。使用
getline
一次读取一行,然后写入行号和行。循环直到文件用完...
减少系统调用:
一次性将整个文件读入一个大缓冲区——这非常简单如果您有文件大小——并一次遍历一个字符以找到行的开头和结尾(使用两个指针)。
A very rough outline:
You can replace
getc(file)
withread(file,&c,1)
: just write a function which accepts the file as an argument and return a character. Read the character into a local ariable and return it.Then implement a
getline(file)
: Make a buffer, starting reading characters into it one at a time, stop when you come to'\n'
(what will you do if you run out of room?), and return a pointer to the buffer (How can you allocate the buffer for this to work?).Use you
getline
to read one line at a time, then write the line number and the line.Loop until you run out of file...
To make fewer system calls:
Read the whole file into a large buffer in one go---that's pretty easy if you have the file size---and walk it one character at a time to find the line starts and ends (use two pointers).
您实际上不需要知道
'\n'
的位置来对行进行编号。 您只需打印出行号(从 1 开始),然后读取字符,直到遇到换行符'\n
'。 使用 write() 将每个字符复制回 stdout,如果该字符是换行符,则增加当前行数并将其打印出来。You don't actually need to know the location of the
'\n'
to number the lines. You just print out the line number (starting with 1), and read characters until you hit a newline'\n
'. Copy each character back to stdout withwrite()
, and if the character is a newline, increment the count of current lines and print that out, too.您必须在缓冲区中搜索
\n
,没有办法绕过它。 不用担心性能,程序大部分时间都花在read
和write
上。另外,请确保缓冲区足够大。 不要调用
read(file,&c,1)
因为它会非常慢。You will have to search the buffer for
\n
, there is no way around it. Don't worry about the performance, the progam will spend most of the time inread
andwrite
.Also, make sure you make the buffer sufficiently large. Don't call
read(file,&c,1)
as it will be incredibly slow.找到“\n”的唯一方法是搜索它。 如果您无法使用
strchr(3)
,则必须迭代缓冲区以找到您要查找的内容(阅读:实现它)。The only way to find a '\n' is to search for it. If you can't use
strchr(3)
, you've got to iterate over the buffer to find what you're looking for (read: implement it).