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

Posted

技术标签:

【中文标题】C ++将结构中的字符串保存到文本文件中【英文标题】:C++ Save a string from a struct into a text file 【发布时间】:2015-05-12 20:25:59 【问题描述】:

在我的程序中,我有一个时钟计时器,我需要将它保存到一个 char 数组 [10] 中,然后将其实现到我的 highscore 函数中。通过我的程序,我已经有了一个格式化的时间。例如,如果时钟的秒数低于十,我必须添加一个零。所以,0:02,如果时钟的秒数大于 10,它保持正常。而不是我在我的结构中使用两个 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); 

【问题讨论】:

为什么不按原样保存结构?如果您必须再次加载文件,您可以将其加载到同一个结构中不是很好吗?让数据成为数据,让呈现成为呈现。 fprintf 有什么问题,或者,如果你真的想要它在一个字符串中,snprintf?还是 C++ iostreams/stringstream? 使用std::string 而不是char 的数组要容易得多。你可以使用std::string吗? @FabioTurati,唯一的问题是当我使用字符串时出现错误。 【参考方案1】:

如果你可以使用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);
    

另一方面,如果您不能使用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);
    

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

【讨论】:

以上是关于C ++将结构中的字符串保存到文本文件中的主要内容,如果未能解决你的问题,请参考以下文章

写一个Linux C程序,将一个文本文件中的所有小写字母转换为大写字母。

将每个 excel 列数据保存到单独的文本文件中

将文本文件中的数据提取到结构中

将文本框中的数据保存到文本文件中

如何将在c语言中生成的数据保存到文本文件中?

将变量保存到 vb.net 中的文本文件