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

标签 c if-statement while-loop

大家好,我目前遇到了一个项目问题。基本上,目标是读取三个不同文件中的必要信息并打印收集的信息作为输出。我现在遇到的问题是我的条件 if 语句没有做它们应该做的事情。我正在通过“观察”功能观察变量的值,但只要不满足条件(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 语句为真并继续打印湖表.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);

}

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

最佳答案

据我了解,您正在尝试比较 if 语句中的 beach_xbch_x。问题是您将 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);
} 

注意:

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

关于c - 从 3 个不同的文件中获取数据并将其打印为 C 语言的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28597959/

相关文章:

创建与 IPv4 和 IPv6 兼容的套接字

mysql - MySQL 事务和变量的问题

java - While循环将循环遍历数组java中的条目

python - While 循环引用时间

c - Kochan 的 C 编程练习 7.12

清除 C 中的输入缓冲区

python - 无效语法 - python

c - 在 C 中混合 'switch' 和 'while'

c - 为什么我的整数菜单有效但我的字符菜单无效?

MySQL IF THEN AS 语法