从 3 个不同的文件中获取数据并将其打印为 C 语言的输出?

Taking data from 3 different files and printing it as output in C language?

大家好,我目前 运行 遇到了一个项目问题。基本上 objective 是读取三个不同文件中的必要信息并打印收集的信息作为输出。我 运行 现在遇到的问题是我的条件 if 语句没有做它们应该做的事情。我正在通过 'watch' 功能观察我的变量的值,但是只要不满足条件(lake_x 值!= prv_x 值)循环仍然忽略它并继续通过 if语句而不是跳转到 else 语句。 这是我的代码。

#include <stdio.h>
#include <string.h>
int
main (void)

{
FILE *data_File;
FILE *lake_File;
FILE *beach_File;

char fileName[10], lake_Table[15],beach_Table[15];  /*.txt file names */

int lake_data=0,lake_x=0, beach_x=0, nr_tests=0;    /* variables for the file july08.txt */
int province_data=0,prv_x=0;        /* variables for the file Lake Table.txt */
int beach_data=0,bch_x=0;           /* variables for the file Beach Table.txt*/

char province[30] = ""; /*variable for the data within the file Lake Table.txt*/
char beach[20]="";   /*variable for the data within the file Beach Table.txt*/

int j;
double status, ecoli_lvl;
printf ("Which month would you like a summary of? \nType month followed by date (i.e: july05): ");
gets(fileName);

/*Opening the files needed for the program*/
data_File = fopen (fileName, "r");
lake_File = fopen ("Lake Table.txt", "r");
beach_File = fopen ("Beach Table.txt", "r");

/*Printing Headers*/
printf ("\nLake     Beach    Average     E-Coli Level        Recommendation\n"); 


/* july08.txt file*/
fscanf (data_File, "%d", &lake_x);
fscanf (data_File, "%d", &beach_x);
lake_data = fscanf (data_File, "%d", &nr_tests);

/* Lake Table.txt file*/
province_data = fscanf (lake_File, "%d", &prv_x);
fgets (province,30,lake_File);

/* Beach Table.txt file*/
beach_data = fscanf (beach_File, "%d", &bch_x);
fgets (beach,20,beach_File);

status = (double) 0;

/*If the value from the july08.txt matches to value from Lake Table.txt file then print the province that matches to it in the Lake Table.txt*/!
while (province_data > 0)
{
    if (lake_x = prv_x)
    {
        printf ("%s", province);
        province_data = 0;

    }    
    else
    {
        province_data = fscanf (lake_File, "%d", &prv_x);
        fgets (province,30,lake_File);
    }      

}    

while (beach_data > 0)
{
    if (beach_x = bch_x)
    {
        printf ("        %s", beach);
        beach_data = 0;
    }    
    else
    {
        beach_data = fscanf (beach_File, "%d", &bch_x);
        fgets (beach,30,beach_File);
    }

}    

我的问题从这里开始。正如您从图像中看到的那样,您会注意到 beach_x = 101 和 bch_x = 100 但是一旦代码移过 while 语句,数据就会变得相同并且 if 语句为真并且继续打印 Lake Table.txt 文件中的所有数据。这是我刚才提到的结果。

那么,为什么我的 if 语句不能正常运行?

while (lake_data != EOF)
{
    for (j=1; j<=nr_tests; ++j)
    {
        fscanf (data_File, "%lf", &ecoli_lvl);
        status = status + ecoli_lvl;
    }
    printf ("                     %.d", j);
    fscanf (data_File, "\n%d", &lake_x);
    fscanf (data_File, "%d", &beach_x);
    lake_data = fscanf (data_File, "\n%d", &nr_tests);
    printf ("\n");
}


fclose (data_File);

return (0);

}

非常感谢。抱歉,我只是发布了整个代码的长度,以防您需要完整的图片。

据我了解,您正在尝试比较 beach_xbch_x 在 if-陈述。问题是您将 bch_x 的值分配给 beach_x.

要比较它们,并将此结果与文件进行比较,您需要这样做:

if ((beach_x == bch_x) && (beach_x != 0)
{
    printf ("        %s", beach);
    beach_data = 0;
}    
else
{
    beach_data = fscanf (beach_File, "%d", &bch_x);
    fgets (beach,30,beach_File);
} 

注意:

您可以将文件与任一变量进行比较,因为在第一步中它们需要相等。

使用==进行比较,而不是=

if (beach_x == bch_x)

通过使用 =,您所做的只是将 bch_x 分配给 beach_x

您在其他几个 if 语句中犯了同样的错误,例如这个:

if (lake_x = prv_x)