Why is my text file blank?

This is a discussion on Why is my text file blank? within the C Programming forums, part of the General Programming Boards category; Code: FILE * Log; fopen("blah.log", "a"); fprintf(Log, "blah.log", "Test"); fclose(Log); Exactly what the sibject says. I'm getting nothing in blah.log. ...

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    Why is my text file blank?

    Code:
    	FILE * Log;
    	fopen("blah.log", "a");
    	fprintf(Log, "blah.log", "Test");
    	fclose(Log);
    Exactly what the sibject says. I'm getting nothing in blah.log.

    BTW, I need to use append mode......
    The world is waiting. I must leave you now.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > fprintf(Log, "blah.log", "Test");


    You shouldn't have the second , in there.

    fprintf( Log, "blah.logTest" );

    Or

    fprintf( Log, "blah.log" "Test" );

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

  3. #3
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    well i'll assume your code was just ment as an example and not actually what you are running.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        FILE * Log;
    
        // you must open the file path correctly(eg."C:\\mypath\\path\\file.bla") 
        // Note: the double backslash '\\'
        Log = fopen("C:\\blah.log", "a");
        fprintf(Log, "blah.log%s", "Test");
        fclose(Log);
    
        return 0;
    }
    quzah,

    that infact should cause no problem the "blah.log" should be written and the "Test" should be ignored.
    Last edited by no-one; 02-13-2002 at 03:45 PM.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Quzah,
    You took me back to square one lol. Thanks for the effort.

    no-one,

    Code:
    int main()
    {
    	FILE * Log;
    	Log = fopen("blah.log", "a");
    	fprintf(Log, "Test");
    	fclose(Log);
    	return 0;
    }
    I have a file pointer with Log.
    *NEW* I'm _using_ the file pointer Log to open a file called "blah.log" ( or to create one if none exist ).
    I'm using the file pointer log to -direct- the printing instructions to the "currently" associated file, blah.log
    I then close the file pointer Log.

    I'd then be able to assign the file pointer Log to a different text file if I wished........

    I had the file pointer log setup, but wasn't using it!
    The world is waiting. I must leave you now.

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    so the problem is solved?

    it took me a minute to see that Log was not being assigned also.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  6. #6
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    yes it is

    It's solved, and now with Cprogramming.com's tutorials, I am refreshing a little bit of my "time" command skills....

    Code:
    int main()
    {
    	// Setup clock / timer
    	// Setup log file
    	time_t hold_time;
      	hold_time=time(NULL);
      	FILE * Log;
      	
      	// Open log file
      	// Write the date & time into the log file along with "Menu opened"
      	// to -log- the time that menu was first opened.
    	Log = fopen("Menu.log", "a");
    	fprintf(Log, "%s", ctime(&hold_time));
    	fprintf(Log, "Menu opened\n\n");
    	
    	// Pause the program flow to allow a few seconds to go by
    	getch();
    	
    	// "Refresh" the timer so when "Menu closed" was logged
    	// it will also have the updated time.
    	hold_time=time(NULL);
    	fprintf(Log, "%s", ctime(&hold_time));
      	fprintf(Log, "Menu closed");
    	fclose(Log);
    	return 0;
    }
    This all works perfectly, just how I want it. The program opens and immediately upon opening it opens that log file & logs the time this program was activated so-to-speak. I put a getch(); command in there just to pause the program so that a few seconds will pass by. This way when the time of closing was logged, and the clock is updated, we will visually see the time this program was running inside that log file.

    Is there a way to convert military time to standard time?
    00:01:05 = 1:05am?
    The world is waiting. I must leave you now.

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    heres a stripped down example from the MSDN

    Code:
    #include <time.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/timeb.h>
    #include <string.h>
    
    void main()
    {
        char ampm[] = "AM";
        time_t ltime;
        struct tm *today, *gmt;
    
        gmt = gmtime( &ltime );
    
        /* Convert to time structure and adjust for PM if necessary. */
        today = localtime( &ltime );
        if( today->tm_hour > 12 )
        {
           strcpy( ampm, "PM" );
           today->tm_hour -= 12;
        }
        if( today->tm_hour == 0 )  /* Adjust if midnight hour. */
            today->tm_hour = 12;
    
        /* Note how pointer addition is used to skip the first 11 
         * characters and printf is used to trim off terminating 
         * characters.
         */
        printf( "12-hour time:\t\t\t\t%.8s %s\n",
           asctime( today ) + 11, ampm );
    
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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