gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:04 26 4
gpt4 key购买 nike

<分区>

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

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

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

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 行遵循这种模式。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com