2015-05-10 65 views
1

在我的程序中,我节省了高分以及几分钟和几秒的时间。在我的代码中,我目前将其作为两个int存储在名为highscore的结构中。但是,当我显示输出时,这对于格式化有点乏味。我想将时间显示为12:02而不是12:2。我在整个游戏中都有一个名为字符串时钟的变量,它已经用冒号格式化,我想要做的就是在我的文本文件中添加该变量。C++将一个结构字符串保存到一个文本文件中

我该如何重构我的代码,使其具有时间戳的单个变量,该格式将被正确格式化?我希望能够通过直接调用结构将我的数据写入文件。

// Used for Highscores 
struct highscore 
{ 
    char name[10]; 
    int zombiesKilled; 

    // I would like these to be a single variable   
    int clockMin; 
    int clockSec; 

    char Date[10]; 
}; 

// I write the data like this: 
highscore data; 
// ... 
data[playerScore].clockMin = clockData.minutes; 
data[playerScore].clockSec = clockData.seconds; 

streaming = fopen("Highscores.dat", "wb"); 
fwrite(data, sizeof(data), 1 , streaming); 
// ... 
+1

我读到_“在我的计划一切完美!” _ –

+0

嗯,我的意思是这样,我只是想用一个字符串,而不是2个整数。它使它更紧凑的代码。 :) –

+1

请在这里发布更简洁的代码 –

回答

2

似乎要简单地只写一个C-字符串或std::string到用C的fwrite()功能的文件。

这应该是很容易的,因为你的C字符串是ASCII符合要求的格式(没有Unicode滑稽的生意):

//It appears you want to use C-style file I/O 
FILE* file = NULL; 
fopen("Highscores.dat", "wb"); 

//std::string has an internal C-string that you can access 
std::string str = "01:00"; 
fwrite(str.c_str(), sizeof(char), sizeof(str.c_str()), file); 
//You can also do this with regular C strings if you know the size. 

我们还可以选择尝试使用C++ - 风格的文件I/O为更清洁的接口。

#include <iostream> 
#include <string> 
#include <iomanip> 
#include <fstream> 

int main() { 
    std::string str = "00:11"; 

    std::ofstream file("example.txt"); 

    if (file.good()) { 
     file << str; 
     std::cout << "Wrote line to file example.txt.\n"; 
    } 
    file.close(); 

    //Let's check if we actually wrote the file. 
    std::ifstream read("example.txt"); 
    std::string buffer; 

    if (read.good()) 
     std::cout << "Opened example.txt.\n"; 
    while(std::getline(read, buffer)) { 
     std::cout << buffer; 
    } 

    return 0; 
} 

此外,也有<chrono>数据类型,可以证明是相当有帮助的时候好像有。

如果你希望能够做到这一点:

file << data_struct; 

那么它将使感,为您创造的std :: ostream的一个操作符重载。

+0

是的,这是读取/写入数据的正确方法。我写了一个使用结构的不同答案,但这只是为了解释实验。 –

+0

我使用char数组的唯一原因是什么,因为我使用高分列表从最高分开始,然后下降到最低分。用这种方法,我怎样才能使用字符串并将其放入char数组中? –

+0

@BryanTheCoder你的语言不清楚。 C++中的“list”是什么意思? 'std :: vector'或'std :: list'?此外,您可能只需使用C字符串函数复制内部C字符串。 – Cinch

1

您可以试验时间函数。和读/写结构。

但是,正确的方法是使用C++基本文件存储而不是转储二进制数据。

struct highscore 
{ 
    char name[10]; 
    int n; 
    std::time_t dateTime; 
}; 

int main() 
{ 
    int total_seconds = 61; 
    char buf[50]; 
    sprintf(buf, "minutes:seconds=> %02d:%02d", total_seconds/60, total_seconds % 60); 
    cout << buf << endl; 

    std::time_t timeNow = std::time(NULL); 
    std::tm timeFormat = *std::localtime(&timeNow); 
    cout << "Current date/time " << std::put_time(&timeFormat, "%c %Z") << endl; 

    highscore data; 

    //write data: 
    { 
     FILE *streaming = fopen("Highscores.dat", "wb"); 

     strcpy(data.name, "name1"); 
     data.n = 1; 
     data.dateTime = std::time(NULL); 
     fwrite(&data, sizeof(data), 1, streaming); 

     strcpy(data.name, "name2"); 
     data.n = 2; 
     data.dateTime = std::time(NULL); 
     fwrite(&data, sizeof(data), 1, streaming); 

     fclose(streaming); 
    } 

    //read data: 
    { 
     FILE *streaming = fopen("Highscores.dat", "rb"); 

     fread(&data, sizeof(data), 1, streaming); 
     cout << "reading:\n"; 
     cout << data.name << endl; 
     cout << data.n << endl; 
     timeFormat = *std::localtime(&data.dateTime); 
     cout << std::put_time(&timeFormat, "%c %Z") << endl; 
     cout << endl; 

     fread(&data, sizeof(data), 1, streaming); 
     cout << "reading:\n"; 
     cout << data.name << endl; 
     cout << data.n << endl; 
     timeFormat = *std::localtime(&data.dateTime); 
     cout << std::put_time(&timeFormat, "%c %Z") << endl; 
     cout << endl; 

     fclose(streaming); 
    } 

    return 0; 
} 
+0

我认为你会对时间事物感到困惑!我有一个在比赛开始时就开始的比赛时间。当游戏结束时,计时器结束。我不想要现实生活中的实际时间! –

+0

请参阅sprintf –

+0

的更新_“您应该停止使用带有结构的读/写功能,因为此方法不可移植,不可扩展,并且容易出错。”_ Huh? –

相关问题