C语言命令行尖括号使用问题及文件读取代码咨询
Hey there! Let's break down how command line angle brackets work with your C program. First, let's clean up your code to make it easier to read (I added the missing header files too—they're required for the functions you're using):
#include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { FILE * fp; char * line = NULL; size_t len = 0; ssize_t read; char * txtfile = argv[1]; fp = fopen(txtfile, "r"); if (fp == NULL) exit(EXIT_FAILURE); while ((read = getline(&line, &len, fp)) != -1) { printf("Retrieved line of length %zu :\n", read); printf("%s", line); } fclose(fp); if (line) free(line); exit(EXIT_SUCCESS); return 0; }
Those angle brackets are part of shell input/output redirection—they're handled by your terminal shell (like bash or zsh), not directly by your C program. Here's what you need to know:
1. Input Redirection (<)
The < operator tells the shell to take the content of a specified file and feed it into your program's standard input (stdin). Instead of your program opening the file itself, it reads input just like it would if you were typing into the terminal.
For example, if you run:
./your_compiled_program < sample.txt
Your shell opens sample.txt behind the scenes and sends all its content to your program's stdin.
How this interacts with your current code
Right now, your program expects a filename as a command-line argument (argv[1]), so you run it like:
./your_compiled_program sample.txt
In this case, your program directly opens and reads the file with fopen(). If you use <, argv[1] will be NULL, and your original code will crash because it tries to pass a NULL pointer to fopen().
Fixing your code to support both modes
If you want your program to handle both passing a filename and using input redirection, modify it to check if a filename was provided. If not, read from stdin:
#include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { FILE * fp; char * line = NULL; size_t len = 0; ssize_t read; // Check if we got a filename argument if (argc == 2) { fp = fopen(argv[1], "r"); if (fp == NULL) { perror("Oops, couldn't open the file"); // Gives a readable error exit(EXIT_FAILURE); } } else { // No filename? Read from stdin (for < redirection) fp = stdin; } while ((read = getline(&line, &len, fp)) != -1) { printf("Retrieved line of length %zu :\n", read); printf("%s", line); } // Only close the file if we opened it (don't close stdin!) if (argc == 2) fclose(fp); if (line) free(line); exit(EXIT_SUCCESS); return 0; }
2. Output Redirection (>/>>)
While you asked about angle brackets generally, it's useful to cover output redirection too:
>sends your program's standard output (stdout) to a file, overwriting any existing content:./your_compiled_program sample.txt > output.txt>>appends your program's output to a file instead of overwriting it:./your_compiled_program sample.txt >> output.txt
Your current code uses printf() which writes to stdout, so these will work without any changes to your code.
Quick Key Points
- Redirection is a shell feature—your program just reads from stdin/writes to stdout as normal when it's used.
- Always check
argcbefore accessingargvto avoid crashes when no arguments are provided. perror()is a handy function to give users meaningful error messages instead of silent failures.
内容的提问来源于stack exchange,提问作者Calahan

