首页 文章

C将结构字符串保存到文本文件中

提问于
浏览
1

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

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

// 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); 
// ...

2 回答

  • 2

    您似乎只想使用C的 fwrite() 函数将C字符串或 std::string 写入文件 .

    这应该很容易,因为你的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创建一个运算符重载是有意义的 .

  • 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;
    }
    

相关问题