C++的fstream如何读取行、列都不定长数据

Susurrant 2015-01-20 07:40:18
data.txt数据格式:
4 8 12
11 18 21 26
... ...

每一行有几个数字不固定,一共有多少行也不固定。
设有整型数组 int num[max][max];
希望按行保存,
即把第一行保存在num[0][0]、num[0][1]、num[0][2]中,
第二行保存在num[1][0]、num[1][1]、num[1][2]、num[1][3]中
以此类推,请问fstream打开文件,怎么判段读取。

用c语言的话也行!求思路!
...全文
616 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Susurrant 2015-01-20
  • 打赏
  • 举报
回复
引用 3 楼 iyomumx 的回复:
纯C++的实现应该像这样
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

const int max = 100;

int main()
{
	using namespace std;
    ifstream data("data.txt");
    int x, y, num[max][max];
    for (x = 0; !data.eof(); x++)
    {
        string line;
        getline(data, line);
        istringstream ss(line);
        for (y = 0; !ss.eof(); y++)
        {
            ss >> num[x][y];
        }
    }
	return 0;
}
正是我需要的~学习了!感觉自己还要多补补c++啊
Susurrant 2015-01-20
  • 打赏
  • 举报
回复
引用 2 楼 cjqpker 的回复:

#include <stdio.h>
#include <sstream>

using namespace std;

#define max 100

void main()
{
	FILE* fp = fopen("data.txt", "r");
	if (!fp)
	{
		return;
	}

	int num[max][max] = {0};

	int i = 0, j = 0;
	while(!feof(fp))
	{
		char str[1024] = {0};
		fgets(str, 1024, fp);
		str[strlen(str)] = '\0';

		istringstream is(str);
		string s;

		while(std::getline(is, s, ' '))
		{
			num[i][j] = atoi(s.c_str());
			j++;
		}

		i++;
	}

	for (int m = 0; m < max; m++)
	{
		bool bHasNum = false;
		for (int n = 0; n < max; n++)
		{
			if (num[m][n] != 0)
			{
				bHasNum = true;
				printf("%d ", num[m][n]);
			}
		}
		if (bHasNum)
		{
			printf("\n");
		}
	}
	
	getchar();
}

嗯~C语言的文件函数,测试无误,谢谢啦~
iyomumx 2015-01-20
  • 打赏
  • 举报
回复
纯C++的实现应该像这样
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

const int max = 100;

int main()
{
	using namespace std;
    ifstream data("data.txt");
    int x, y, num[max][max];
    for (x = 0; !data.eof(); x++)
    {
        string line;
        getline(data, line);
        istringstream ss(line);
        for (y = 0; !ss.eof(); y++)
        {
            ss >> num[x][y];
        }
    }
	return 0;
}
假正经的班长 2015-01-20
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <sstream>

using namespace std;

#define max 100

void main()
{
	FILE* fp = fopen("data.txt", "r");
	if (!fp)
	{
		return;
	}

	int num[max][max] = {0};

	int i = 0, j = 0;
	while(!feof(fp))
	{
		char str[1024] = {0};
		fgets(str, 1024, fp);
		str[strlen(str)] = '\0';

		istringstream is(str);
		string s;

		while(std::getline(is, s, ' '))
		{
			num[i][j] = atoi(s.c_str());
			j++;
		}

		i++;
	}

	for (int m = 0; m < max; m++)
	{
		bool bHasNum = false;
		for (int n = 0; n < max; n++)
		{
			if (num[m][n] != 0)
			{
				bHasNum = true;
				printf("%d ", num[m][n]);
			}
		}
		if (bHasNum)
		{
			printf("\n");
		}
	}
	
	getchar();
}

likfeng 2015-01-20
  • 打赏
  • 举报
回复
读取一行,根据空格分割字符,转化为数值存入数组 下一行...

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧