log and restore

This is a discussion on log and restore within the C Programming forums, part of the General Programming Boards category; I need to log what I type while the program is running. this is saved in a text file. Then ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    log and restore

    I need to log what I type while the program is running.
    this is saved in a text file.
    Then next time I run the program I have the choice of restoring what is stored in the log file.

    This a simple version of my code.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void Log();
    void Save();
    void Del();
    void Dir();
    
    typedef struct {
    	char n;
    	struct files *next;
    }files;
    
    struct root {
    	files files[50];
    	int count;
    }root;
    
    static int h;
    
    int main() {
    
    int choice;
    
    root.count = 0;
    h = root.count;
    
    Log();
    
    do {
             printf("\nPlease select an option (1-4):\n");
             printf("1) Save\n");
             printf("2) Del\n");
             printf("3) Dir\n");
             printf("4) Quit\n");
          
             scanf("%d",&choice);
          
             switch (choice) {
                case 1:
                   if (h <50)
                      Save();
                   else 
                      printf("Directory Full\n");
                   break;
                case 2:
                   Del();
                   break;
                case 3:
                   if (h == 0)
                      printf("No Files Found\n");
                   else
                      Dir();						
                   break;
                case 4:
                   break;
                default:
                   printf("Invalid choice\n");
                   break;
             }
          } while(choice != 4);
    fclose(outfile);
    return 0;
    }
    
    void Log() {
    	char filename[25];
    	printf("Enter Log filename -> ");
    	scanf("%s", filename);  
     	outfile = fopen(filename, "a");
    }
    
    void Save() {
    
    	printf("\nEnter a char: ");
    	scanf("%s",&root.files[h].n);
        
    	h++;
    	root.count = h;
    }
    
    void Del() {
    	int i;
    	char c;
    	char hex = 0xE5;
    	printf("\nEnter a char to del: ");
    	scanf("%s",&c);
    	for(i=0; i < h; i++) {
    		if (root.files[i].n == c) {
    			root.files[i].n = hex;
    		}
    	}
    }
    
    void Dir() {
    	int i;
    	char hex = 0xE5;
    	for(i=0; i < h; i++) {
    	                 if (root.files[i].n != hex){
    			printf("%c\n",root.files[i].n);
    		}
    	}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you want to log everything you input, you should create an input function that is used for all input in your program. Ideally, you'd just use something which line buffers all input, and writes that input to a file.

    If you really want to capture everything, you'd need to do it on a keystroke basis. This is unportable, since there is no standard for reading escape sequences and the like on a keyhit basis.

    Anyway, create an input function, dump its contents to file, send the input some place to be parsed and handled.

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

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    Can you be more specific.

    you don't have to give exact code.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    <type> inputFunction( const char *prompt, FILE *logfile )
    {
        char buf[BUFSIZ] = {0};
    
        fprintf( stdout, "%s", prompt );
        fgets( buf, BUFSIZ, stdin );
        fprintf( logfile, "%s", buf );
    
        ...do something with buf...
        ...return whatever, optional...
    }
    Here is a quick example. This one function would be called like:

    inputFunction( "Enter your name: ", logfile );

    Which would then display the prompt to the user (Enter your name: ), read from the keyboard, log that input, and then you'd process said input.

    Like I said, it's a quick hack. You'd want to do error checking and the like, but it should get you started.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 10:33 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM

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