C语言 命令行参数,读取文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16869467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:32:03  来源:igfitidea点击:

Command line arguments, reading a file

cfileargumentscommand-line-arguments

提问by user2440329

If i entered into the command line C: myprogram myfile.txt

如果我进入命令行 C: myprogram myfile.txt

How can I use myfile in my program. Do I have to scanf it in or is there an arbitrary way of accessing it.

如何在我的程序中使用 myfile。我必须扫描它还是有任意访问它的方式。

My question is how can I use the myfile.txt in my program.

我的问题是如何在我的程序中使用 myfile.txt。

int
main(){
    /* So in this area how do I access the myfile.txt 
    to then be able to read from it./*

回答by srikanta

You can use int main(int argc, char **argv)as your main function.

您可以将其int main(int argc, char **argv)用作主要功能。

argc- will be the count of input arguments to your program.
argv- will be a pointer to all the input arguments.

argc- 将是您程序的输入参数的计数。
argv- 将是指向所有输入参数的指针。

So, if you entered C:\myprogram myfile.txtto run your program:

所以,如果你输入C:\myprogram myfile.txt运行你的程序:

  • argcwill be 2
  • argv[0]will be myprogram.
  • argv[1]will be myfile.txt.
  • argc将是 2
  • argv[0]myprogram
  • argv[1]myfile.txt

More details can be found here

可以在此处找到更多详细信息

To read the file:
FILE *f = fopen(argv[1], "r"); // "r" for read

要读取文件:
FILE *f = fopen(argv[1], "r"); // "r" for read

For opening the file in other modes, read this.

要以其他模式打开文件,请阅读此.

回答by Nobilis

  1. Declare your main like this

    int main(int argc, char* argv [])

    • argc specified the number of arguments (if no arguments are passed it's equal to 1 for the name of the program)

    • argv is a pointer to an array of strings (containing at least one member - the name of the program)

    • you would read the file from the command line like so: C:\my_program input_file.txt

  2. Set up a file handle:

    File* file_handle;

  3. Open the file_handle for reading:

    file_handle = fopen(argv[1], "r");

    • fopen returns a pointer to a file or NULLif the file doesn't exist. argv1, contains the file you want to read as an argument

    • "r" means that you open the file for reading (more on the other modes here)

  4. Read the contents using for example fgets:

    fgets (buffer_to_store_data_in , 50 , file_handle);

    • you need a char *buffer to store the data in (such as an array of chars), the second argument specifies how much to read and the third is a pointer to a file
  5. Finally close the handle

    fclose(file_handle);

  1. 像这样声明你的主要

    int main(int argc, char* argv [])

    • argc 指定参数的数量(如果没有传递参数,则程序名称等于 1)

    • argv 是一个指向字符串数组的指针(至少包含一个成员——程序的名称)

    • 您可以像这样从命令行读取文件: C:\my_program input_file.txt

  2. 设置文件句柄:

    File* file_handle;

  3. 打开 file_handle 进行读取:

    file_handle = fopen(argv[1], "r");

    • fopen 返回一个指向文件或NULL文件不存在的指针。argv 1,包含要作为参数读取的文件

    • “r”表示您打开文件进行阅读(更多关于其他模式在这里

  4. 使用例如fgets阅读内容:

    fgets (buffer_to_store_data_in , 50 , file_handle);

    • 您需要一个char *缓冲区来存储数据(例如字符数组),第二个参数指定要读取的数量,第三个参数是指向文件的指针
  5. 最后关闭手柄

    fclose(file_handle);

All done :)

全部完成 :)

回答by egrunin

This is the Programming 101 way. It takes a lot for granted, and it doesn't do any error-checking at all! But it will get you started.

这就是编程 101 方式。它认为很多都是理所当然的,它根本不做任何错误检查!但它会让你开始。

/* this has declarations for fopen(), printf(), etc. */
#include <stdio.h>

/* Arbitrary, just to set the size of the buffer (see below).
   Can be bigger or smaller */
#define BUFSIZE 1000

int main(int argc, char *argv[])
{
    /* the first command-line parameter is in argv[1] 
       (arg[0] is the name of the program) */
    FILE *fp = fopen(argv[1], "r"); /* "r" = open for reading */

    char buff[BUFSIZE]; /* a buffer to hold what you read in */

    /* read in one line, up to BUFSIZE-1 in length */
    while(fgets(buff, BUFSIZE - 1, fp) != NULL) 
    {
        /* buff has one line of the file, do with it what you will... */

        printf ("%s\n", buff); /* ...such as show it on the screen */
    }
    fclose(fp);  /* close the file */ 
}

回答by Felice Pollano

All the suggestion you received about using the command line are correct,but It sounds to me you can also consider to use a typical pattern that is read the stdininstead of a file, then drive your app by piping, for example cat myfile > yourpgm. You then can use scanfto read from the stdin. In an analogous way you can use stdout/stderrto produce the output.

您收到的有关使用命令行的所有建议都是正确的,但在我看来,您也可以考虑使用读取stdin文件而不是文件的典型模式,然后通过管道驱动您的应用程序,例如 cat myfile > yourpgm。然后您可以使用scanf从标准输入读取。以类似的方式,您可以使用它stdout/stderr来生成输出。

回答by akalenuk

Command line arguments are just plain C-strings. You can do whatever you want with them. In your case you might want to open a file, read something from it and close it.

命令行参数只是普通的 C 字符串。你可以对他们做任何你想做的事情。在您的情况下,您可能想要打开一个文件,从中读取一些内容并关闭它。

You might find this question(and answer) useful.

您可能会发现这个问题(和答案)很有用。