C-Enter the name of the file at the command line and print the contents of the file

Source: Internet
Author: User

C language programming, often encountered in the main function argc and argv[] these two parameters. ARGC is the abbreviation for argument count, which is the number of parameters, argv is the abbreviation of argument vector, which is the parameter list. Argv[0] is the name of the program itself, Argv[1] is the parameter of the first program entered on the command line, ARGV[ARGC] is null, as follows:

#include "stdio.h" int main (int argc, char *argv[]) {    printf ("The argc value is%d \ n", argc);    int i;    for (i = 0; I <= argc; i++) {            printf ("The argv[%d] value is%s \ n", I, Argv[i]);    }    return 0;} #将上述代码编译为test可执行文件, enter the following on the command line/*./test arg_1 arg_2*/#执行结果如下:/*the argc value is 3 the argv[0] value is./test_c_0 the argv[ 1] value is arg_1 the argv[2] value was arg_2 the argv[3] value is (NULL) */

Figuring out argc and argv[], we can use both to transfer the file name parameters that will be processed by the command line to the program, as shown in the code below.

#include "stdio.h" int main (int argc, char *argv[]) {    FILE *fp;    int C;    fp = fopen (Argv[1], "R");    while ((c = fgetc (FP))! = EOF) {        printf ("%c", c);    }    Fclose (FP);    return 0;}



C-Enter the name of the file at the command line and print the contents of the file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.