2017-04-14 42 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