我想从文件中获取输入并将其完全显示在屏幕上。
我从文件中获取输入,其中包含:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
#include<ctype.h>
char garbage[12][6];
char courseIdentifier[12][6],prereqIdentifier[12][5][6];
char semOffered[12];
bool done[12];
int main(void)
{
FILE *fp;
fp = fopen("graduate.in","r");
int n,m,p[12];
int i,j,k;
fscanf(fp,"%d %d",&n,&m);// take n,m
while(n != -1)
{
for(i=0;i<n;i++) //the list of offered courses
{
fscanf(fp,"%s",garbage[i]);
}
for(i=0;i<n;i++)
{
fscanf(fp,"%s %c %d",courseIdentifier[i],&semOffered[i],&p[i] );//the name of course, sem,no. of prereqs
for(j=0; j < (p[i]) ;j++)
{
fscanf(fp,"%s ",prereqIdentifier[i][j]);
}
}
for(i=0;i<n;i++)
{
printf("%s %c %d ",courseIdentifier[i],semOffered[i],p[i]);
for(j=0; j < (p[i]) ; j++ )
{
printf("%s ",prereqIdentifier[i][j]);
}
printf("%s\n");
}
fscanf(fp,"%d %d",&n,&m);// take n,m
printf("\n\n");
}
fclose(fp);
return 1;
}
输出不正确。我在哪里做错了,使用数组或fscanf()
?
答案 0 :(得分:2)
这一行:
printf("%s\n");
将打印一些随机垃圾 - 基于特定寄存器中的任何字符串。在您的情况下,它会一直打印出courseIdentifier[i]