C 语言 - 如何确保在读取多个输入文件时保持不变的格式?

C Language - how to ensure a constant format is kept while reading multiple input files?

我正在尝试为以下代码缩进我的字符串输出,但由于某些原因,我的变量不断从文件中提取,并且具有一定长度的噪声或 space(我不确定)。

这是我的代码:

#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 file Lake Table.txt*/
char beach[20]="",beach1[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");
printf ("\n  Lake           Beach          Average E-Coli Level     Recommendation\n");
dashes();

/* 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;

while (lake_data != EOF)
{
    while (province_data > 0)
    {
        if (lake_x == prv_x)
        {
            province_data = 0;
            while (beach_data > 0)
            {
                if (beach_x == bch_x)
                {
                    beach_data = 0;
                }
                else
                {
                    beach_data = fscanf (beach_File, "%d", &bch_x);
                    fgets (beach,30,beach_File);
                }
            }
        }
        else
        {
            province_data = fscanf (lake_File, "%d", &prv_x);
            fgets (province,30,lake_File);
        }
        if (province[strlen(province)-1] =='\n')
            province[strlen(province)-1] ='[=10=]';
        else if (beach[strlen(beach)-1] =='\n')
            beach[strlen(beach)-1] ='[=10=]';
    }
    for (j=1; j<=nr_tests; ++j)
    {
        fscanf (data_File, "%lf", &ecoli_lvl);
        status = status + ecoli_lvl;
    }

    printf ("%s\t%s\t\t%.2f", province, beach, status);

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

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

    fscanf (data_File, "%d", &lake_x);
    fscanf (data_File, "%d", &beach_x);
    lake_data = fscanf (data_File, "%d", &nr_tests);
    printf ("\n");
    status = (double) 0;
}
fclose (data_File);
return (0);
}

如何对齐每列下的字符串?请记住,这是 C 语言的初学者编程。谢谢!

在此声明中,

 if (province[strlen(province)-1] =='\n')
        province[strlen(province)-1] ='[=10=]';
    else if (beach[strlen(beach)-1] =='\n')// here
        beach[strlen(beach)-1] ='[=10=]';

在此语句中,您必须使用空字符而不是换行符。因为 你是这样打印的。

printf ("%s\t%s\t\t%.2f", province, beach, status);

把语句改成这样,

if (province[strlen(province)-1] =='\n')
        province[strlen(province)-1] ='[=12=]';
if (beach[strlen(beach)-1] =='\n')
        beach[strlen(beach)-1] ='[=12=]';

所以如果新行放在海滩字符串中,输出就会像你的输出一样。打印海滩换行符后,将打印两个制表符 \t\t。这就是您在下一行获得输出的原因。然后就像评论中所说的那样,使用 %-s 来填充空格。它会给你格式化的打印。