我想把stdin
扫描成数量可变的字符数组。如下所示:
char words1[num][100]; //num passed as command line argument
i = 0;
for (i = 0; i < num; ++i)
{
While (fscanf(stdin, "%s %s %s ...", words[i], words[i + 1], word[i + 2] ...) != EOF)
{
fprintf(outFileStream, "%s", words[i];
}
}
我们的目标是将stdin
拆分成num
个文件流,以便多个进程对文件进行排序。我认为vfscanf
可能会有所帮助,但您仍然需要知道要发送到多少个格式说明符。我想我可以使用循环和strcat(format, " %s")
,并使用带有va_list
的vfscanf
?谁能举个例子?
发布于 2013-02-17 21:22:41
如果我没理解错的话,我认为您不需要复杂的fscanf
格式,但一次只能读取一个字符串。也就是说,您可以使用如下内容:
#include <stdio.h>
int main (int argc, char** argv) {
int num = atoi(argv[1]);
char words[num][100];
int i = 0;
while (fscanf(stdin,"%s",words[i]) > 0) {
fprintf(stdout,"Stream %d: %s\n",i,words[i]);
i = (i + 1 ) % num;
}
}
输入文件texta.txt
如下:
a
b
c
d
e
f
g
h
i
j
k
l
m
n
..。那么上面的程序将会给出:
$ ./nstream 4 <texta.txt
Stream 0: a
Stream 1: b
Stream 2: c
Stream 3: d
Stream 0: e
Stream 1: f
Stream 2: g
Stream 3: h
Stream 0: i
Stream 1: j
Stream 2: k
Stream 3: l
Stream 0: m
Stream 1: n
https://stackoverflow.com/questions/14924776
复制相似问题