C 将字符串从结构保存到文本文件中
C++ Save a string from a struct into a text file
在我的程序中,我有一个时钟计时器,我需要将其保存到一个char数组[10]中,然后将其实现到我的高分函数中。通过我的程序,我已经有一个格式化的时间。例如,如果时钟的秒数低于十,我必须添加零。因此,0:02,如果时钟的秒数大于十,则保持正常。我不必在结构中使用两个int变量,而只能将字符串写入文本文件中?例如,让我们写一个称为字符串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);
如果您可以使用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++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入