您的位置:首页 > 编程语言 > C语言/C++

C++读取txt数据为二维数组 将数据保存到txt文本中

2017-12-14 16:25 417 查看

    C++读取txt数据为二维数组 保存txt文本数据

    C++文件读写操作有:ofstream,ifstream,fstream:
#include <fstream>
ofstream         //文件写操作 内存写入存储设备
ifstream         //文件读操作,存储设备读区到内存中
fstream          //读写操作,对打开的文件可进行读写操作
   详细资料请参考http://blog.csdn.net/kingstar158/article/details/6859379   C++获取二维数组的行列数的方法:
//对于type array[A];形式的二维数组,可以通过计算sizeof获取行列数。
sizeof(array[0][0])//为一个元素占用的空间,
sizeof(array[0])   //为一行元素占用的空间,
sizeof(array)      //为整个数组占用的空间,
行数 = sizeof(array)/sizeof(array[0]);
列数 = sizeof(array[0])/sizeof(array[0][0]);
#include <iostream>
using namespace std;
template <class T>
int getArrayLen(T& array) //使用模板定义一个函数getArrayLen,该函数将返回数组array的长度
{
return (sizeof(array) / sizeof(array[0]));
}
int main()
{
char a[] = {'1','2','3'};
cout << getArrayLen(a) << endl;
return 0;
}
输出为3

[b]例子:
【1】C++读取txt数据为二维数组
     imageData.txt文本的数据如下:

10.0 25 30
100 200 1000
1 2 3
1 2 3
    C++读取imageData.txt文本的数据,并将数据转存为二维数组的方法#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <io.h>
#include <fstream>
using namespace std;

//txt文本必须是txtRows行,txtCols列存储的文本数据
#define readDataPath "D:\\imageData.txt"//txt文本的路径
#define txtRows 4 //txt行数
#define txtCols 3 //txt列数
int main()
{
float txtData[txtRows][txtCols];
int i, j;
FILE* fp = fopen(readDataPath, "r"); //打开文件
if (fp == NULL)
{
printf("文件读取错误...");
return -1;
}
for (i = 0; i < txtRows; i++)
{
for (j = 0; j < txtCols; j++)
{
fscanf(fp, "%f", &txtData[i][j]);/*每次读取一个数,fscanf函数遇到空格或者换行结束*/
}
fscanf(fp, "\n");
}
fclose(fp);
for (i = 0; i < txtRows; i++)
{
for (j = 0; j < txtCols; j++)
{
printf("%.1f ", txtData[i][j]);//输出
}
printf("\n");
}
return 0;
}   运行结果:

【2】读取txt文本中的字符
   imageName.txt文本的数据如下:

1.jpg
2.jpg
3.jpg
   C++读取方法如下:// CppPython.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;

//txt文本每行存储一个字符串
#define readDataPath "D:\\imageName.txt" //txt文本的路径
int main()
{
ifstream txtStream(readDataPath);
string line;
if (!txtStream.is_open())
cout << "Error opening file";

//(1)推荐这个写法
while (getline(txtStream, line)) // line中不包括每行的换行符
{
cout << line << endl;
}

//(2)使用eof()判断,会多出一行空的
while (!txtStream.eof())
{
getline(txtStream, line);
cout << line << endl;
}
//或者用字符数组存储
char buffer[256];
while (!txtStream.eof())//eof():如果读文件到达文件末尾,返回true。
{
txtStream.getline(buffer, 100);
cout << buffer<< endl;
}
return 0;
}【3】txt文本复杂的情况
    假设data.txt的数据如下: 每行是一个样本,每行的第1列是一个字符串,其余是整型的数据
1.jpg 11 12 13 14 15
2.jpg 1 2 3 4 5
3.jpg 111 121 131 141 151   这里把第1列的字符串保存在vector中,其余数据保存在二维数组data中,C++实现代码如下:
#include <fstream>
#include <string>
#include "vector"
#include <iostream>
using namespace std;

//txt文本的路径
#define readDataPath "D:\\MyGitPro\\imageData\\data.txt"
#define txtRows 3//txt文本行数
#define txtCols 6//txt文本列数

struct fileData
{
vector<string> name;//第1列
int data[txtRows][txtCols - 1];//保存txt文本的数据(第2列开始)
};

fileData loadFileData(char* path);

int main()
{
fileData m_fileData = loadFileData(readDataPath);
for (int i = 0; i < m_fileData.name.size(); i++)
{
cout << m_fileData.name.at(i) << endl;
}
int data[txtRows][txtCols - 1];
memcpy(data, m_fileData.data, sizeof(data));
for (size_t row=0;row<txtRows;row++)
{
for (size_t col = 0; col < txtCols - 1; col++)
{
cout << data[row][col]<<",";
}
cout << endl;
}
}

fileData loadFileData(char* path) {
fileData m_fileData;
char name[81];
std::vector<string> v_name;
int txtData[txtRows][txtCols - 1];
int i, j;
FILE* fp = fopen(path, "r"); //打开文件
if (fp == NULL)
{
printf("文件读取错误...");
return m_fileData;
}
for (i = 0; i < txtRows; i++)
{
for (j = 0; j < txtCols; j++)
{
if (j == 0)
{
fscanf(fp, "%s", name);
v_name.push_back(name);
}
else {
fscanf(fp, "%d", &txtData[i][j - 1]);/*每次读取一个数,fscanf函数遇到空格或者换行结束*/
}
}
fscanf(fp, "\n");
}
fclose(fp);
memcpy(m_fileData.data, txtData, sizeof(m_fileData.data));
m_fileData.name = v_name;
return m_fileData;
}【4】C++将数据保存到txt文本中#include "stdafx.h"
#include <fstream>
#include <string>
#include "vector"
#include <iostream>
using namespace std;

//txt文本保存的路径
#define writeDataPath "D:\\imageName.txt"
int main()
{
fstream fout(writeDataPath);
vector<string> imageName;
imageName.push_back("1.jpg");
imageName.push_back("2.jpg");
imageName.push_back("3.jpg");
for (int i = 0; i < imageName.size(); i++)
{
fout << imageName.at(i) << endl;
}
cout << "数据保存完毕..." << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐