Thread: Specify a path for write file

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    39

    Post Specify a path for write file

    Hi
    i'm having this function:

    Code:
    int logger(char *log_prefix,char *buffer)
    {
    	printf("Logger: Logging Command...\n");
    	time_t now;
    	time(&now);
    	FILE *file;
    	file = fopen("log.txt","a+"); // apend file (add text to a file or create a file if it does not exist.
    	//writes the "buffer" to the txt file and current time and log prefix ( either sent or received).
    	fprintf(file,"Time/Date:%s# Command %s = %s\n",ctime(&now), log_prefix,buffer);
    	fclose(file);
    	printf("Logger: Command Logged, Check log.txt for more details.\n");
    	return 0;
    }
    this function creates a txt file called log.txt, in the path that the .c file exist. how can i specify a path in which the file will be created?

    this code

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    By specifying the filename you want created. If you want "../other_dir/log.txt", then say so.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    39
    Yes...

    Code:
    file = fopen("./path/log.txt","a+");
    this is what i needed to change, thanks for the help

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. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

Tags for this Thread