c - 使用多个文件指针试图在 C 中一次打开选择的文件..?

标签 c file pointers

<分区>

好吧,这是一个更大项目的一部分,它已经变得一团糟。我附上了程序中的一些示例。

我想做的是根据用户对上一个问题的输入打开一定数量的文件进行读取。 用户还需要提供文件夹的路径,我必须在字符串中附加文件名,我还没有这样做,因为我不确定我想怎么做。 但这不是目前的问题。 我的问题是我所有的文件指针都是错误的,而且我的编译器还说我有不匹配的值,我正在尝试比较,我不太确定为什么我会遇到这个问题。 你们看到什么/知道为什么我的代码不起作用吗?

我还附上了一张我试过的编译器的屏幕截图。 (是的,我尝试了不同的,因为有几次我犯了很多错误,然后我更换了编译器,它们就消失了)

int bpSensors;
  int hrSensors;
  int btSensors;

//values read passed to other functions
  char bp1_Val1[20], bp1_Val2[20], bp1_Val3[20], bp1_Line[100],
          bp2_Val1[20], bp2_Val2[20], bp2_Val3[20], bp2_Line[100],
          bp3_Val1[20], bp3_Val2[20], bp3_Val3[20], bp3_Line[100],
          bp4_Val1[20], bp4_Val2[20], bp4_Val3[20], bp4_Line[100],
          bp5_Val1[20], bp5_Val2[20], bp5_Val3[20], bp5_Line[100];

  char filepath[1000];

//file pointers
  FILE* fpBP1, fpBP2, fpBP3, fpBP4, fpBP5;

 printf("Enter the path to the file holding the data files:");

     fflush(stdin);
     scanf("%s", filepath);

     if (bpSensors == 1)
     {

        fpBP1 = fopen(filepath, "r");
        if (fpBP1 == NULL)

        {

           puts("ERROR OPENING FILES");

           exit(EXIT_FAILURE);
        }

        else
        {
           while (!feof(fpBP1))
           {
              printf("\n\nREADING BP_1.txt...");
              fgets(bp1_Line, 100, fpBP1);
              sscanf(bp1_Line, "%s\t%s\t%s", bp1_Val1, bp1_Val2, bp1_Val3);
              printf("%s\t%s\t%s\n", bp1_Val1, bp1_Val2, bp1_Val3);
           }

        }

     }// end BP Sensor = 1

     /////////////BP Sensors = 2//////////////////
        {
           if (bpSensors == 2)
           {
              //open file 1 &2
              fpBP1 = fopen(filepath, "r");
              fpBP2 = fopen(filepath, "r");

              //if they dont open, error shoots
              if (fpBP1 == NULL)

              {
                 puts("ERROR OPENING FILES");

                 exit(EXIT_FAILURE);
              }

              fpBP2 = fopen(filepath, "r");
              if (fpBP2 == NULL)

              {

                 puts("ERROR OPENING FILES");

                 exit(EXIT_FAILURE);
              }


              else
              {
                 while (!feof(fpBP1))
                 {
                    printf("\n\nREADING BP_1.txt...");
                    fgets(bp1_Line, 100, fpBP1);
                    sscanf(bp1_Line, "%s\t%s\t%s", bp1_Val1, bp1_Val2, bp1_Val3);
                    printf("%s\t%s\t%s\n", bp1_Val1, bp_Val2, bp_Val3);
                 }
                 while (!feof(fpBP2))
                 {
                    printf("\n\nREADING BP_2.txt...");
                    fgets(bp2_Line, 100, fpBP2);
                    sscanf(bp2_Line, "%s\t%s\t%s", bp2_Val1, bp2_Val2, bp2_Val3);
                    printf("%s\t%s\t%s\n", bp2_Val1, bp2_Val2, bp2_Val3);
                 }
              }
           }
        }// end BP = 2
     //////////////////BP Sensors = 3//////////////
        {
           if (bpSensors == 3)
           {
              //open file 1 -3
              fpBP1 = fopen(filepath, "r");
              fpBP2 = fopen(filepath, "r");
              fpBP3 = fopen(filepath, "r");

              //if they dont open, error shoots

              fpBP1 = fopen(filepath, "r");
              if (fpBP1 == NULL)

              {

                 puts("ERROR OPENING FILES");

                 exit(EXIT_FAILURE);
              }
              fpBP3 = fopen(filepath, "r");
              if (fpBP3 == NULL)

              {

                 puts("ERROR OPENING FILES");

                 exit(EXIT_FAILURE);
              }
              fpBP2 = fopen(filepath, "r");
              if (fpBP2 == NULL)

              {

                 puts("ERROR OPENING FILES");

                 exit(EXIT_FAILURE);
              }


              //read both files
              //if there open. files 1 -3 are read and displayed
              else
              {
                 while (!feof(fpBP1))
                 {
                    printf("\n\nREADING BP_1.txt...");
                    fgets(bp1_Line, 100, fpBP1);
                    sscanf(bp1_Line, "%s\t%s\t%s", bp1_Val1, bp1_Val2, bp1_Val3);
                    printf("%s\t%s\t%s\n", bp1_Val1, bp_Val2, bp_Val3);
                 }
                 while (!feof(fpBP2))
                 {
                    printf("\n\nREADING BP_2.txt...");
                    fgets(bp2_Line, 100, fpBP2);
                    sscanf(bp2_Line, "%s\t%s\t%s", bp2_Val1, bp2_Val2, bp2_Val3);
                    printf("%s\t%s\t%s\n", bp2_Val1, bp2_Val2, bp2_Val3);

                 }
                 while (!feof(fpBP3))
                 {
                    printf("\n\nREADING BP_3.txt...");
                    fgets(bp3_Line, 100, fpBP3);
                    sscanf(bp3_Line, "%s\t%s\t%s", bp3_Val1, bp3_Val2, bp3_Val3);
                    printf("%s\t%s\t%s\n", bp3_Val1, bp3_Val2, bp3_Val3);

                 }
              }
           }
        }// end BP = 3

这段代码大约有 800 行遵循这种模式。

最佳答案

定义错误,它将fpBP1定义为指向FILE的指针,而其他4个都是FILE类型的对象。

FILE* fpBP1, fpBP2, fpBP3, fpBP4, fpBP5;

您应该使用以下内容:

FILE *fpBP1, *fpBP2, *fpBP3, *fpBP4, *fpBP5;

或者更好的是,你应该这样做:

FILE *fpBP1;
FILE *fpBP2;
FILE *fpBP3;
FILE *fpBP4;
FILE *fpBP5;

关于c - 使用多个文件指针试图在 C 中一次打开选择的文件..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36858454/

相关文章:

c - 如何在 c 中打印结构成员的值(使用指向结构的指针)

c - C中的显示缓冲区

c - 驻留在一个内存地址的两个整数变量?

python - 在python中写入输出文件

java - 将字符串写入 Planner 中的文本文件

c++ - 可以将变化的数字输出到终端和文件的代码

c - 函数指针类型

c++ - 关于使用指针访问 3D 数组的元素

c - 函数指针中的最低有效位

c - 为什么命令行参数声明会导致段错误