从包含时间的文本文件中读取

时间:2015-02-27 20:30:38

标签: c

我有这个相当大的项目要做。在这里张贴太大了。我必须写一个凿岩练习的结果。我有一个名为rock的结构,有6个变量。一切都按计划进行,我将所有细节写入文本文件,包括当前时间加上钻石的截止日期8小时。 我的.txt文件看起来就像我想要的那样

1001 11 frank 1 1 Sat Feb 28 04:23:49 2015

我保存了我的详细信息然后再次打开程序并再次保存,我的文本文件就像这样。

1001 11 frank 1 1 Sat 
0 0 Feb 28 4 :23:49

时间函数似乎导致问题。有任何想法吗

void load_master_data()
{
    struct rover b;
    struct rock c;
    int i;
    // open master file for reading and writing
    masterFile = fopen("Rover_Master.txt", "r+");
    // create master file if it doesn't exist
    if (masterFile == NULL)
    {
        FILE * temp;
        // open file for writing (creates file)
        temp = fopen("Rover_Master.txt", "w");
        fclose(temp);
        masterFile = fopen("Rover_master.txt", "r+");
        if (masterFile !=NULL)
            printf("Master file created successfully!\n");
        else
            printf("Error while creating master file!\n");
    }
    while (!feof(masterFile))
    {
        fscanf(masterFile, "%d", &b.rover_number);
        fscanf(masterFile, "%s", &b.rover_name);
        ++totalRovers;
    }
    --totalRovers;
    fseek(masterFile,0,0);
    rovers = (struct rover *) malloc(totalRovers*sizeof(struct rover));
    // reading data about branches
    for (i=0; i<totalRovers; ++i)
    {
        fgetpos(masterFile, &rovers[i].cursorPos);
        //++Branches[i].cursorPos;

        fscanf(masterFile, "%d", &rovers[i].rover_number);
        fscanf(masterFile, "%s\n", &rovers[i].rover_name);

    }

    masterFile2 = fopen("Rock_Master.txt", "r+");

    if (masterFile2 == NULL)
    {
        FILE * temp1;
        // open file for writing (creates file)
        temp1 = fopen("Rock_Master.txt", "w");
        fclose(temp1);
        masterFile2 = fopen("Rock_master.txt", "r+");
        if (masterFile2 !=NULL)
            printf("Master file created successfully!\n");
        else
            printf("Error while creating master file!\n");
    }
    while (!feof(masterFile2))

    {
        fscanf(masterFile2, "%d", &c.rock_rover_number);
        fscanf(masterFile2, "%d", &c.rock_number);
        fscanf(masterFile2, "%s", &c.geoligist);
        fscanf(masterFile2, "%d", &c.drilling_candidate);
        fscanf(masterFile2, "%d", &c.rock_completed);
        fscanf(masterFile2, "%s", &c.due_date);
        ++totalRocks;

    }
    --totalRocks;
    fseek(masterFile2,0,0);
    rocks = (struct rock *) malloc(totalRocks*sizeof(struct rock));
    for (i=0; i<totalRocks; ++i)
    {
        fgetpos(masterFile2, &rocks[i].cursorPos);
        fscanf(masterFile2, "%d", &rocks[i].rock_rover_number);
        fscanf(masterFile2, "%d", &rocks[i].rock_number);
        fscanf(masterFile2, "%s", &rocks[i].geoligist);
        fscanf(masterFile2, "%d", &rocks[i].drilling_candidate);
        fscanf(masterFile2, "%d", &rocks[i].rock_completed);
        fscanf(masterFile2, "%s", &rocks[i].due_date);
    }
}

1 个答案:

答案 0 :(得分:0)

您无法使用以下代码输入此格式的日期:

    fscanf(masterFile2, "%s", &rocks[i].due_date);

fscanf将扫描masterFile2中的单个单词,日期格式包含嵌入空格:Sat Feb 28 04:23:49 2015。您应该使用fgetsfscanf使用字符类。