简体   繁体   中英

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. The last line ends with "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'. Is there an easier way to do this for the whole line of data? ie data[0-9][0] .

The redirected file is associated with the 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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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