如何阅读在C文件中的行由行? [英] How to read a file line-by-line in C?
问题描述
好吧,我是初学者,当谈到C,如此忍受我。
Okay, I'm a beginner when it comes to C, so bear with me.
下面的问题:我有多达100个IP地址,1每行的文本文件。我需要阅读每一个地址,一个字符串,进入所谓的名单的数组。首先,我假设,排行榜将需要一个二维字符数组。每个IP地址的长度为11个字符,12如果包含'\\ 0',所以我宣布名单如下:
Here's the problem: I have a text file with up to 100 IP addresses, 1 per line. I need to read each address, as a string, into an array called "list". First, I'm assuming that "list" will need to be a two-dimensional char array. Each IP address is 11 characters in length, 12 if you include '\0', so I declared list as follows:
字符名单[100] [12];
接下来,我尝试使用与fgets来读取数据流:
Next, I attempt to use fgets to read the stream:
for (i = 0; i < 100; i++)
{
if (feof(stream))
break;
for (j = 0; j < 12; j++)
fgets(&list[i][j], 12, stream);
count++;
}
要检查是否字符串被正确读取,我试图将它们输出:
To check to see if the strings were read properly, I attempt to output them:
for (i = 0; i < 5; i++)
{
for (j = 0; j < 11; j++)
printf("%c", list[i][j]);
printf("\n");
}
运行程序后,很清楚什么是错的。作为一个初学者,我不知道是什么,但我猜我在读取文件错误。有没有错误。它编译,但在打印两行一个陌生的地址。
After running the program, it's clear something is wrong. Being a beginner, I'm not sure what, but I'm guessing I'm reading the file wrong. There are no errors. It compiles, but prints a strange address on two lines.
任何帮助将是AP preciated。
Any help would be appreciated.
编辑:
我更换了与fgets code这一点:
I replaced the fgets code with this:
for (i = 0; i < 100; i++)
{
if (feof(stream))
break;
fgets(list[i], 12, stream);
count++;
}
现在打印五根弦,但他们是从内存中随机的字符。
It now prints five strings, but they are "random" characters from memory.
推荐答案
首先,内容如下:
for (j = 0; j < 12; j++)
fgets(&list[i][j], 12, stream);
您有一个很大的问题就在这里。这是试图读取的字符串的进入每个连续的字符的阵列中
You have a big problem right here. This is attempting to read a string into each successive character in your array.
总而言之,我认为你正在做这个有很多比它需要的更加复杂。想想你的数组为100个字符串,而与fgets
将在时间的字符串工作。这意味着阅读能是这个样子:
All in all, I think you're making this a lot more complex than it needs to be. Think of your array as 100 strings, and fgets
will work with a string at a time. That means reading can look something like this:
for (i=0; i<100 && fgets(list[i], 11, string); i++)
;
还有另外一个小的细节处理:与fgets()
通常保持在每行的末尾新行。因此,您可能需要留有余地13个字符(11地址,1新线,1 NUL终止),否则你可能需要将数据读入临时缓冲区,并将其复制到你的列表
您已经剥去后才能关闭新线。
There is one other minor detail to deal with: fgets()
normally retains the new-line at the end of each line. As such, you may need to leave room for 13 characters (11 for address, 1 for new-line, 1 for NUL terminator), or else you may want to read the data into a temporary buffer, and copy it to your list
only after you've stripped off the new-line.
在你目前的code打印的琴弦,你在一次工作一个字符,它可以工作,但不必要的困难。有几个人使用%s的printf的转换,这是罚款本身建议。去用它,但是,你必须简化你的索引了一下。打印第6个地址会是这个样子:
In your current code for printing the strings, you're working one character at a time, which can work, but is unnecessarily difficult. Several people have suggested using the %s printf conversion, which is fine in itself. To go with it, however, you have to simplify your indexing a bit. Printing the first six addresses would look something like this:
for (i=0; i<6; i++)
printf("%s", list[i]);
这篇关于如何阅读在C文件中的行由行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!