如何计算文件中每一行的字节数? [英] How can I calculate the number of bytes of every line in file?

查看:323
本文介绍了如何计算文件中每一行的字节数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友..

我有一个包含行号的文件..

我的问题是如何在c语言或其他函数中使用函数fgets你知道..

我想计算另一个文件中每一行的字节数

这个例子

逐行读取file1那么我的file2将是:

234:第1行的字节数

321:数字字节......

...

感谢您的帮助



我尝试过:



File1 = fopen(``fl1``,``rb``);

File2 = open(fl2,wb);

Int line [128] ;

Int x;

Int number = 0;

while(fgets(行数,行数,file1)

{

Sscanf(行,& x);

}

Number_line ++;

Hi friends ..
I hAve a file contains number of line ..
My question is how I can use the function fgets in c language or another function maybe you know ..
I want to calculate the number of bytes of every line in another file
This example
Read line by line the file1 then my file2 will be :
234 : number bytes of line 1
321 : number bytes ...
...
Thanks for your helps

What I have tried:

File1 = fopen (``fl1``,``rb``);
File2 = open (fl2,wb);
Int line [128];
Int x ;
Int number=0;
While ( fgets ( line , size of line , file1)
{
Sscanf ( line , &x );
}
Number_line ++;

推荐答案

有两种方法:

1)使用fgets读取每一行并写出长度。这是微不足道的 - 它是一个标准的空终止字符串,所以将字符计数到空,你可以打印它。

2)将整个文件读入内存,并进行扫描。对于每一行,保留一个字符数的计数器并查找换行符代码(取决于您的系统可能是'\ r','\ n'或'\ n''后跟'\ n )。当你找到它时,打印字符数,并重置计数。



两者都不是特别困难,这只是个人偏好的问题。

但是......这是你的作业,所以代码完全取决于你!
There are two ways:
1) Read each line using fgets and write out the length. That's trivial - it's a standard null terminated string so count the characters up to the null and you can print it.
2) Read the whole file into memory, and scan it. For each line, keep a counter of the number of characters and look for the newline code (depending on your system that might be '\r', '\n', or '\r' followed by '\n'). When you find it, print the number of characters, and reset the count.

Neither is particularly difficult, it's just a question of personal preference.
But ... this is your homework, so code is all down to you!


这篇关于如何计算文件中每一行的字节数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆