English 中文(简体)
C: fscanf - infinite loop when first nature对等
原标题:C: fscanf - infinite loop when first character matches
  • 时间:2010-09-30 09:14:28
  •  标签:
  • c
  • scanf

我试图用扫描仪复制一个文本(CSS)文件,并删除所有与这一模式相符的声明:

@import “some/file/some where.css”;

为了做到这一点,我设立了以下机构:

FILE *file = fopen(pathToSomeFile, "r");
char *buffer = (char *)malloc(sizeof(char) * 9000);

while(!feof(file))
{
    // %*[^@] : Read and discard all characters up to a  @ 
    // %8999[^;] : Read up to 8999 characters starting at  @  to a  ; .
    if(fscanf(file, "%*[^@] %8999[^;]", buffer) == 1)
    {
        // Do stuff with the matching characters here.
        // This code is long and not relevant to the question.
    }
}

这完全是SO LONG AS, VERYIRST在档案中的性质不是@ 。 (嗣后,在特别安全局档案中第一个@ 特性之前的单一空间将对该代码进行罚款。)

但是,如果中心档案中的第一个特征是@ ,那么我所看到的夸张中的东西是无限的 lo——处决在 lo期间进入,打上了假冒的口号,但如声明(fscanf方)失败,则不会进入。

我认为,我的扫描格式可能需要一些 t,但无法保证如何进行。 任何建议或解释为什么发生这种情况?

谢谢。

最佳回答

您的形式表明采取了以下行动:

  • Read (and discard) 1 or more non-@ characters
  • Read (and discard) 0 or more whitespace characters (due to the space in the format string)
  • Read and store 1 to 8999 non-; characters

不幸的是,没有用户定义的“零或更多”特性的模型光谱仪。

如果你不关心一行的多种@include发言,你可以将你的代码改为一条单行(有点),然后从中提取“include 声明”(如果第一种特性不等于@,则你可以使用你现行格式,代之以扫描,否则可以使用scanf(line,“%8999[^;]”,缓冲)

如果一行的多个@include statemens应当得到正确处理,你可以检查下一个特点,并读到getc,然后将其带回ungetc

问题回答

页: 1 我对你的解释是:

  • Match a non-empty sequence of non- @ characters, then
  • Match a non-empty sequence of up to 8999 non- ; characters

因此,如果你以<条码>@为首,则第一部分将失败。

页: 1 如果你开始使用一些白色空间,那么fscanf就会在你的数据显示中吃任何主要的白色空间,即简单地说是%8999[^;]

Oli已经说,为什么失败。 而由于失败是你忙.的正常状态,这并不是假肢失败的结果,而是因为缺乏处理。

即便你的格式正确(在你的特殊情况下),你也不得不处理错误的失败,因为你不能确信,投入总是与格式相符。 实际上,你可以肯定,与投入匹配相比,有更多的非匹配投入。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签