用C++中的二维向量从txt文件中读入任意大小的矩阵
读取一个txt文件,形式类似如下:1111111111011000111100101011011010101000由于矩阵大小不固定所以我打算用向量做:typedefve...
读取一个txt文件,形式类似如下:
1111111111
0110001111
0010101101
1010101000
由于矩阵大小不固定所以我打算用向量做:
typedef vector<vector<int> > Mat;
Mat input()
{
ifstream in("D:\\aaa.txt");
Mat a;
for(string s;getline(in,s);)
{
vector<int> b;//下面就不会编了,问题是如何从s中一个一个的取字符然后调用push_back
for(int i;i<=s.length();i++)
{//这边都不对,脑袋完全乱掉了
。
。
。
。
b.push_back(i);
}
a.push_back(b);
}
return a;
}
最后输出是这样:
void print(Mat& a)
{
for(int i=0;i<a.size();++i)
{
for(int j=0;j<a[i].size();++j)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
算了,把问题放简单点:
txt文件中矩阵大小m×n固定,如4×5:
10110
10110
12021
24021
用二维数组a[m][n]读入,该怎么编写?编出来再追加分数~! 展开
1111111111
0110001111
0010101101
1010101000
由于矩阵大小不固定所以我打算用向量做:
typedef vector<vector<int> > Mat;
Mat input()
{
ifstream in("D:\\aaa.txt");
Mat a;
for(string s;getline(in,s);)
{
vector<int> b;//下面就不会编了,问题是如何从s中一个一个的取字符然后调用push_back
for(int i;i<=s.length();i++)
{//这边都不对,脑袋完全乱掉了
。
。
。
。
b.push_back(i);
}
a.push_back(b);
}
return a;
}
最后输出是这样:
void print(Mat& a)
{
for(int i=0;i<a.size();++i)
{
for(int j=0;j<a[i].size();++j)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
算了,把问题放简单点:
txt文件中矩阵大小m×n固定,如4×5:
10110
10110
12021
24021
用二维数组a[m][n]读入,该怎么编写?编出来再追加分数~! 展开
6个回答
展开全部
可以按照如下几步操作来读取矩阵。
1、确定文件名。
2、打开文件,使用fopen函数。
fopen("文件名", “r”);
3、根据约定的文件格式,包括文件中矩阵规模,元素的类型,以及元素分隔的符号,采用fscanf函数循环读入矩阵。
4、判断文件是否读完,如未读完,重复第三步直到读完。
5 关闭文件。
举例说明:
文件名为in.txt, 文件中存有若干行整型数据,每行3个元素,元素间以空格分隔。即存有一个3*n的矩阵,n值不定,约定最大为100行。
代码如下:
int a[100][3];//定义矩阵
int lines=0;//矩阵行数
void matrix_read()
{
FILE *fp;
int i;
fp = fopen("in.txt", "r");//打开文件
if(fp == NULL)//打开失败
return;
while(lines < 100)
{
for(i = 0; i < 3; i ++)
if(fscanf(fp, "%d",&a[lines][i]) == EOF) break;//读取数据
if(feof(fp)) break;//判断是否文件结束。
lines++;//读取一行成功,增加行数。
}
fclose(fp);//关闭文件。
}
展开全部
/* To read a matrix in arbitrary dimensions from a specified file
*/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
/*
When running the program, you should promise that the directory where the program
resides contains a data file named `dat.txt`. The format of the file is similar as
below:
1 2 3
4 5 6
7 8 9
The matrix may not be a square matrix whose row number equals column number.
For example. The following case is also okay.
1 2 4 5
2 3 0 0
*/
/* The buffer contains the whole content of the data file */
size_t matrix_get_col_num(const char* buffer)
{
/* to check input parameter */
if ( buffer == NULL || buffer[0] == 0 )
{
return 0;
}
/*
for example:
1 2 3 5 6
2 3 1 2 1
The column number is 5.
You can count the space character number to get this value.
*/
size_t col = 1;
for (; *buffer != '\n'; buffer++)
{
if (*buffer == ' ')
col++;
}
return col;
}
size_t matrix_get_row_num(const char* buffer)
{
/* Check input parameter to determine whether it is legal or not*/
if ( buffer == NULL || buffer[0] == 0)
return 0;
size_t row = 0;
/* Count the the number of line feed characters('\n') as the row number */
for (; *buffer != 0; buffer++)
{
if (*buffer == '\n')
row++;
}
return row;
}
/* Read the data file's content*/
char* matrix_read_file(const char* filename)
{
FILE* fp = fopen(filename, "r");
size_t file_len = 0;
char* buffer = NULL;
if (fp == NULL)
return NULL;
/* Get the file length in byte */
fseek(fp, 0, SEEK_END);
file_len = ftell(fp);
fseek(fp, 0, SEEK_SET);
/* Read the whole content of the file and save it into a memory buffer.
NOTE, the buffer allocated is larger than the file length by one byte
to store the string terminal character('\0')
*/
buffer = (char*)malloc(file_len + 1);
if (!buffer)
{
fclose(fp);
return NULL;
}
/* Check whether the reading amount equals to the file length */
if (fread(buffer, 1, file_len, fp) != file_len)
{
free(buffer);
fclose(fp);
return NULL;
}
fclose(fp);
return buffer;
}
/* Get matrix elements from the given buffer. `row` and `col` represent the dimension of the matrix */
int* matrix_parse_and_save(const char* buffer, size_t row, size_t col)
{
int i = 0;
int* arrays = (int*)malloc(row * col * sizeof(int));
const char* pt = buffer;
for (; *pt != 0; pt++)
{
/* `pt == buffer` means the beginning of the matrix;
`*(pt - 1) == ' '` means a new number being encountered;
`*(pt - 1) == '\n' means a new line being encountered;
DO NOT use `strtok()` function to get each number string and convert
it to an integer as follow:
char* line = "12 23 34";
line[2] = 0;
int num = atoi(firstNum); /* It is not good */
More smart manner as follow:
char* line = "12 23 34";
int num = atoi(line); /* It is good because it is more convenient */
*/
if (pt == buffer || *(pt - 1) == ' ' || *(pt - 1) == '\n')
arrays[i++] = atoi(pt); /* convert string literals to integers */
}
return (int*)arrays;
}
int main(void)
{
int i, j;
int row, col;
const char* filename = "dat.txt";
char* buffer = NULL;
int* matrix = NULL;
/* Read the data file `dat.txt` into a buffer */
buffer = matrix_read_file(filename);
if (buffer == NULL)
return 1;
/* To get the dimension of the matrix
row = matrix_get_row_num(buffer);
col = matrix_get_col_num(buffer);
if (row == 0 || col == 0)
{
free(buffer);
return 1;
}
/* To get the matrix */
matrix = matrix_parse_and_save(buffer, row, col);
/* Print the matrix */
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
/* matrix[i][j] */
printf("%d ", matrix[i * col + j]);
}
printf("\n");
}
free(buffer);
free(matrix);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我写给你!
linux G++s测试输出正常
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
typedef vector<vector<int> > Mat;
Mat input();
int
main (void)
{
Mat a = input();
for (int i = 0; i < a.size();i++)
{
for(int j = 0; j < a[i].size();j++)
{
cout<<a[i][j]<<" "<<flush;
}
cout<<endl;
}
return 0;
}
Mat input()
{
ifstream in("int.txt");
Mat a;
istringstream istr;
string str;
vector<int> tmpvec;
while(getline(in,str))
{
istr.str(str);
int tmp;
while(istr>>tmp)
{
tmpvec.push_back(tmp);
}
a.push_back(tmpvec);
tmpvec.clear();
istr.clear();
}
return a;
}
linux G++s测试输出正常
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
typedef vector<vector<int> > Mat;
Mat input();
int
main (void)
{
Mat a = input();
for (int i = 0; i < a.size();i++)
{
for(int j = 0; j < a[i].size();j++)
{
cout<<a[i][j]<<" "<<flush;
}
cout<<endl;
}
return 0;
}
Mat input()
{
ifstream in("int.txt");
Mat a;
istringstream istr;
string str;
vector<int> tmpvec;
while(getline(in,str))
{
istr.str(str);
int tmp;
while(istr>>tmp)
{
tmpvec.push_back(tmp);
}
a.push_back(tmpvec);
tmpvec.clear();
istr.clear();
}
return a;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
int a[1000][1000];
int i=0,j=0;
ifstream in;
in.open("xxx.txt");
while(!in.eof())
{
for(j=0;j<d;j++)
in>>a[i][j];
i++;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//file:intxt
//1 2 3 4 5
//2 2 3 4 5
//3 2 3 4 5
//4 2 3 4 5
//5 2 3 4 5
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <cassert>
#include <vector>
using namespace std;
const int col = 5;
const int row = 5;
int main() {
ifstream in;
in.open("/home/**xing123/in.txt");
vector<int> tmp;
string s;
while(getline(in, s)){
stringstream stre(s);
int t;
while(stre >> t){
tmp.push_back(t);
}
}
for(vector<int>::iterator pos = tmp.begin();pos != tmp.end();++pos){
cout << *pos << " ";
}
cout<<endl;
int Int[row][col];
vector<int>::iterator pos = tmp.begin();
for(int i = 0;i < row;++i){
for(int j = 0;j < col;++j){
Int[i][j] = *pos;
// cout << Int[i][j] << " ";
++pos;
}
cout<<endl;
}
return 0;
}
//1 2 3 4 5
//2 2 3 4 5
//3 2 3 4 5
//4 2 3 4 5
//5 2 3 4 5
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <cassert>
#include <vector>
using namespace std;
const int col = 5;
const int row = 5;
int main() {
ifstream in;
in.open("/home/**xing123/in.txt");
vector<int> tmp;
string s;
while(getline(in, s)){
stringstream stre(s);
int t;
while(stre >> t){
tmp.push_back(t);
}
}
for(vector<int>::iterator pos = tmp.begin();pos != tmp.end();++pos){
cout << *pos << " ";
}
cout<<endl;
int Int[row][col];
vector<int>::iterator pos = tmp.begin();
for(int i = 0;i < row;++i){
for(int j = 0;j < col;++j){
Int[i][j] = *pos;
// cout << Int[i][j] << " ";
++pos;
}
cout<<endl;
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询