简体   繁体   中英

How do I read file into a command line?

Basically what I want to do is have a program with int main(argc, *argv[]) and instead of writing chars into command line, I want to have my program read those words from a file. How could I accomplish this? Is there a special command in Linux for that?

You can use standard redirect operations in a *nix shell to pass files as input:

./myprogram < inputfile.txt

This statement executes your program (myprogram) and pumps the data inside of inputfile.txt to your program

You can also redirect the output of program to a file in a similar fashion:

./myprogram > outputfile.txt

Use redirection

yourprogram < youtextfile

will offer the content of yourtextfile as standard input (stdin) to yourprogram. Likewise

yourprogram > yourothertextfile

will send everything the program writes to standard output (stdout) to yourothertextfile

You'll notice when reading man pages that most system calls have a version that works directly with stdin or stdout

For example consider the printf family:

printf ("hello world\n");

is a shorter version of

fprintf (stdout,"hello world\n");

and the same goes for scanf and stdin.

This is only the most basic usage of redirection, which in my opinion is one of the key aspects of "the unix way of doing things". As such, you'll find lots of articles and tutorials that show examples that are a lot more advanced than what I wrote here. Have a look at this Linux Documentation Project page on redirection to get started.

EDIT: getting fed input via redirection ior interactively "looks" the same to the program, so it will react the same to redirected input as it does to console input. This means that if your program expects data line-wise (eg because it uses gets() to read lines), the input text file should be organized in lines.

Instead of doing

for(int i = 1; i < argc; i++)
{
    insert(&trie, argv[i]);
}

you could doing something like

FILE *input;
char *line;

....

while (fscanf(input, "%ms", &line) != EOF) {
    insert(&trie, line);
        /* If you make a copy of line in `insert()`, you should
         * free `line` at here; if you do not, free it later. */
        free(line);
}

By default, every program you execute on POSIX-compliant systems has three file descriptors open (see <unistd.h> for the macros' definition): the standard input ( STDOUT_FILENO ), the standard output ( STDOUT_FILENO ), and the error output ( STDERR_FILENO ), which is tied to the console.

Since you said you want read lines, I believe the ssize_t getline(char **lineptr, size_t *n, FILE *stream) function can do the job. It takes a stream ( FILE pointer) as a third argument, so you must either use fopen(3) to open a file, or a combination of open(2) and fdopen(3) .

Getting inspiration from man 3 getline , here is a program demonstrating what you want:

#define _GNU_SOURCE
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    FILE    *fp;
    size_t  len;
    char    *line;
    ssize_t bytes_read;

    len = 0;
    line = NULL;
    if (argc > 1)
    {
        fp = fopen(argv[1], "r");
        if (fp == NULL)
        {
            perror(*argv);
            exit(EXIT_FAILURE);
        }
    }
    else
        fp = stdin;

    while ((bytes_read = getline(&line, &len, fp)) != -1)
        printf("[%2zi] %s", bytes_read, line);
    free(line);
    exit(EXIT_SUCCESS);
}

Without arguments, this program reads lines from the standard input: you can either feed it lines like echo "This is a line of 31 characters" | ./a.out echo "This is a line of 31 characters" | ./a.out or execute it directly and write your input from there (finish with ^D).

With a file as an argument, it will output every line from the file, and then exit.

You can have your executable read its arguments on the command line and use xargs , the special Linux command for passing the contents of a file to a command as arguments.

An alternative to xargs is parallel .

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