2017-04-14 96 views
-1

https://i.stack.imgur.com/4TmGr.pngfopen文件名以奇怪點開始

嗨。

正如你可能看到的,我試圖從日誌文件中的C項目中保存最近的數據,其中的文件名與實際的時間/日期相對應。

雖然路徑在控制檯內部組合並正確顯示,但文件本身始於一個奇怪的點,更確切地說是一個空格,後面跟着該點和另一個空格,顯示在圖片中。

我使用Windows 7 64位和cygwin64。

代碼的相關位是:

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <string.h> 
void save_to_file(char* timestamp, char* homepath, int generation) 
char* create_timestamp(char* timestamp) 
int main(){ 
    char homepath[28] = "D:\\cygwin64\\home\\ignite\\log\\"; 
    int generation = 0; 

    char* timestamp = malloc (30 * sizeof(char)); 
    create_timestamp(timestamp); 
    save_to_file(timestamp, homepath, generation); 
} 

void save_to_file(char* timestamp, char* homepath, int generation){ 
    char string[4]; 
    char logchar[4] = "log"; 
    char dot[] = {"."}; 
    char fileend[5] = {".txt"}; 
    char* path = malloc(60*sizeof(char)); 
    strcpy(path, homepath); 
    strcat(path, logchar); 
    snprintf(string, 4, "%d", generation); 
    strcat(path, string); 
    strcat(path, dot); 
    strcat(path, timestamp); 
    strcat(path, fileend); 
    FILE* f = fopen(path, "ab+"); 
    if(f == NULL){ 
     printf("Error opening file!\n"); 
     exit(1); 
    } 
    else{ 
     //write to file 
    } 
} 
char* create_timestamp(char* timestamp){ 
    time_t rawtime; 
    struct tm *info; 
    char buffer[30], *string, *work; 
    string = malloc (5* sizeof(char)); 
    work = malloc (30* sizeof(char)); 
    char point[] = {"."}; 
    time(&rawtime); 

    info = localtime(&rawtime); 
    strcpy(buffer, asctime(info)); 

    int n = info->tm_mday; 
    snprintf(string, 4, "%d", n); 
    strcpy(work, string); 

    n = (int) info->tm_mon + 1; 
    snprintf(string, 3, "%d", n); 
    strcat(work, point); 
    strcat(work, string); 
    ///* 
    n = info->tm_year + 1900; 
    snprintf(string, 5, "%d", n); 
    strcat(work, point); 
    strcat(work, string); 


    n = info->tm_hour; 
    snprintf(string, 3, "%d", n); 
    strcat(work, point); 
    strcat(work, string); 

    n = info->tm_min; 
    snprintf(string, 3, "%d", n); 
    strcat(work, point); 
    strcat(work, string); 

    n = info->tm_sec; 
    snprintf(string, 3, "%d", n); 
    strcat(work, point); 
    strcat(work, string); 
    strcpy(timestamp, work); 
    free(string); 
    return timestamp; 
} 
+1

向我們展示如何獲得'homepath'? –

+1

您是使用Windows還是Linux?如上所述,您所展示的不是相關代碼的所有內容。包括足夠的東西,以便提供的東西可以編譯。 – ryyker

+2

請參閱[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。你的代碼使用了你沒有定義的幾個變量(例如'homepath','generation','timestamp'),這些變量都被用在這個代碼塊中。你的代碼應該包含一個完整的例子,如果需要的話,我們可以複製/粘貼和編譯。 –

回答

-2

你的陣列是太短了。 「D:\ cygwin64 \ home \ ignite \ log \」是29個字節。 - melpomene

+1

我不是下來的選民,但看起來你的意圖是把這個評論的海報列爲答案。評論(在您原來的帖子下)的升級可以用於此目的。或者,您可以要求評論者發表評論作爲答案,以便您可以點擊或接受。 – ryyker

+0

請不要先轉讓其他人的評論作爲回答,而是先要求他們這樣做。如果他們沒有迴應,或者告訴你可以,請隨時發佈自己的回答,但不要僅僅是評論的直接引用 - 解釋它爲什麼重要,影響是什麼等等。 –

+0

好吧,我沒有找到一種方法來標記他的解決方案作爲答案,所以我決定引用他,所以很明顯,這個問題是固定的... @QPaysTaxes而不是隻是抱怨你可以給一個像新手一樣有用的指示ryyker做了。 「請不要轉貼」根本沒有幫助。 順便說一句,我也不能找到upvote評論的選項,我只能看到櫃檯和/或投票的OP和給定的答案?! – Tignite