0

I'm trying to read in a file that contains multiple lines of the form:

www.someurl.com,timestamp

I am using the following code:

char url[256];
unsigned int timestamp;

// Read each line from the input file.
while(fscanf(inputfile, "%s,%d", url, &timestamp) == 2) {
    printf("%s was visited at %d\n", url, timestamp);
}

However, fscanf scans the entire line into the string, and does not scan the timestamp into the integer. I'm sure this is a very basic mistake, but I can't figure it out. Could someone please explain why this is, and how I can go about fixing it?

4

1 に答える 1

4

を処理するとき%sfscanfは空白で終了する文字列を期待しており、貪欲でもあります。そのため、行全体が URL に読み込まれます。

禁止文字セットを直接指定できます。

fscanf(inputfile, "%[^,],%d", url, &timestamp)

fscanfバリアントとフォーマット文字列に関するもう少しのデータを次に示します。

于 2013-02-06T20:49:48.717 に答える