Thread: Append a temp file to a log file

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    11

    Append a temp file to a log file

    hello, i am faced with another problem that I am not sure how to tackle:
    Code:
    FILE *file = tmpfile();
    FILE *logfile;
    Code:
    time_t time;
    fprintf(file, "%s", ctime(&time));
    Code:
    logfile = fopen("logfile.txt","a");
    ////////////////
    Append here
    ///////////////////////
    Code:
    fclose(file);
    fclose(logfile);
    These are little snippets of my code for my basic direction. Here I am printing the time with some commands entered in to a temp file. After the session is complete I will append the temp file to a log file. Any good way to append the temp to logfile?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You've got the right idea opening the log file for append and the tmp file for read...

    Now you can use fread() and fwrite() to transfer the file in chunks.

    Close both when you're done.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    Quote Originally Posted by CommonTater View Post
    You've got the right idea opening the log file for append and the tmp file for read...

    Now you can use fread() and fwrite() to transfer the file in chunks.

    Close both when you're done.
    From the looks of these functions it does not look like text, uses binary?

    Here is what I tried and i could open the logfile.txt
    Code:
    FILE *file = tmpfile();
    FILE *logfile;
    Code:
    int buffer = 256; 
    logfile = fopen("logfile.txt","a");
    fread(&buffer,sizeof(buffer),1,file);  
    fwrite(&buffer,sizeof(buffer),1,logfile);
    Code:
    fclose(file);
    fclose(logfile);

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, you're opening the logfile... now you need to open the tmp file for read (same process...)

    To read and write you need a character buffer to hold the data, you can't point it at an integer.

    Code:
    char buffer[256];   // to hold data
    int ChrRead;      // to hold data counts
    
    // open and check the logfile
    logfile = fopen("logfile.txt","a");  
    if (logfile == 0)
      { puts("Error opening logfile\n");
         exit(1); }
    
    // open temp file here
    
    //  transfer the data
    do
     { ChrRead = fread(buffer,sizeof(char),sizeof(buffer),file);  
         fwrite(buffer,1,ChrRead,logfile); }
    while (ChrRead > 0);
    
    // close files here

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    Ok, you're opening the logfile... now you need to open the tmp file for read (same process...)
    Not sure what you mean but I have opened a temp file and have put some data in it. I never closed it till the end.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Randellk02 View Post
    Not sure what you mean but I have opened a temp file and have put some data in it. I never closed it till the end.
    Your posted code doesn't show that. Pleas post an update of your complete code, lets see where we're at...

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    Quote Originally Posted by CommonTater View Post
    Your posted code doesn't show that. Pleas post an update of your complete code, lets see where we're at...
    This is my main code where i where i am doing the appending.
    Code:
      FILE *file = tmpfile();
      FILE *logfile;
      pid_t pid;
      int pd[2];
      pipe(pd);
      pid = fork();
      if(pid == -1){
        fprintf(stderr, "Pipe Error");
        exit(1);
      }
      // printf("1\n");
        if(pid != 0){ // Parent Process writes
            printf("In Parent\n");
    
          	while (fgets(input, sizeof(input), stdin)) {
                printf("Parent received: %s", input);
                strcpy(in,input); // saves previous command in
       	    strcpy(in2,input); // saves full command in2
                start = append(start, input);
                stripcrlf(input);
                parse(input);
                printf("\n$ ");
    
                if(strcmp(input,"history") == 0)
         	        displayRecentCommands(start);
                if(input[0] == '!')
                    getCommand(start, input, in);
    
                time_t time;
    	    fprintf(file, "%s", ctime(&time));
    
    	    write(pd[1],input, sizeof(input));
                printf("parent end\n");
          	}
        }
        else		// Child Process
        { 
          	read(pd[0],temp,sizeof(temp));
          	while(strcmp(temp,"stop") != 0){		// 4 is ctrl+d ascii
     	   printf("Im in the Child");
               printf("Input Received: %s\n", temp);
               read(pd[0],temp,sizeof(temp));
    
               int i = 1;
        	   fprintf(file, "%d. Command: %s", i++, temp);
         	}
         
          // exec only returns if error
          //fprintf(stderr, "an error with exec occured.");
          // exit(1);
          printf("child end\n");
        }    
        char buffer[100];
        int Char;
        write(pd[1],"stop", sizeof("stop"));    // send "stop" to end child process 
      
        logfile = fopen("logfile.txt","a");
        Char = fread(buffer,sizeof(char),sizeof(buffer),file);
        while(Char > 0){ 
        	fwrite(buffer,1,Char,logfile);
            Char = fread(buffer,sizeof(char),sizeof(buffer),file);
        }
        fclose(file);
        fclose(logfile);
    
        savedata(start);
        displaydata(start);
        freedata(start);
        
        return 0;
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... is it working? What problems are you having?

    I don't think it needs to be as complex as it is... certainly I see no need of parent and child processes.

    The only comment I have is with the use of Char as a variable name... I'd suggest you pick something a) more descriptive and b) not like a type or function name... Perhaps CharsRead or such...

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    Quote Originally Posted by CommonTater View Post
    Ok... is it working? What problems are you having?

    I don't think it needs to be as complex as it is... certainly I see no need of parent and child processes.

    The only comment I have is with the use of Char as a variable name... I'd suggest you pick something a) more descriptive and b) not like a type or function name... Perhaps CharsRead or such...
    The problem now is when I open my logfile nothing is in it. I use the child process to save the commands to a temp file and the parent executes the command. My code is also running twice so im guessing I need to kill the child right after the parent/child code?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    When this started you just wanted to tack a temp file onto the end of a log file. Now it's a lot more complex and I don't think I quite understand what you're up to...

    Perhaps one of the others can assist you better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tmpnam() : permission denied when try to open temp file
    By happyclown in forum C Programming
    Replies: 3
    Last Post: 03-16-2009, 11:48 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM