用sscanf_s按指定格式读文件,出现乱码

在尝试使用sscanf_s从文件中读取姓名和年龄时,初学者遇到了乱码问题。代码能正常打开文件,但读取时出现了错误。问题出在代码与文本格式不匹配,导致解析失败并出现乱码。通过调整sscanf_s的格式匹配,可以解决这个问题。
摘要由CSDN通过智能技术生成

我(小白学c/c++两个半月)只是想记录我的一些bug,大神勿进

读的文件:
文件
代码如下:
#include
#include
#include
#include
#include <Windows.h>

using namespace std;

int main() {
string line;
ifstream infile;
char name[32];
int age;
infile.open(“wei.txt”);

while (1) {
	getline(infile, line);
	if (infile.eof()) {
		break;
	}

	sscanf_s(line.c_str(), "name:%s age:%d", name,
		sizeof(name), &age);
	cout << "姓名:" << name << "\t\t\t年龄:" << age << endl;

}

infile.close();

system("pause");
return 0;

}
运行正确结果:
运行结果
//
<以下是乱码的情况>
代码如下:
#include
#include
#include

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weifc-wei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值