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

时间:2021-04-30 13:26:10

In my program, I have a clock timer and I need it to be saved into a char array[10] which is then implemented into my highscore function. Through my program, I already have a formatted time. Example, if the clock's seconds is lower than ten, I have to add a zero. So, 0:02, and if the clock's seconds is greater than ten, it stays normal. Instead of me using two int variables in my struct, how can I just write a string into my text file? For example, lets write a string called string clocksTime = "0:15". Note it has already been formatted. Here is my code:

在我的程序中,我有一个时钟计时器,我需要将它保存到一个char数组[10],然后将其实现到我的高分函数中。通过我的程序,我已经有一个格式化的时间。例如,如果时钟的秒数小于10,我必须加零。所以,0:02,如果时钟的秒数大于10,则保持正常。而不是我在我的struct中使用两个int变量,我怎么才能在我的文本文件中写一个字符串?例如,让我们写一个名为string clocksTime =“0:15”的字符串。请注意,它已被格式化。这是我的代码:

struct highscore
{
    // *Want* Change these two int values to a string
    int clockMin;
    int clockSec;
}; 
...[Code]…
// Change the two variables to have it instead, data[playerScore].clock = clocksTime
data[playerScore].clockMin = clockData.minutes;
data[playerScore].clockSec = clockData.seconds;
_strdate(data[playerScore].Date);

// Write the variables into the text file
streaming = fopen( "Highscores.dat", "wb" );
fwrite( data, sizeof(data), 1 , streaming); 

1 个解决方案

#1


If you can use std::string it's a lot cleaner. Here is a program that does it:

如果你可以使用std :: string它会更清洁。这是一个程序:

#include <iostream>

using namespace std;

void int_time_to_string(const int int_time, string& string_time);

int main() {

    int clockMin = 0;
    int clockSec = 15;

    string clockMinString;
    string clockSecString;

    int_time_to_string(clockMin, clockMinString);
    int_time_to_string(clockSec, clockSecString);

    string clockString = clockMinString + ":" + clockSecString;

    cout << "clock = " << clockString << endl;

    return 0;
}

// Convert a time in int to char*, and pad with a 0 if there's only one digit
void int_time_to_string(const int int_time, string& string_time) {

    if (int_time < 10) {
        // Only one digit, pad with a 0
        string_time = "0" + to_string(int_time);
    } else {
        // Two digits, it's already OK
        string_time = to_string(int_time);
    }
}

If, on the other hand, you can't use std::string, you have to resort to C strings, that is, arrays of chars. I'm quite rusty about this, but I think we can use sprintf for the conversion, and strcpy/strcat to deal with some of the lower-level operations (like putting '\0' at the end).

另一方面,如果不能使用std :: string,则必须使用C字符串,即字符数组。我对此非常生疏,但我认为我们可以使用sprintf进行转换,并使用strcpy / strcat来处理一些较低级别的操作(比如将'\ 0'放在最后)。

#include <iostream>
#include <cstring>

using namespace std;

void int_time_to_string(const int int_time, char* string_time);

int main() {

    int clockMin = 0;
    int clockSec = 15;

    char clockMinString[3] = ""; // 2 digits + null character
    char clockSecString[3] = ""; // 2 digits + null character

    int_time_to_string(clockMin, clockMinString);
    int_time_to_string(clockSec, clockSecString);

    char clockString[6]; // 2 digits, colon, 2 more digits, null character
    strcpy(clockString, clockMinString);
    strcat(clockString, ":");
    strcat(clockString, clockSecString);

    cout << "clock = " << clockString << endl;

    return 0;
}

// Convert a time in int to char*, and pad with a 0 if there's only one digit
void int_time_to_string(const int int_time, char* string_time) {

    if (int_time < 10) {
        // Only one digit, pad with a 0
        strcpy(string_time, "0") ;
        char temp[2]; // 1 digit + null character
        sprintf(temp, "%d", int_time);
        strcat(string_time, temp);
    } else {
        // Two digits, it's already OK
        sprintf(string_time, "%d", int_time);
    }
}

In both cases you just have to take clockString.

在这两种情况下,您只需要使用clockString。

#1


If you can use std::string it's a lot cleaner. Here is a program that does it:

如果你可以使用std :: string它会更清洁。这是一个程序:

#include <iostream>

using namespace std;

void int_time_to_string(const int int_time, string& string_time);

int main() {

    int clockMin = 0;
    int clockSec = 15;

    string clockMinString;
    string clockSecString;

    int_time_to_string(clockMin, clockMinString);
    int_time_to_string(clockSec, clockSecString);

    string clockString = clockMinString + ":" + clockSecString;

    cout << "clock = " << clockString << endl;

    return 0;
}

// Convert a time in int to char*, and pad with a 0 if there's only one digit
void int_time_to_string(const int int_time, string& string_time) {

    if (int_time < 10) {
        // Only one digit, pad with a 0
        string_time = "0" + to_string(int_time);
    } else {
        // Two digits, it's already OK
        string_time = to_string(int_time);
    }
}

If, on the other hand, you can't use std::string, you have to resort to C strings, that is, arrays of chars. I'm quite rusty about this, but I think we can use sprintf for the conversion, and strcpy/strcat to deal with some of the lower-level operations (like putting '\0' at the end).

另一方面,如果不能使用std :: string,则必须使用C字符串,即字符数组。我对此非常生疏,但我认为我们可以使用sprintf进行转换,并使用strcpy / strcat来处理一些较低级别的操作(比如将'\ 0'放在最后)。

#include <iostream>
#include <cstring>

using namespace std;

void int_time_to_string(const int int_time, char* string_time);

int main() {

    int clockMin = 0;
    int clockSec = 15;

    char clockMinString[3] = ""; // 2 digits + null character
    char clockSecString[3] = ""; // 2 digits + null character

    int_time_to_string(clockMin, clockMinString);
    int_time_to_string(clockSec, clockSecString);

    char clockString[6]; // 2 digits, colon, 2 more digits, null character
    strcpy(clockString, clockMinString);
    strcat(clockString, ":");
    strcat(clockString, clockSecString);

    cout << "clock = " << clockString << endl;

    return 0;
}

// Convert a time in int to char*, and pad with a 0 if there's only one digit
void int_time_to_string(const int int_time, char* string_time) {

    if (int_time < 10) {
        // Only one digit, pad with a 0
        strcpy(string_time, "0") ;
        char temp[2]; // 1 digit + null character
        sprintf(temp, "%d", int_time);
        strcat(string_time, temp);
    } else {
        // Two digits, it's already OK
        sprintf(string_time, "%d", int_time);
    }
}

In both cases you just have to take clockString.

在这两种情况下,您只需要使用clockString。