简体   繁体   English

C程序读取多个文件的技术

[英]c programming reading multiple files technique

I am curious what is the best way to open multiple files. 我很好奇打开多个文件的最佳方法是什么。 I know you use a combination of FILE *inputfp1; 我知道您使用FILE *inputfp1;的组合FILE *inputfp1; and inputfp1 = fopen(argv[1], "r"); inputfp1 = fopen(argv[1], "r"); then check for errors. 然后检查错误。 I would like to know the best way to do this. 我想知道最好的方法。

Is it best to open and close one file at a time like this? 最好一次打开和关闭一个文件吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char **argv) {

    char line[80] = {0};
    FILE *inputfp1;
    //input = fopen("myfile.txt", "r");
    inputfp1 = fopen(argv[1], "r");                         //Open file for read.

    if (inputfp1 == NULL)
    {
        printf("Error opening file %s!",argv[1]);    //Program prints error message and closes if file is not found            
        exit(0);
    }

    if( argc == 7 )
    {
        /*printf("The first argument supplied is %s\n", argv[1]);
        printf("The second argument supplied is %s\n", argv[2]);
        printf("The third argument supplied is %s\n", argv[3]);
        printf("The first argument supplied is %s\n", argv[4]);
        printf("The second argument supplied is %s\n", argv[5]);
        printf("The third argument supplied is %s\n", argv[6]);
        printf("The third argument supplied is %s\n", argv[7]);*/
    }
    else if( argc > 7 )
    {
        printf("Too many arguments supplied.\n");
        exit( 1 );
    }
    else
    {
        printf("Not enough arguments supplied. \n");
        exit( 1 );
    }

    //Unique behavior on file1
    while(fgets(line, 80, inputfp1) != NULL)
    {
        //do work on file1
    }
    fclose(inputfp1);

    inputfp1 = fopen(argv[2], "r");                         //Open file for read.

    if (inputfp1 == NULL)
    {
        printf("Error opening file %s!",argv[1]);    //Program prints error message and closes if file is not found            
        exit(0);
    }

    //Unique behavior on file2
    while(fgets(line, 80, inputfp1) != NULL)
    {
        //do work on file2
    }
    fclose(inputfp1);

    return 0;
}

Is it better to create all the file pointers and open all the files at once like this? 这样创建所有文件指针并一次打开所有文件是否更好?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char **argv) {

    char line[80] = {0};
    FILE *inputfp1;
    FILE *inputfp2;
    FILE *inputfp3;
    FILE *inputfp4;
    FILE *inputfp5;
    FILE *inputfp6;
    //input = fopen("myfile.txt", "r");
    inputfp1 = fopen(argv[1], "r");                         //Open file for read.
    inputfp2 = fopen(argv[2], "r");                         //Open file for read.
    inputfp3 = fopen(argv[3], "r");                         //Open file for read.
    inputfp4 = fopen(argv[4], "r");                         //Open file for read.
    inputfp5 = fopen(argv[5], "r");                         //Open file for read.
    inputfp6 = fopen(argv[6], "r");                         //Open file for read.

    if (inputfp1 == NULL)
    {
        printf("Error opening file %s!",argv[1]);    //Program prints error message and closes if file is not found            
        exit(0);
    }

    //The rest of error checking. 

    if( argc == 7 )
    {
        /*printf("The first argument supplied is %s\n", argv[1]);
        printf("The second argument supplied is %s\n", argv[2]);
        printf("The third argument supplied is %s\n", argv[3]);
        printf("The first argument supplied is %s\n", argv[4]);
        printf("The second argument supplied is %s\n", argv[5]);
        printf("The third argument supplied is %s\n", argv[6]);
        printf("The third argument supplied is %s\n", argv[7]);*/
    }
    else if( argc > 7 )
    {
        printf("Too many arguments supplied.\n");
        exit( 1 );
    }
    else
    {
        printf("Not enough arguments supplied. \n");
        exit( 1 );
    }

    //Unique behavior on file1
    while(fgets(line, 80, inputfp1) != NULL)
    {
        //do work on file1
    }
    fclose(inputfp1);

    //Unique behavior on file2
    while(fgets(line, 80, inputfp2) != NULL)
    {
        //do work on file2
    }
    fclose(inputfp2);

    //The rest of reading and closing files. 

    return 0;
}

Are there any better ways I missed? 我有没有更好的方法?

A good way of doing this would be putting all your file pointers in an array: 这样做的一个好方法是将所有文件指针放在一个数组中:

 FILE *inputfp[6]; 
for(int i=0;i<6;i++)
{
  inputfp[i] = fopen(argv[i+1], "r");                         //Open file for read.

  if (inputfp[i] == NULL)
  {
    printf("Error opening file %s!",argv[i+1]);    //Program prints error message and closes if file is not found            
    exit(0);
  }
}

I'd do it the first way, but use a loop: 我会第一种方式做,但是要使用循环:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char **argv) {

    char line[80] = {0};
    FILE *inputfp1;

    if( argc == 7 )
    {
        /*printf("The first argument supplied is %s\n", argv[1]);
        printf("The second argument supplied is %s\n", argv[2]);
        printf("The third argument supplied is %s\n", argv[3]);
        printf("The first argument supplied is %s\n", argv[4]);
        printf("The second argument supplied is %s\n", argv[5]);
        printf("The third argument supplied is %s\n", argv[6]);
        printf("The third argument supplied is %s\n", argv[7]);*/
    }
    else if( argc > 7 )
    {
        printf("Too many arguments supplied.\n");
        exit( 1 );
    }
    else
    {
        printf("Not enough arguments supplied. \n");
        exit( 1 );
    }

    for (int i = 1; i < argc; ++i)
    {
        inputfp1 = fopen(argv[i], "r");                         //Open file for read.
        if (inputfp1 == NULL)
        {
            printf("Error opening file %s!",argv[i]);    //Program prints error message and closes if file is not found            
            exit(0);
        }

        while(fgets(line, 80, inputfp1) != NULL)
        {
            //do work
        }
        fclose(inputfp1);
    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM