Thread: Need to specify the PATH in C

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    7

    Need to specify the PATH in C

    I using the system command to run an executable file.
    for that I need to specify path for the input file and then the output file.
    Right now I'm specifying the path manually i.e. the path according to my system. But I need to provide this to the professor when he tries to run it. He uses a location so. Can you suggest me what exactly can I do for specifying dynamic path in C

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define PROGNAME "okay"
    
    int main(int argc, const char *argv[]) {
    	char *path;
    	if (argc < 2) {
    		puts("Argument required.");
    		return -1;
    	}
    
    	path = malloc(strlen(argv[1]) + strlen(PROGNAME) + 1);
    
    	sprintf(path, "%s/%s", PROGNAME, argv[1]);
    	puts(path);
    
    	free(path);
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for that I need to specify path for the input file and then the output file.
    So what's wrong with
    ./myprog.exe /here/is/input.txt /there/goes/output.txt


    And MK27's malloc is off by 1
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to get path?
    By arul in forum C Programming
    Replies: 6
    Last Post: 10-11-2008, 02:44 PM
  2. path
    By yes in forum C++ Programming
    Replies: 12
    Last Post: 01-29-2006, 08:14 AM
  3. path of app
    By joeyzt in forum C++ Programming
    Replies: 5
    Last Post: 01-15-2006, 12:23 PM
  4. what is the best path to take?
    By oceanrats in forum C++ Programming
    Replies: 3
    Last Post: 05-14-2005, 10:59 PM
  5. How to set the path WIN XP
    By rippascal in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2002, 07:45 PM