C 如何将stdin扫描到数量可变的流中

C 如何将stdin扫描到数量可变的流中,c,scanf,C,Scanf,我想将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(outFi

我想将
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];
    }
}
charwords1[num][100]//作为命令行参数传递的num
i=0;
对于(i=0;i

目标是将
stdin
拆分为
num
个文件流,以便多个进程对文件进行排序。我想
vfscanf
可能会有所帮助,但您仍然需要知道要发送到多少格式说明符。我想我可以为循环和
strcat(格式,“%s”)
并将
vfscanf
va_列表一起使用
?有人能举个例子吗?

如果我正确理解您的问题,我认为您不需要复杂的
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;
    }
}
…然后上述程序将给出:

$ ./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

$。/nstream 4您有一个循环,可以运行
num
次。您希望在循环中读取多少字符串?我希望stdin是一个少于100个单词的文本文件。因此,如果我创建4个流,每个流将获得25个单词。很好。Thx以获得一个新的透视图。我从未想过使用这样的mod运算符或string converter.C霍森和他投了赞成票。
$ ./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