2017-03-09 10 views
1

崩潰,我使用的Visual Studio 2013來構建交流應用的fopen和fopen_s似乎對長文件名

的代碼看起來是這樣的 -

int main(int argc, char *argv[]) 
{ 
    char *InFilename = NULL; 
    char *OutFilename = NULL; 
    int ff_count; // counts the number of successive 0xff's read from the file buffer 
    int fpga_end, nios_start, nios_end; // used to report the size of each region 
    int file_length, file_index, temp_length, new_file_length; 
    int root_length; 
    int result; 

    FILE *infile = NULL; 
    FILE *outfile = NULL; 

    printf("Start JIC 2 rbf\r\n"); 

    if ((argc != 2)) 
    { 
     printf("\r\n\r\nV1.2 - Usage: <jic2rbf> <name of jicfile> \r\n"); 
     printf("\r\n This program strips out the header info at the top of the file\r\n"); 
     printf("\r\n and most of the ff's at the bottom. \r\n"); 
     exit(1); 
    } 

    // 
    // Extract the name of the input file up to the '.' and use it to create the output file name with a .rbf extension. 
    // 

    InFilename = argv[1]; 
    root_length = strcspn(InFilename,"."); 

    printf("Root len = %d\r\n",root_length); 

    OutFilename = (char *)malloc(root_length+EXT_LENGTH); 
    memcpy(OutFilename,InFilename,root_length); 
    OutFilename[root_length] = 0; 
    strcat(OutFilename,".rbf"); 

    printf("In file to be used %s\r\n", InFilename); 
    printf("Out file to be used %s\r\n", OutFilename); 

    result = fopen_s(&outfile, OutFilename, "wb"); 
    if (result) 
    { 
     printf("Cannot open this file %s\r\n - 0x%x", OutFilename, result); 
     return 0; 
    } 
    printf("Open In - %d\r\n",result); 

如果我把從DOS命令這個可執行文件

E:/projects/Q4300_Hdcp/q_series_hdcp_base/fpga/q_series_hdcp_tx_dual_singleHID/par/q_series_hdcp_tx_dual_singleHID/output_files/q_series_hdcp_tx_dual_singleHID_elf.jic 

整個應用程序的工作

如果我所說的申請 - 與線使用以下命令行 -

E:/projects/Q4300_Hdcp/q_series_hdcp_base/fpga/q_series_hdcp_tx_dual_fpga/par/q_series_hdcp_tx_dual_fpga/output_files/q_series_hdcp_tx_dual_fpga_elf.jic 

我沒有看到printf("Open In - %d\r\n",result);輸出。該應用程序似乎崩潰。 我認爲這可能是某種緩衝區溢出的文件名稱,但較短的文件名工程.....如果我cd到目錄與文件和調用命令行q_series_hdcp_tx_dual_fpga_elf.jic的工作。 如果我DIR文件 - E:/projects/Q4300_Hdcp/q_series_hdcp_base/fpga/q_series_hdcp_tx_dual_fpga/par/q_series_hdcp_tx_dual_fpga/output_files/q_series_hdcp_tx_dual_fpga_elf.jic 我看到文件.......

我不知道如何捕捉到異常或什麼別的追求解決這個問題,任何想法將是巨大的。 謝謝, 馬丁

+0

爲什麼用'memcpy'複製字符串?你不能使用'strcpy'嗎?另外,你的malloc需要爲空終止符分配一個額外的字符。 – bruceg

+0

「EXT_LENGTH」未被聲明/定義在任何地方。它的價值很重要。 – chux

+1

@bruceg OP不是試圖複製_string_,而是一個部分,因此合理使用'memcpy()'。 – chux

回答

2

嘗試改變這一行:

OutFilename = (char *)malloc(root_length+EXT_LENGTH); 

這樣:

OutFilename = malloc(1 + root_length + EXT_LENGTH); 

爲空終止分配空間。另外,不需要施放malloc的返回值。

+0

還要確保EXT_LENGTH是4,因爲你假設它的長度是「.rbf」 –

+1

輝煌,謝謝----我一直在看這個片刻,但忘了回到程序的開始,因爲通常工作,大聲笑。解決方法是將malloc加1 ......,謝謝 – Martin

0

DOS對命令行的長度一些嚴格的限制

所以當命令行太長,比如你的問題是會發生的經驗。

你真的認爲你使用Visual Studio和Windows?

很多DOS的侷限性,已結轉到Windows和Visual Studio