Thread: Creating Log File

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    Creating Log File

    Hello,

    I'm trying to write a function that will create a log file. I have code in here to get the current system time and day and doing a strcat to add it to the file name. However, when I run it, it doesnt create the file. When I do a printf, I can see the file name. But it never creates the file. I appreciate any help.

    Code:
    void BuildLogFile(char *drive)
    {
    // Build path to log
    	
    	char logFileName[100] ="";
    	char s[30];
    	size_t i;
    	struct tm tim;
    	time_t now;
    	
    	now = time(NULL);
    	tim = *(localtime(&now));
    	i = strftime(s,30,"%m%d%I%M\n",&tim);
    
    	strcpy(logFileName, drive);
     	strcat(logFileName, ":\\Test\\GrpClean_");
    	strcat(logFileName, s);
    	strcat(logFileName, ".log");
    	
    	fpLogFile = fopen(logFileName,"w");
    
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If fopen fails, then obviously there won't be a file. So check if it fails.
    Is this C or C++? This all appears just C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why not add
    Code:
    if (fpLogFile == NULL) {
        perror("Unable to open file");
    }
    to the end of that function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    19

    Creating Log File

    I'm trying to write a function to create a log file that includes the current system time in the file name. When I run this code, I have an error code of "invalid argument" for fopen. When I try to print the file name, its shows up correctly. Also, when I try to replace the system time with "123" the file creates fine. I appreciate any help.

    Code:
    void BuildLogFile(char *drive)
    {
    // Build path to log
    	
    	char logFileName[100] ="";
    	char s[30];
    	size_t i;
    	struct tm tim;
    	time_t now;
    	
    	now = time(NULL);
    	tim = *(localtime(&now));
    	i = strftime(s,30,"%m%d%I%M\n",&tim);
    
    	printf("%s", s);
    	strcpy(logFileName, drive);
     	strcat(logFileName, ":\\Test\\GrpClean_");
    	strcat(logFileName, s);
    	strcat(logFileName, ".log");
    	
    	printf("%s",logFileName);
    	fpLogFile = fopen(logFileName,"w");
    
    	if (fpLogFile == NULL) 
    	{
        perror("Unable to open file");
    	}
    
    
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Got it:
    Code:
    i = strftime(s,30,"%m%d%I%M\n",&tim);
    Is newline a valid character for filenames?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You asked this already, and several people have offered suggestions and questions that you have not responded to as of yet.

    http://cboard.cprogramming.com/showthread.php?t=98467

  7. #7
    Registered User Tommo's Avatar
    Join Date
    Jun 2007
    Location
    Scotland
    Posts
    101
    Code:
    fpLogFile = fopen(logFileName,"w");
    Have you declared fpLogFile globally? If not, you need to declare it somewhere.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Threads merged. Thanks, matsp.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by laserlight View Post
    Threads merged. Thanks, matsp.
    You're welcome Salem.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    You're welcome Salem.
    s/Salem/laserlight/ ??
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I thought laserlight was thanking you (matsp) for my suggestion. http://cboard.cprogramming.com/showp...18&postcount=6

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    matsp requested for the merge. The alternative was to close the other thread.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    I thought laserlight was thanking you (matsp) for my suggestion. http://cboard.cprogramming.com/showp...18&postcount=6
    Yes, I think Laserlight did, but you said thanks to Salem!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by matsp View Post
    Yes, I think Laserlight did, but you said thanks to Salem!

    --
    Mats
    Purposefully!

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. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM