Thread: Error when creating file

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

    Error when creating file

    I'm trying to create a program that creates a log file and enters in information.
    I created the following function to just create the file. However, whenever I run it, the program results in an error. When I put the file name directly in the fopen command and comment everything else, it works fine. The variable "Drive" that I send is either "C" or "D".

    Code:
    void BuildLogFile(char *drive)
    {
    // Build path to log
    	
    	char logFileName[] ="";
    	
    	strcpy(logFileName, drive);
     	strcat(logFileName, ":\\Test\\ppbatch_3.log");
    		
    	fpLogFile = fopen(logFileName,"w");
    
    	fclose(fpLogFile);
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	char logFileName[] ="";
    How much space is there in the above variable?

    How long is your filename?

    And also, considering which forum you posted in, why not use std::string and the c_str() 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.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Is drive defined as a CHAR or as an ARRAY? If it's a CHAR, you can't use string functions on it.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Since you're trying to write into a 1 byte literal string, it should be blowing up.

    To re-write this in C++ instead of the C that it is now:
    Code:
    void BuildLogFile( const char  drive )
    {
        std::string logFileName( drive );
        logFileName += ":\\Test\\ppbatch_3.log";
        std::ofstream logFile( logFileName );
    }

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Small corrections:
    no string constructor that takes just a char,
    no ofstream constructor that takes a string.

    Code:
    void BuildLogFile( const char  drive )
    {
        std::string logFileName( 1, drive );
        logFileName += ":\\Test\\ppbatch_3.log";
        std::ofstream logFile( logFileName.c_str() );
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM