我试图为下面的代码缩进我的字符串输出,但由于某种原因,我的变量一直被从文件中提取出来,其中包含了范围很长的噪声或空间(我不确定)。


这是我的密码:
#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语言的初学者编程,谢谢!
发布于 2015-02-20 04:39:16
在这份声明中,
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填充空格。它会给你格式化的打印。
https://stackoverflow.com/questions/28621567
复制相似问题