Thread: My log file player; Hit the brick wall

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    17

    My log file player; Hit the brick wall

    Okie dokie. I've been working on my log file player. I've run into a problem. Here is the code I have. However it doesn't print off the log file. It just loads the log file, then jumps to the last printf. Also I get implicited delcaration of 'pause'. Not sure how to get rid of that.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <dos.h>
    
    
    
    int main(void)
    {
    
    	FILE * log_file;
    	char log_file_name[20];
    	char log;
    
    
    	printf("Enter the name of the log to be played.\n");
    	gets(log_file_name);
    	if (log_file_name != NULL)
    	{
    		printf("Loading log file.\n");
    	}else{
    		printf("Please enter a log file.\n");
    		gets(log_file_name);
    		return 0;
    	}
    
    
    
    	while (1){
    		log = fgetc(log_file);
    		if(log !=EOF ){
    			printf("%c", log);
    		}
    		else if (log == '<')
    		{
    			pause(1);
    			continue;
    	}else{
    
    		break;
    	}
    }
    
    	printf("Log completed. Thank you for watching. Come back next time.\n");
    	fclose(log_file);
    	return 0;
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    char log;

    log = fgetc(log_file);
    if(log !=EOF ){
    'log' should be an integer, or it can never equal EOF.


    if(log !=EOF ){
    printf("%c", log);
    }
    else if (log == '<')
    Your second if check will never happen. "If this is not EOF, do this, otherwise (if it is, and) if it is '<' ..."

    This cannot possibly happen. 'log' cannot be EOF and '<' at the same time.

    Also I get implicited delcaration of 'pause'. Not sure how to get rid of that.
    There is no 'pause' function in any of the headers you've included. Furthermore, there is no ANSI pause function.

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

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    Well that sheds some light into my problem. Thanks. I'm not sure where I picked up the pause function. But i've used it for when I want to stop the program for a certain period of time. Any ideas on what else I could use instead of it?

  4. #4
    Unregistered
    Guest
    I can't see where you opened the file

    should this
    Code:
    if (log_file_name != NULL)
    be this
    Code:
    if ((log_file = fopen(log_file_name, "rt")) != NULL)
    assuming it's a text file.
    If you want to halt the program you could use getch()
    or if you actually want to pause it for a specific period of time then you could write your own pause function.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    17
    God I feel dumb now. Heh, that was the problem. I wasn't opening the stupid file. It works fine now. It just scrolls way to fast to read. I've got no idea where to start in writing a delay function. I'll just check around the 'net to see if I can find one. Thanks for the help.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This should work. Although it only displays 400 chars at a time.

    Code:
    int main(void)
    {
    
    	FILE * log_file = NULL;
    	char log_file_name[20], *buffer, *current_pos, holder;
    	int log, fsize, counter;
    
    
    	while(log_file == NULL) {
    		printf("Enter the name of the log to be played.\n");
    		gets(log_file_name);	
    		log_file = fopen(log_file_name, "rb");
    		if(!log_file)
    			printf("Invalid filename.\n");
    	}
    
    	fseek(log_file, 0, 2);
    	fsize = ftell(log_file);
    	
    	fseek(log_file, 0, 0);
    	buffer = (char *)malloc(fsize);
    	
    	if(!buffer) {
    		printf("Out of memory!\n");
    		return 1;
    	}
    	
    	fread(buffer, fsize, 1, log_file);
    	fclose(log_file);
    	
    	for(current_pos = buffer, counter = 0; counter < (fsize/400)+((fsize%400)?1:0); counter++, current_pos+=400) {
    		holder = current_pos[400];
    		current_pos[400] = '\0';
    		
    		printf("%s", current_pos);
    		current_pos[400] = holder;
    		getch();
    	}
    
    	printf("Log completed. Thank you for watching. Come back next time.\n");
    	free(buffer);
    	return 0;
    
    }
    Last edited by master5001; 07-27-2002 at 01:59 AM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If all your doing is copying a file to stdout, you really only need something like this:
    Code:
    #define LINES 20
    
    void FileDisplay(FILE *ifp)
    {
        char    buf[BUFSIZ];  /* Some suitable size */
        int     i = 0;
    
        while (fgets(buf, sizeof buf, ifp))
        {
            if (fputs(buf, stdout) == EOF) break;
            if ((i = (i + 1) % LINES) == 0)
            {
                printf("...more...");
                getch();  /* Assumes you have getch().  If not, use a substitute. */
            }
        }
        /* Do error checking here if you want */
    }
    
    .... /* somewhere else ...*/
    myfp = fopen(.....);
    FileDisplay(myfp);
    ....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM