Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C fopen文件名以奇怪的点开头_C_File_Fopen - Fatal编程技术网

C fopen文件名以奇怪的点开头

C fopen文件名以奇怪的点开头,c,file,fopen,C,File,Fopen,嗨 正如您可能看到的,我试图将C项目中最近的数据保存在日志文件中,其中文件名对应于实际的时间/日期 当路径被组合并正确显示在控制台中时,文件本身以一个奇怪的点开始,更准确地说,是一个空格,后跟该点和另一个空格,显示在图片中 我正在使用Windows7 64位和cygwin64 相关代码位为: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> vo

正如您可能看到的,我试图将C项目中最近的数据保存在日志文件中,其中文件名对应于实际的时间/日期

当路径被组合并正确显示在控制台中时,文件本身以一个奇怪的点开始,更准确地说,是一个空格,后跟该点和另一个空格,显示在图片中

我正在使用Windows7 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;
}
#包括
#包括
#包括
#包括
void save_to_文件(char*时间戳、char*homepath、int生成)
char*create_时间戳(char*timestamp)
int main(){
char homepath[28]=“D:\\cygwin64\\home\\ignite\\log\\”;
整数代=0;
char*timestamp=malloc(30*sizeof(char));
创建时间戳(timestamp);
将_保存到_文件(时间戳、主路径、生成);
}
void save_to_文件(char*时间戳、char*homepath、int生成){
字符串[4];
char logchar[4]=“log”;
字符点[]={“};
char fileend[5]={.txt};
char*path=malloc(60*sizeof(char));
strcpy(路径,homepath);
strcat(路径,logchar);
snprintf(字符串,4,“%d”,生成);
strcat(路径、字符串);
strcat(路径,点);
strcat(路径、时间戳);
strcat(路径,fileend);
文件*f=fopen(路径“ab+”);
如果(f==NULL){
printf(“打开文件时出错!\n”);
出口(1);
}
否则{
//写入文件
}
}
char*create_时间戳(char*timestamp){
时间与时间;
结构tm*信息;
字符缓冲区[30],*字符串,*工作;
字符串=malloc(5*sizeof(char));
工作=malloc(30*sizeof(char));
字符点[]={“};
时间(&rawtime);
info=本地时间(&rawtime);
strcpy(缓冲区,asctime(信息));
int n=info->tm\u mday;
snprintf(字符串,4,“%d”,n);
strcpy(工作、字符串);
n=(int)info->tm\u mon+1;
snprintf(字符串,3,“%d”,n);
strcat(工作、点);
strcat(工作、字符串);
///*
n=信息->商标年+1900;
snprintf(字符串,5,“%d”,n);
strcat(工作、点);
strcat(工作、字符串);
n=信息->工时;
snprintf(字符串,3,“%d”,n);
strcat(工作、点);
strcat(工作、字符串);
n=信息->tm_min;
snprintf(字符串,3,“%d”,n);
strcat(工作、点);
strcat(工作、字符串);
n=信息->tm_秒;
snprintf(字符串,3,“%d”,n);
strcat(工作、点);
strcat(工作、字符串);
strcpy(时间戳、工时);
自由(弦);
返回时间戳;
}
你的数组太短了。“D:\cygwin64\home\ignite\log\”是29个字节梅尔波梅


向我们展示如何获得
homepath
?您使用的是Windows还是Linux?如前所述,您显示的并不是所有相关代码。包括足够的内容,以便所提供的内容可以编译。请参阅。您的代码使用了多个未定义的变量(例如,
homepath
generation
timestamp
),这些变量都在该代码块中使用。您的代码应该包含一个完整的示例,如果需要,我们可以复制/粘贴和编译。因为您使用的是
fopen()
中的
“ab+”
选项,所以您打开的文件是预先存在的(即,可以包含一个空格,后跟该点和另一个空格),这合理吗?您的阵列太短<代码>“D:\\cygwin64\\home\\ignite\\log\\”是29个字节。我不是被否决的投票人,但你的意图似乎是通过将此评论的海报列为答案来认可它。出于这一目的,向上点击评论(在您的原始帖子下)是可以接受的。或者,您可以要求评论者将他们的评论作为答案发布,这样您就可以点击或接受。未经要求,请不要将其他人的评论作为答案转发。如果他们没有回应,或者告诉你可以,请随意发布你自己的答案,但不要只是直接引用评论——解释为什么重要,影响是什么,等等。我没有找到一种方法将他的解决方案标记为答案,所以我决定引用他,这样问题显然已经解决了@QPaysTaxes,而不是仅仅抱怨,你可以给一个像Ryker那样的新手提供有用的指导。“请不要转载”一点帮助都没有。顺便说一句,我也找不到向上投票的选项,我只能看到计数器和/或投票的OP和给定的答案@提格尼特…首先。被动的攻击是不好的。是的,你是被动攻击。第二,我做到了;你能重读我的评论吗?rykker提到的唯一一件事是我没有对评论进行投票,这比让最初的评论人发表一个答案有用得多,因为系统认为这个问题没有答案。