如何控制程序输出的格式?

How to take control of formatting a programm's output?

我是 运行 下面的程序代码,它从 3 个不同的 .txt 文件中获取数据。当我尝试按列 post 值时,我似乎无法控制我的数据得到 posted 的行。

#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 data within 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]="";   /*variable for 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");

/*These are my columns*/
printf ("\n  Lake           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;

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);
    }

这就是我的问题所在。我希望将以下两个变量打印在各自列下的同一行上。我已经编辑了我需要的空间,但由于某种原因,第二个变量 'beach' 在第二行得到 posted。注意:通过 'beach' 变量的条件语句的数据位于第二个文件的第二行。这就是它在第二行 posted 的原因吗?如何控制我的数据被 post 编辑的行?

printf ("%s     %s", province, beach);

}    

更新条件 while 语句(使用 strlen 方法)

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] ='[=12=]';
            beach[strlen(beach)-1] ='[=12=]';
        }        


        /*code was right here*/


    }            

    for (j=1; j<=nr_tests; ++j)
    {
        fscanf (data_File, "%lf", &ecoli_lvl);
        status = status + ecoli_lvl;
    }


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

    /* printf ("             %.2f", status); */

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

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


fclose (data_File);

return (0);

} 这是我的输出:我在“”内使用 tab 命令来尝试对齐我的文本,但正如您所看到的那样效率不高。我怎样才能解决这个问题?

您正在从 fgets 获取输入,它获取包含换行符的行, 之后它放置空值。

当您打印 province 换行符时,该字符串中放置了换行符。所以下一行打印的第二个变量。

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

正在测试,因为它在读取文件的最后一行时可以有 EOF 个字符。

来自 fgets

的手册页

Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('[=20=]') is stored after the last character in the buffer.

strlen() 引用此 link。这是一个简单的函数,用于获取放置在给定字符串中的字符总数,包括空字符。它只适用于字符串。

这样你就得用

if (province[strlen(province)-1] == '\n')
 {
    province[strlen(province)-1] = '[=11=]';
    beach[strlen(beach)-1] = '[=11=]';
 }

 printf ("%s     %s", province, beach);
}   

格式:

在打印 scanf 时,您可以使用 -。用于打印正确的 space.

  printf("test:%-10s\n","Testing");

它将给出尾随 space。在这种情况下,测试是七个字符,因此三个尾随 spaces 将在那里。