简体   繁体   English

如何在C(STDIN)中扫描重定向文件?

[英]How do you scan redirected files in C (STDIN)?

Say I'm calling a program: 假设我正在调用一个程序:

$ ./dataset < filename

where filename is any file with x amount of line pairs where the first line contains a string and second line contains 10 numbers separated by spaces. 其中filename是具有x个行对的任何文件,其中第一行包含字符串,第二行包含由空格分隔的10个数字。 The last line ends with "END" 最后一行以“END”结尾

How can I then start putting the first lines of pairs (string) into: 然后,我怎样才能开始将第一行对(字符串)放入:

char *experiments[20] // max of 20 pairs

and the second lines of the pairs (numbers) into: 并且第二行(数字)成:

int data[10][20] // max of 20, 10 integers each

Any guidance? 任何指导? I don't even understand how I'm supposed to scan the file into my arrays. 我甚至不明白我应该如何将文件扫描到我的数组中。

Update: 更新:

So say this is my file: 所以说这是我的文件:

Test One  
0 1 2 3 4 5 6 7 8 9  
END

Then redirecting this file would mean if I want to put the first line into my *experiments, that I would need to scan it as such? 然后重定向这个文件意味着如果我想把第一行放到我的*实验中,我需要扫描它吗?

scanf("%s", *experiments[0]);

Doing so gives me an error: Segmentation fault (core dumped) 这样做会给我一个错误:分段错误(核心转储)

What is incorrect about this? 这是不正确的?

Say my file is simply numbers, for ex: 说我的文件只是数字,例如:

0 1 2 3 4 5 6 7 8 9

Then, 然后,
scanf("%d", data[0][0]); works, and will hold value of '1'. 有效,价值为'1'。 Is there an easier way to do this for the whole line of data? 是否有更简单的方法为整个数据线执行此操作? ie data[0-9][0] . data[0-9][0]

The redirected file is associated with the FILE * stdin . 重定向的文件与FILE * stdin相关联。 It's already opened for you... 它已经为你打开了......

otherwise, you can treat it the same as any other text file, and/or use the functions that are dedicated to standard input - with the only exception that you cannot seek in the file and not retrieve the size of the input. 否则,您可以将其视为与任何其他文本文件相同,和/或使用专用于标准输入的函数 - 唯一的例外是您无法在文件中搜索而不检索输入的大小。

find the pseudo-code, code explains how to read the input 找到伪代码,代码解释了如何读取输入

int main()
{ 

    char str[100]; // make sure that this size is enough to hold the single line
    int no_line=1;

    while(gets(str) != NULL && strcmp(str,"END"))
    {    
            if(no_line % 2 == 0)
            {
                /*read integer values from the string "str" using sscanf, sscanf can be called in a loop with %d untill it fails */   
            }  
            else
            {
                /*strore string in your variable "experiments" , before copying allocate a memory for the each entry */ 
            }
         no_line++;
    }
 }

For the data sizes you're talking about, by far the easiest thing to do is just slurp all of the content into a buffer and work on that: you don't have to be super-stingy, just make sure that you don't overrun. 对于你所谈论的数据大小,到目前为止最简单的方法就是将所有内容放入缓冲区并进行处理:你不必非常吝啬,只要确保你不要超支。

If you want to be super-stingy with memory, preallocate a 4kB buffer with malloc() , progressively read() into it from stdin , and realloc() another 4kB every time the input exceeds what you've already read. 如果你想对内存过于吝啬,可以使用malloc()预先分配一个4kB缓冲区,从stdin逐步read() ,并在每次输入超过你已读过的时候再重新分配realloc()另一个4kB。 If you don't care so much about being stingy with memory (eg on a modern machine with gigabytes of memory), just malloc() something much bigger than the expected input (eg a megabyte) and bug out if the input is more than that: this is far simpler to implement but less general/elegant. 如果你不太关心内存的吝啬(例如在具有千兆字节内存的现代机器上),只需要malloc()比预期输入(例如兆字节)大得多的东西,如果输入超过预期输入则会出错那:这个实现起来要简单得多,但不那么通用/优雅。

You then have all of the input in a buffer and you can do what you like with it, which depends too strongly on the format of the input for me to say how you should approach that part. 然后,您可以将所有输入放在缓冲区中,并且可以使用它来执行您喜欢的操作,这非常依赖于输入的格式,以便我说明您应该如何处理该部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM