WIndows下 C++ 从文件中读取数据

本文介绍了在Windows下使用C++进行文件数据读取的方法,包括尝试使用fscanf函数以及getline函数,但在实际操作中遇到了内存错误std::bad_alloc的问题,目前该问题尚未解决。
摘要由CSDN通过智能技术生成

背景:WIndows下利用C++ 从文件中读写内容

1、使用fscanf语句

#include <map>
#include <fstream>

std::FILE *fp;
fp = fopen(filePath.c_str(), "r");
if (!fp ) {
	printf("Cann't open input  csv file: %s\n", filePath);
	return 0;
};
std::map<std::string,std::string> NoNameMap;
fscanf(fp, "%s,%s,%s", &a, &b, &c);
printf("%s %s %s\n", a, b, c);
while (1){
    std::string first, second,temp;
	fscanf(fp, "%s,%s,%s", &first, &second, &temp);
	printf("%s %s %s\n", first, second,temp);
	NoNameMap.insert(std::pair<std::string, std::string>(first, second));
	if(feof(fp)) //判断是否到文件结尾
		break;
}	
fclose(fp);

注意:执行到 NoNameMap.insert 这一步。总是出现异常:内存错误 std::bad_alloc,目前没有解决方法

2、使用getLine函数

std::ifstream f(filePath);
	std::string csvLine;
	getline(f, csvLine);//第一行表头不做统计
	while (getline(f, csvLine))
	
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值