Creating Log File

This is a discussion on Creating Log File within the C Programming forums, part of the General Programming Boards category; Hello, I'm trying to write a function that will create a log file. I have code in here to get ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    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
    Katy, Texas
    Posts
    2,309
    You asked this already, and several people have offered suggestions and questions that you have not responded to as of yet.

    Error when creating file

  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
    21,401
    Threads merged. Thanks, matsp.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Katy, Texas
    Posts
    2,309
    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
    Katy, Texas
    Posts
    2,309
    I thought laserlight was thanking you (matsp) for my suggestion. Creating Log File

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    matsp requested for the merge. The alternative was to close the other thread.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. Creating Log File
    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
    Katy, Texas
    Posts
    2,309
    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, 04: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, 01:46 PM

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