64,282
社区成员
发帖
与我相关
我的任务
分享
#include <fstream>// file stream
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
void main()
{
int getFileRows(const string fileName);
int getFileColumns(const string fileName);
int getInputData(const string fileName, int rowNum, int colNum, string X, string Y);
string filename;
string location;
filename = "test.txt";
location = "D:\Visual Studio 2015 - My data\Read-0208\Read-0208" + filename;
int R = getFileRows(filename);
int C = getFileColumns(filename);
string *Y = new string[R];
string *X = new string[R*(C - 1)];
int F = getInputData(filename, R, C, *X, *Y);
cout << "FileRows = " << R << "\n";
cout << "FileColumns = " << C << "\n";
cout << F << endl;
delete[] Y, X;
}
/***********************************获取矩阵行数**************************************************/
int getFileRows(const string fileName)//const防止被意外修改
{
ifstream fileStream;
string tmp;
int count = 0;//行数计数器
fileStream.open(fileName,ios::in); //ios::in表示只读
if (fileStream.fail())
{
return 0;
}
else//如果文件存在
{
while (getline(fileStream, tmp, '\n'))//读取一行
{
if (tmp.size() > 0)
count++;
}
fileStream.close();
return count;
}
}
/***********************************获取矩阵列数**************************************************/
int getFileColumns(const string fileName)
{
ifstream fileStream;
fileStream.open(fileName, ios::in);
string tmp;
int count = 0;
char c;
c = fileStream.peek();
while (('\n' != c) && (!fileStream.eof()))
{
fileStream >> tmp;
//tmp << fileStream.rdbuf();//把文件流中的字符输入到字符串流中
//getline(fileStream, tmp, ' ');
//if (' ' != c)
//{
++count;
//}
c = fileStream.peek();
//cout << c << endl;
}
fileStream.close();
return count;
}
/***********************************获取矩阵数据**************************************************/
int getInputData(const string fileName, int rowNum, int colNum, string *X[], string *Y[])
{
ifstream fileStream;
string oneLine = "";
//char tmp;//当前位置上的数值
int rowCount;//行数计数器
int colCount;//列数计数器
int index = 0;
int maxIndex = rowNum*(colNum - 1) - 1;
//打开文件
fileStream.open(fileName, ios::in);
if (fileStream.fail())//文件打开失败,返回0
{
cout << "文件不存在!" << endl;
fileStream.close();
system("pause");
return 0;
}
else//文件存在
{
//读入数据
rowCount = 0;//初始化当前行数为0
colCount = 0;//初始化当前列数为0
string tmp;//当前读入的值
while (!fileStream.eof())
{
fileStream >> tmp;
cout << tmp << endl;//测试句
if (colCount == 0)
{
if (rowCount < rowNum)//越界检查,第一列是Y
{
*Y[rowCount] = tmp;
cout << *Y[rowCount] << endl;
}
else
{
cout << "计算出的行数与输入数据不符,请检查数据(如文件末尾不能有空行)." << endl;
fileStream.close();
system("pause");
return 0;
}
}
else//非第一列是数据X
{
index = rowCount*(colNum - 1) + colCount - 1;
if (index <= maxIndex)
{
*X[index] = tmp;
cout << *X[index] << tmp;
}
else
{
cout << "X[]下标超出数组范围,可能是文件中某行的列数>第一行的列数,退出!" << endl;
fileStream.close();
system("pause");
return 0;
}
}
if ('\n' != fileStream.peek())
{
++colCount;
}
else
{
if ((colCount + 1 != colNum))
{
cout << "第" << rowCount + 1 << "行,输入的列数与文件列数不一致,请检查数据." << endl;
fileStream.close();
system("pause");
return 0;
}
else
{
rowCount++;
colCount = 0;
}
}
}
fileStream.close();
return 1;
}
}