sách gpt4 ăn đã đi

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

In lại Tác giả: Walker 123 更新时间:2023-11-30 15:23:53 26 4
mua khóa gpt4 giày nike

我正在尝试缩进以下代码的字符串输出,但由于某种原因,我的变量不断从文件中提取,并且具有不同长度的噪声或空间(我不确定)。 nhập mô tả hình ảnh ở đây nhập mô tả hình ảnh ở đây

Đây là mã của tôi:

#include 
#include
số nguyên
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;
}
khác
{
beach_data = fscanf (beach_File, "%d", &bch_x);
fgets (beach,30,beach_File);
}
}
}
khác
{
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 语言的初学者编程。谢谢!

câu trả lời hay nhất

在此声明中,

 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/

26 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress