gpt4 book ai didi

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

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

我正在为从 3 个不同的 .txt 文件中获取数据的程序运行以下代码。当我尝试按列发布值时,我似乎无法控制发布数据的行。

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

这是我的问题发生的地方。我希望将以下两个变量打印在各自列下的同一行上。我已经编辑了我需要的空间,但出于某种原因,第二个变量“海滩”被发布在第二行。注意:传递“海滩”变量条件语句的数据位于第二个文件的第二行。这就是它被张贴在第二行的原因吗?如何控制我的数据发布在哪一行?

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

}

更新条件语句(使用 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] ='\0';
beach[strlen(beach)-1] ='\0';
}


/*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 命令来尝试对齐我的文本,但正如您所看到的那样效率不高。我怎样才能解决这个问题? enter image description here

最佳答案

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

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

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

正在测试,因为它可以在读取文件的最后一行时包含 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 ('\0') is stored after the last character in the buffer.

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

像这样你必须使用。

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

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

格式:

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

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

它将给出尾随空格。在这种情况下,测试是七个字符,因此将有三个尾随空格。

关于c - 如何控制程序输出的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28598645/

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