Hi ,
This is my first post in the forum. I am reading some data from the file and printing it in certain format on the screen. I guess reading part is fine but printing is somewhat messed up. Its tv guide program. The data file contains channels and the program timings. Following is my program
Following is the file i am reading fromCode:#include <stdio.h>
void getprograminfo(char *, char *, char *);
char * getline(FILE *);
int main(int argc, char *argv[])
{
FILE *fp;
int channels;
char channelname[20];
char *line;
char *linep;
int i,a,len;
char time[50];
char program[50];
char line1[1024];
char line2[2000];
int tlen,plen;
if (argc < 2)
{
printf(" USAGE: %s <filename>\n",argv[0]);
exit(0);
}
if ((fp=fopen(argv[1],"r")) == NULL)
{
printf("can not open the file\n");
exit(0);
}
fscanf(fp,"%d",&channels);
printf("Total Channels: %d\n",channels);
while (!feof(fp))
{
memset(channelname,'\0',20);
GETCHANNEL:
linep=getline(fp);
if (linep!=NULL)
{
if ((line=strstr(linep,"=")) != NULL)
{
//printf("linep1: %s\n",linep);
strcpy(channelname,++line);
printf("channel: %s\n",channelname);
}
} else {
goto GETCHANNEL;
}
//printf("linep2: %s\n",linep);
//printf("Check 1\n");
memset(line1,'\0',sizeof(line1));
memset(line2,'\0',sizeof(line2));
for (i=0;i<5;i++)
{
memset(linep,'\0',sizeof(linep));
memset(time,'\0',sizeof(time));
memset(program,'\0',sizeof(program));
if ((linep=getline(fp)) == NULL)
{
continue;
}
//printf("linep: %s\n",linep);
getprograminfo(linep,time,program);
//printf("time: %s ",time);
//printf("program: %s\n",program);
if (i == 0)
{
sprintf(line1,"Time\t\t%s",time);
sprintf(line2,"Program\t\t%s",program);
//printf("tlen: %d plen: %d\n",strlen(line1),strlen(line2));
} else {
strcat(line1,"\t");
strcat(line1,time);
strcat(line2,"\t");
strcat(line2,program);
//printf("tlen: %d plen: %d\n",strlen(line1),strlen(line2));
}
}
printf("%s\n%s\n",line1,line2);
if (strlen(line1) > strlen(line2))
{
len = strlen(line1);
} else {
len = strlen(line2);
}
for (a=0;a<len;a++)
{
printf("-");
}
printf("\n");
}
fclose(fp);
return 0;
}
void getprograminfo(char *line, char *time, char *program)
{
int i,j;
char tword[50];
char pword[50];
i=j=0;
while (i<strlen(line))
{
if (line[i] == ' ')
{
tword[j]='\0';
strcpy(time,tword);
i++;
strcpy(program,line+i);
j=0;
return;
} else {
tword[j]=line[i];
i++;
j++;
}
}
}
char * getline(FILE *fp)
{
static char line[100];
char ch;
int i=0;
memset(line,'\0',sizeof(line));
while ((ch=fgetc(fp))!='\n')
{
line[i]=ch;
i++;
}
line[i+1]='\0';
//printf("line length: %d\n",strlen(line));
if (strlen(line) == 1)
{
return NULL;
} else {
return line;
}
}
Following is the program output.Quote:
8
=ABC
18.00 Dr Who: Underworld
18.30 Collectors
19.00 News
19.30 The 7.30 Report
20.00 Catalyst
=Seven
18.00 Seven News
18.30 Today Tonight
19.00 Home and Away
19.30 My Restaurant Rules
20.30 **END
Any guess, what am i doing wrong?Quote:
[root@linux-outside snoopy]# ./tvguide tc.txt
Total Channels: 8
channel: ABC
Time 18.00 18.30 19.00 19.30 20.00
Program Catalyst Report Underworld
-------------------------------------------------------------------------
channel: Seven
Time 18.00 18.30 19.00 19.30 20.30
Program **ENDstaurant Rules
------------------------------------------------------------------------------
Thanks,