开发者

how to load files from any directory in c programme

开发者 https://www.devze.com 2023-01-22 00:33 出处:网络
i want to change this code below for the inwashfile to be able to load from any directory instead of loa开发者_如何转开发ding from the tech_projects environmental variable.

i want to change this code below for the inwashfile to be able to load from any directory instead of loa开发者_如何转开发ding from the tech_projects environmental variable.

  /**Get projects directory from environment variable****************/
 strcpy(pjects.arr, getenv("Tech_Projects"));

  pjects.arr[strlen(pjects.arr)] = '\0';

  if (strcmp(inwashfile.arr, "null") != 0)
  {
    for (d=2;d<inwashfile.len;d++)
    {
      tempfile.arr[d-2] = inwashfile.arr[d];
    }      
    memset(inwashfile.arr, '\0', 255);    

    strcpy(inwashfile.arr, pjects.arr);
    strcat(inwashfile.arr, tempfile.arr);

    inwashfile.len = strlen(inwashfile.arr);
    inwashfile.arr[inwashfile.len] = '\0';
    do_wash[0] = 'T';
  }
  else
  {
    do_wash[0] = 'F';
  }
printf("3\n");
  do_wash[1] = '\0';


 /**Get projects directory from environment variable****************/
 /* You need to replace this line with wherever you want to get the
    file name from. Either request it from the user, pull it from a
    configuration file or read it from the command line */
 strcpy(pjects.arr, getenv("Tech_Projects"));

  /* this line does absolutely nothing. strlen() relies on the string
     already being null-terminated */
  pjects.arr[strlen(pjects.arr)] = '\0';

  if (strcmp(inwashfile.arr, "null") != 0)
  {
    for (d=2;d<inwashfile.len;d++)
    {
      tempfile.arr[d-2] = inwashfile.arr[d];
    }      
    memset(inwashfile.arr, '\0', 255);    

    strcpy(inwashfile.arr, pjects.arr);
    strcat(inwashfile.arr, tempfile.arr);

    inwashfile.len = strlen(inwashfile.arr);
    inwashfile.arr[inwashfile.len] = '\0';
    do_wash[0] = 'T';
  }
  else
  {
    do_wash[0] = 'F';
  }
printf("3\n");
  do_wash[1] = '\0';


It could be

strcpy(pjects.arr, argv[ 1 ] );

and the directory named could be passed from the command line, e.g.

$ myprogram /user/foo
0

精彩评论

暂无评论...
验证码 换一张
取 消