使用与fgets来读取C从文件中的字符串 [英] Using fgets to read strings from file in C

查看:219
本文介绍了使用与fgets来读取C从文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从具有在新行每个字符串文件中读取字符串,但是我觉得它读取一个换行符一次,而不是一个字符串,我不知道为什么。如果我要去关于阅读的字符串错误的方法,请大家指正。

  I = 0;
F1 =的fopen(alg.txt,R);
F2 =的fopen(tul.txt,W);
     如果(!的feof(F1)){
     不要{//开始扫描文件
     与fgets(inimene [I] .Enimi,20,F1);
     与fgets(inimene [I] .Pnimi,20,F1);
     与fgets(inimene [I] .Kood,12,F1);
     输出(I =%d个\\ nEnimi =%S \\ nPnimi =%S \\ nKaad =%s的,我,inimene [I] .Enimi,inimene [I] .Pnimi,inimene [I] .Kood);
     我++;}
     而(的feof(F1)!);};
/ *光洁度得到结构* /

的printf的是有没有让我看到了什么读成什么,这里是结果。

  I = 0
Enimi =彼得Pnimi = pupkin沽= 223456iatb I = 1
Enimi =Pnimi =玛莎KAAD = gubkina
设为i = 2
Enimi = 234567iasbPnimi =萨沽= dudkina

正如你所看到的第一个结构被读取后,有一个空白(换行?)onct,然后一切都被转移。我想我可以读取空字符串,以吸收多余的空白,然后什么都不会被转移,但是这并不能帮助我理解这个问题,并避免将来。

编辑1:我知道,它停在一个换行符,但仍然读它。我不知道为什么它不第三串并转移到第四字符串,而不是给第四弦源的第四行中读取它,但它发生一次。
该文件的方式格式如下

 彼得
pupkin
223456iatb
玛莎
gubkina
234567iasb

dudkina
123456iasb


解决方案

如前所述,如果有足够的空间在缓冲区中,然后与fgets()读取数据,包括换行到缓冲器和零终止该行。如果没有足够的空间缓冲跨越换行符来临前,与fgets()复制所能做和空(缓冲区减去一个字节的长度)终止串。该库的简历从阅读,其中与fgets()的下一次迭代不放过。

不要惹超过2个字节长的小缓冲区。

注意获得()删除换行符(但不会保护你免受缓冲区溢出,所以不要使用它)。如果一切按目前的计划,获得()将从C标准的下一个版本中删除;这将是它是从C库(它只是成为一个非标准的 - 或前标准 - 对于滥用附加功能)移除之前很长一段时间。

您code应该检查每个与fgets的()函数调用:

 而(与fgets(inimene [I] .Enimi,20,F1)= 0&安培;!&安培;
       与fgets(inimene [I] .Pnimi,20,F1)= 0&安培;!&安培;
       与fgets(inimene [I] .Kood,12,F1)!= 0)
{
    输出(I =%d个\\ nEnimi =%S \\ nPnimi =%S \\ nKaad =%s的,我,inimene [I] .Enimi,inimene [I] .Pnimi,inimene [I] .Kood);
    我++;
}

有DO的地方/ while循环;他们不是经常使用,但。

I am trying to read strings from a file that has each string on a new line but I think it reads a newline character once instead of a string and I don't know why. If I'm going about reading strings the wrong way please correct me.

    i=0;
F1 = fopen("alg.txt", "r"); 
F2 = fopen("tul.txt", "w"); 
     if(!feof(F1)) {
     do{ //start scanning file
     fgets(inimene[i].Enimi, 20, F1);
     fgets(inimene[i].Pnimi, 20, F1);
     fgets(inimene[i].Kood, 12, F1);
     printf("i=%d\nEnimi=%s\nPnimi=%s\nKaad=%s",i,inimene[i].Enimi,inimene[i].Pnimi,inimene[i].Kood);
     i++;}
     while(!feof(F1));};
/*finish getting structs*/

The printf is there to let me see what was read into what and here is the result

i=0
Enimi=peter

Pnimi=pupkin

Kood=223456iatb i=1
Enimi=

Pnimi=masha

Kaad=gubkina
i=2
Enimi=234567iasb

Pnimi=sasha

Kood=dudkina

As you can see after the first struct is read there is a blank(a newline?) onct and then everything is shifted. I suppose I could read a dummy string to absorb that extra blank and then nothing would be shifted, but that doesn't help me understand the problem and avoid in the future.

Edit 1: I know that it stops at a newline character but still reads it. I'm wondering why it doesn't read it during the third string and transfers to the fourth string instead of giving the fourth string the fourth line of the source but it happens just once. The file is formatted like this by the way

peter 
pupkin 
223456iatb 
masha 
gubkina 
234567iasb 
sasha 
dudkina 
123456iasb 

解决方案

As already stated, if there's enough room in the buffer, then fgets() reads the data including the newline into the buffer and null terminates the line. If there isn't enough room in the buffer before coming across the newline, fgets() copies what it can (the length of the buffer minus one byte) and null terminates the string. The library resumes reading from where fgets() left off on the next iteration.

Don't mess with buffers smaller than 2 bytes long.

Note that gets() removes the newline (but does not protect you from buffer overflows, so do not use it). If things go as currently planned, gets() will be removed from the next version of the C standard; it will be a long time before it is removed from C libraries (it will just become a non-standard - or ex-standard - additional function available for abuse).

Your code should check each of the fgets() function calls:

while (fgets(inimene[i].Enimi, 20, F1) != 0 &&
       fgets(inimene[i].Pnimi, 20, F1) != 0 &&
       fgets(inimene[i].Kood,  12, F1) != 0)
{
    printf("i=%d\nEnimi=%s\nPnimi=%s\nKaad=%s", i, inimene[i].Enimi, inimene[i].Pnimi, inimene[i].Kood);
    i++;
}

There are places for do/while loops; they are not used very often, though.

这篇关于使用与fgets来读取C从文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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