gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:23:53 25 4
gpt4 key购买 nike

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

这是我的代码:

#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] ='\0';
else if (beach[strlen(beach)-1] =='\n')
beach[strlen(beach)-1] ='\0';
}
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] ='\0';
else if (beach[strlen(beach)-1] =='\n')// here
beach[strlen(beach)-1] ='\0';

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

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

将语句改为这样,

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

因此,如果新行放置在海滩字符串中,输出将与您的输出类似。打印后,将打印海滩换行符和两个选项卡 \t\t。这就是您在下一行获得输出的原因。然后就像评论中所说的那样,使用 %-s 来填充空格。它将为您提供格式化的打印。

关于C 语言 - 如何确保在读取多个输入文件时保持恒定格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28621567/

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