命令行参数,读取文件

时间:2021-10-22 23:16:59

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./*

5 个解决方案

#1


13  

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.txt to run your program:

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

  • argc will be 2
  • argc将是2

  • argv[0] will be myprogram.
  • argv [0]将是myprogram。

  • argv[1] will be myfile.txt.
  • 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 opening the file in other modes, read this.

要在其他模式下打开文件,请阅读此内容。

#2


2  

  1. Declare your main like this

    像这样宣告你的主要

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

    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)

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

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

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

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

      你会从命令行中读取文件,如下所示:C:\ my_program input_file.txt

  2. Set up a file handle:

    设置文件句柄:

    File* file_handle;

  3. Open the file_handle for reading:

    打开file_handle进行阅读:

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

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

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

      如果文件不存在,fopen返回指向文件的指针或NULL。 argv1,包含要作为参数读取的文件

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

      “r”表示您打开文件进行阅读(此处更多关于其他模式)

  4. Read the contents using for example fgets:

    使用例如fgets读取内容:

    fgets (buffer_to_store_data_in , 50 , file_handle);

    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
    • 你需要一个char *缓冲区来存储数据(比如一个字符数组),第二个参数指定要读取多少,第三个参数是指向文件的指针

  5. Finally close the handle

    最后关上手柄

    fclose(file_handle);

All done :)

全做完了 :)

#3


1  

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 */ 
}

#4


0  

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.

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

#5


0  

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 stdin instead of a file, then drive your app by piping, for example cat myfile > yourpgm. You then can use scanf to read from the stdin. In an analogous way you can use stdout/stderr to produce the output.

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

#1


13  

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.txt to run your program:

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

  • argc will be 2
  • argc将是2

  • argv[0] will be myprogram.
  • argv [0]将是myprogram。

  • argv[1] will be myfile.txt.
  • 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 opening the file in other modes, read this.

要在其他模式下打开文件,请阅读此内容。

#2


2  

  1. Declare your main like this

    像这样宣告你的主要

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

    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)

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

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

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

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

      你会从命令行中读取文件,如下所示:C:\ my_program input_file.txt

  2. Set up a file handle:

    设置文件句柄:

    File* file_handle;

  3. Open the file_handle for reading:

    打开file_handle进行阅读:

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

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

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

      如果文件不存在,fopen返回指向文件的指针或NULL。 argv1,包含要作为参数读取的文件

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

      “r”表示您打开文件进行阅读(此处更多关于其他模式)

  4. Read the contents using for example fgets:

    使用例如fgets读取内容:

    fgets (buffer_to_store_data_in , 50 , file_handle);

    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
    • 你需要一个char *缓冲区来存储数据(比如一个字符数组),第二个参数指定要读取多少,第三个参数是指向文件的指针

  5. Finally close the handle

    最后关上手柄

    fclose(file_handle);

All done :)

全做完了 :)

#3


1  

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 */ 
}

#4


0  

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.

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

#5


0  

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 stdin instead of a file, then drive your app by piping, for example cat myfile > yourpgm. You then can use scanf to read from the stdin. In an analogous way you can use stdout/stderr to produce the output.

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