File I/O program

This is a discussion on File I/O program within the C Programming forums, part of the General Programming Boards category; Hi , This is my first post in the forum. I am reading some data from the file and printing ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    134

    File I/O program

    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

    Code:
    #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 file i am reading from

    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
    Following is the program output.

    [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
    ------------------------------------------------------------------------------
    Any guess, what am i doing wrong?

    Thanks,

  2. #2
    customized user title!
    Join Date
    Mar 2005
    Posts
    24
    You're not including <string.h> for your str* functions and memset. Also, refer here for why using !feof(fp) in a loop is wrong.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Okey Thanks, changed it as per the link. Still the same problem. Any help.??

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.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[100];
    	int i,a,len;
    	char time[50];
    	char program[50];
    	char line1[1024];
    	char line2[2000];
    	char *temp;
    	int tlen,plen,first;
    
    	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);
    	
    	i = 0;
    	first = 1;
    	
    	memset(line1,'\0',sizeof(line1));
    	memset(line2,'\0',sizeof(line2));
    
    	while ((fgets(linep,sizeof(linep),fp))!=NULL)
    	{
    		memset(channelname,'\0',20);		
    		linep[strlen(linep)-1]= '\0';
    
    		if ((line=strstr(linep,"=")) != NULL)
    		{
    			if (!first)
    			{
    				
    				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");
    				memset(line1,'\0',sizeof(line1));
    				memset(line2,'\0',sizeof(line2));
    				i=0;
    				
    			} 	
    			first = 0;				
    			strcpy(channelname,++line);
    			printf("channel: %s\n",channelname);
    			
    		} else {
    			memset(time,'\0',sizeof(time));
    			memset(program,'\0',sizeof(program));
    			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));
    			}
    			i++;
    		}
    	}
    
    	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')
    	{
    		if (ch == EOF)
    		{
    			return NULL;
    		} else {
    			line[i]=ch;
    			i++;
    		}
    	}
    	line[i+1]='\0';
    	//printf("line length: %d\n",strlen(line));
    
    	if (strlen(line) == 1)
    	{
    		return NULL;
    	} else {
    		return line;
    	}
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what output were you expecting?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    The current output of the program is as below

    Total Channels: 8
    channel: ABC
    Time 18.00 18.30 19.00 19.30 20.00
    Program Catalyst Report Dr Who: Underworld
    -------------------------------------------------------------------------------
    channel: Seven
    Time 18.00 18.30 19.00 19.30 20.30
    Program **ENDstaurant Rules
    ----------------------------------------------------------------------------------
    As you can see, the program line is messed up while the time line is printed correctly. I want program line to be printed as time line.

    Currently it seems it overwriting the data.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I didn't ask what the current output was. I asked you to show me what you want it to be. You already showed what it was up above. You never told us what the program is supposed to output. Kinda hard to fix it when we don't know what it's supposed to look like.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Quote Originally Posted by quzah
    I didn't ask what the current output was. I asked you to show me what you want it to be. You already showed what it was up above. You never told us what the program is supposed to output. Kinda hard to fix it when we don't know what it's supposed to look like.

    Quzah.
    Hi,

    I guess, i was clear when I said my program line should look similar to my time line. Currently all the program names are getting overlapped with one another.

    Anyway, following is what my program output should look like. Not showing the all time slots due to space constraints. This isn't any homework assignment and I am not a college student. . Just trying to develop my skills in C programming. I am not a full time programmer either.

    Total Channels: 8
    channel: ABC
    Time 18.00 18.30 19.00 19.30
    Program Dr Who: Underworld Collectors News The 7.30 Report
    -------------------------------------------------------------------------------
    channel: Seven
    Time 18.00 18.30 19.00
    Program Seven News Today Tonight Home and Away
    ----------------------------------------------------------------------------------

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    My output (within [code][/code] tags to preserve whitespace):
    Code:
    Total Channels: 8
    channel: ABC
    Time			 18.00	 18.30	 19.00	 19.30	 20.00
    Program			 Dr Who: Underworld	 Collectors	 News	 The 7.30 Report	 Catalyst
    --------------------------------------------------------------------------
    channel: Seven
    Time		18.00	 18.30	 19.00	 19.30	 20.30
    Program		Seven News	 Today Tonight	 Home and Away	 My Restaurant Rules	 **END
    -----------------------------------------------------------------------------
    [edit]This is a lazy change to use sprintf to keep each "slot" the same size.
    Code:
             if (i == 0)
             {
                sprintf(line1,"Time    %-15.15s ",time);
                sprintf(line2,"Program %-15.15s ",program);
                //printf("tlen: %d plen: %d\n",strlen(line1),strlen(line2));
             } else {
                char buffer[sizeof line1];
                strcpy(buffer, line1);
                sprintf(line1, "%s %-15.15s ", buffer, time);
                strcpy(buffer, line2);
                sprintf(line2, "%s %-15.15s ", buffer, program);
                //printf("tlen: %d plen: %d\n",strlen(line1),strlen(line2));
             }
    The output is then like this.
    Code:
    Total Channels: 8
    channel: ABC
    Time                     18.00            18.30            19.00            19.30            20.00           
    Program                  Dr Who: Underwo  Collectors       News             The 7.30 Report  Catalyst        
    -------------------------------------------------------------------------------------------------------------
    channel: Seven
    Time    18.00            18.30            19.00            19.30            20.30           
    Program Seven News       Today Tonight    Home and Away    My Restaurant R  **END           
    --------------------------------------------------------------------------------------------
    Last edited by Dave_Sinkula; 04-05-2005 at 04:25 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Hi,

    I hate to say, but its still not working. Is this something compiler specific. If something works with you (donno which complier you used though) doesn't work with my compiler. I use gcc complier on linux.

    Following is the code, incorporating the comments in last post from "Dave_Sinkula".
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.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[100];
    	int i,a,len;
    	char time[100];
    	char program[100];
    	char line1[1024];
    	char line2[1024];
    	char buffer[sizeof(line2)];
    	int first;
    
    	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);
    	
    	i = 0;
    	first = 1;
    	
    	memset(line1,'\0',sizeof(line1));
    	memset(line2,'\0',sizeof(line2));
    
    	while ((fgets(linep,sizeof(linep),fp))!=NULL)
    	{
    		memset(channelname,'\0',20);		
    		linep[strlen(linep)-1]= '\0';
    
    		if ((line=strstr(linep,"=")) != NULL)
    		{
    			if (!first)
    			{
    				
    				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");
    				memset(line1,'\0',sizeof(line1));
    				memset(line2,'\0',sizeof(line2));
    				i=0;				
    			} else {	
    				first = 0;
    			}
    			strcpy(channelname,++line);
    			printf("channel: %s\n",channelname);
    			
    		} else {
    			memset(time,'\0',sizeof(time));
    			memset(program,'\0',sizeof(program));
    			getprograminfo(linep,time,program);
    			
    			if (i == 0)
    			{
    				sprintf(line1,"Time    %-15.15s ",time);
    				sprintf(line2,"Program %-15.15s ",program);
    			} else {
    				
    				strcpy(buffer, line1);
    				memset(line1,'\0',sizeof(line1));
    				sprintf(line1, "%s %-15.15s ", buffer, time);
    				strcpy(buffer, line2);
    				memset(line2,'\0',sizeof(line2));
    				sprintf(line2, "%s %-15.15s ", buffer, program);
    			}
    			i++;
    		}
    	}
    
    	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];
    	
    	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')
    	{
    		if (ch == EOF)
    		{
    			return NULL;
    		} else {
    			line[i]=ch;
    			i++;
    		}
    	}
    	line[i+1]='\0';
    	//printf("line length: %d\n",strlen(line));
    
    	if (strlen(line) == 1)
    	{
    		return NULL;
    	} else {
    		return line;
    	}
    }
    And here is the output (in code tags to preserve indentation)
    Code:
    Total Channels: 8
    channel: ABC
    Time                     18.00            18.30            19.00            19.30            20.00           
                The 7.30 Report  Catalystrwo  Collectors
    -------------------------------------------------------------------------------------------------------------
    channel: Seven
    Time    18.00            18.30            19.00            19.30            20.30           
              aurant R  **END
    --------------------------------------------------------------------------------------------

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Well, i'll be damned. It worked just fine on my Windows PC. I have no clue whatsoever. I am using digital mars C compiler. Following was the program output

    Code:
    Total Channels: 8
    
    channel: ABC
    ============
    Time    18.00            18.30       19.00       19.30       20.00
    Program Dr Who: Underwo  Collectors  News        The 7.30 R  Catalyst
    ------------------------------------------------------------------------
    
    channel: Seven
    ==============
    Time    18.00            18.30       19.00       19.30       20.30
    Program Seven News       Today Toni  Home and A  My Restaur  **END
    ------------------------------------------------------------------------

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. File processing program not workin...
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-30-2002, 10:25 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21