【问题标题】:fopen filename starts with weird dotfopen 文件名以奇怪的点开头
【发布时间】:2017-09-10 20:07:08
【问题描述】:

嗨。

如您所见,我正在尝试将来自 C 项目的最新数据保存在日志文件中,其中文件名对应于实际时间/日期。

虽然路径在控制台中正确组合并显示,但文件本身以一个奇怪的点开头,更准确地说,是一个空格,后面跟着那个点和另一个空格,显示在图片中。

我使用的是 Windows 7 64bit 和 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;
}

【问题讨论】:

  • 告诉我们你如何获得homepath
  • 您使用的是 Windows 还是 Linux?如前所述,您所展示的并不是相关代码的所有。包含足够的内容,以便提供的内容可以编译。
  • 请参阅How to create a Minimal, Complete and Verifiable Example。您的代码使用了几个您尚未定义的变量(例如,homepathgenerationtimestamp),这些变量都在该代码块中使用。您的代码应该包含一个完整的示例,如果需要,我们可以复制/粘贴和编译。
  • 因为您在fopen() 中使用了"ab+" 选项,是否可以合理地预期您正在打开的文件是预先存在的,(即并且可能包含后面的空格通过那个点和另一个空格)?
  • 你的数组太短了。 "D:\\cygwin64\\home\\ignite\\log\\" 是 29 个字节。

标签: c file fopen


【解决方案1】:

您的数组太短。 “D:\cygwin64\home\ignite\log\”是 29 个字节。 – 美尔波美尼

【讨论】:

  • 我不是反对者,但看来您的意图是通过将其列为答案来赞扬此评论的发布者。为此目的,可以接受评论的向上点击(在您的原始帖子下)。或者,您可以要求评论者发布他们的评论作为答案,以便您可以点击或接受。
  • 请不要在没有先要求他们这样做的情况下重新发布其他人的评论作为答案。如果他们没有回复,或者告诉您可以,请随时发布您自己的答案,但不要只是直接引用评论 - 解释它为什么重要,效果是什么等等。
  • 嗯,我没有找到将他的解决方案标记为答案的方法,所以我决定引用他的话,所以很明显问题已经解决了...... @QPaysTaxes 而不是仅仅抱怨你本可以给出对像 ryyker 这样的新手有用的指导。 “请不要转发”根本没有帮助。顺便说一句,我也找不到支持 cmets 的选项,我只能看到柜台和/或投票给 OP 和给定的答案?!
  • @Tignite ...首先,Be Nice。被动攻击不好。是的,你是被动的攻击性。其次,我做到了;你能重新阅读我的评论吗? rykker 提到的唯一一件事是我没有为评论点赞,这比让原始评论者发布答案的用处要小,因为系统认为该问题未得到回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多