2010-10-25 79 views
0

我想更改下面的代碼以使inwash文件能夠從任何目錄加載,而不是從tech_projects環境變量加載。如何從c程序中的任何目錄加載文件

/**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'; 
+0

@Scott,謝謝你修復可怕的格式。 @Monica,像'pjects.arr [strlen(pjects.arr)] ='\ 0'這樣的代碼點是什麼?你知道strlen()是如何工作的嗎? – Blastfurnace 2010-10-25 06:01:45

+1

是你寫的代碼,還是來自其他地方,你需要幫助理解它? – 2010-10-25 06:04:04

+0

這段代碼總是在那裏工作,但是這些文件是從環境變量「Tech_Projects」加載的,該環境變量映射爲每個用戶計算機上的ap:\ drive,所以當他們運行該程序時,它會自動打開p:\中的文件夾。驅動器加載inwash文件.............所以現在我需要幫助來改變能夠從任何目錄加載文件,我只是nid來改變文件加載位置從p: \驅動器從任何目錄加載。這解釋。 – Monica 2010-10-28 07:21:02

回答

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'; 
+0

我試圖用strcpy(pjects.arr,getdir([cdirectory,[ctext,[ccaption,[nflag,[clrootonly]]]]]))來替換行,但是它給出了一個錯誤 gc_load.obj:error LNK2001:無法解析的外部符號_getdir gc_load.exe:致命錯誤LNK1120:1個未解析的外部 – Monica 2010-10-28 07:08:12

0

這可能是

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

並命名可以通過命令行傳遞的目錄中,例如

$ myprogram /user/foo 
相關問題