C++读文档问题 想读取以下txt文档,然后把数字转换成int
AlbertEinstein526763SteveAbrew90869093DavidNagasake100859389MikeBlack81878185AndrewVa...
Albert Einstein 52 67 63
Steve Abrew 90 86 90 93
David Nagasake 100 85 93 89
Mike Black 81 87 81 85
Andrew Van Den 90 82 95 87
Joanne Dong Nguyen 84 80 95 91
Chris Walljasper 86 100 96 89
Fred Albert 70 68
Dennis Dudley 74 79 77 81
Leo Rice 95
Fred Flinstone 73 81 78 74
Frances Dupre 82 76 79
Dave Light 89 76 91 83
Hua Tran Du 91 81 87 94
Sarah Trapp 83 98
ifstream fin;
fin.open("student.txt");
if(!fin.good())
{
cout<<"Could not open the file ."<<endl;
exit(1);
}
while(!fin.eof())
{
record = new char[50];
fin.getline(record,50,'\n');
strlen = Length_rec(record);
//cout<<record<<endl;
//cout<<strlen<<endl;
StoreRec(strlen,record);
if(strlen==0)
break;
}
}
int Length_rec(char a[])
{
int i=0;
while(a[i])
i++;
return i;
}
void StoreRec(int strlen,char record[])
{
char name[50];
int count = 0;
char mark_str[10];
int num_int[5];
int k = 0;
int mark_int[5];
int rec_int = 0;
for (int i=0;i<strlen;i++) //get name with space
{
if((record[i]>='a'&& record[i]<='z') || (record[i]>='A'&& record[i]<='Z' || record[i]==32))
{
name[i] = record[i];
count++;
}
else if(record[i]>=48 && record[i]<=57) //if record is number,break
break;
}
for(int i=count,j=0;i<strlen;i++) //get marks without space
{
if(record[i]!=' ' && i!=strlen-1)
{
mark_str[j] = record[i]; //storing the number one by one
j++;
//cout<<mark_str[j];
}
else
{
//cout<<"ELSE!!!"<<endl;
//cout<<" "<<mark_str<<endl;
//cout<<mark<<endl;
num_int[k] = atoi(mark_str);
//cout<<num_int[k]<<endl;
mark_int[rec_int] = num_int[k];
k++;
rec_int++;
j=0; //when the number finish ,default the array
}
}
for(int i=0;i<4;i++)
{
cout<<mark_int[i]<<" ";
}
cout<<endl;
}
为什么我写的代码(不是很会写,所以有点乱),有些数据后的数字后全多了个0呢(如下)
52 67 63 0
90 86 90 93
100 850 930 890
81 87 81 85
90 82 95 87
84 80 95 91
86 100 960 890 //应该是86 100 96 89
70 68 960 890 // 70 68 96 89
74 79 77 81
95 79 77 81
73 81 78 74
82 76 79 74
89 76 91 83
91 81 87 94
83 93 1023 0 //应该只有83 93 展开
Steve Abrew 90 86 90 93
David Nagasake 100 85 93 89
Mike Black 81 87 81 85
Andrew Van Den 90 82 95 87
Joanne Dong Nguyen 84 80 95 91
Chris Walljasper 86 100 96 89
Fred Albert 70 68
Dennis Dudley 74 79 77 81
Leo Rice 95
Fred Flinstone 73 81 78 74
Frances Dupre 82 76 79
Dave Light 89 76 91 83
Hua Tran Du 91 81 87 94
Sarah Trapp 83 98
ifstream fin;
fin.open("student.txt");
if(!fin.good())
{
cout<<"Could not open the file ."<<endl;
exit(1);
}
while(!fin.eof())
{
record = new char[50];
fin.getline(record,50,'\n');
strlen = Length_rec(record);
//cout<<record<<endl;
//cout<<strlen<<endl;
StoreRec(strlen,record);
if(strlen==0)
break;
}
}
int Length_rec(char a[])
{
int i=0;
while(a[i])
i++;
return i;
}
void StoreRec(int strlen,char record[])
{
char name[50];
int count = 0;
char mark_str[10];
int num_int[5];
int k = 0;
int mark_int[5];
int rec_int = 0;
for (int i=0;i<strlen;i++) //get name with space
{
if((record[i]>='a'&& record[i]<='z') || (record[i]>='A'&& record[i]<='Z' || record[i]==32))
{
name[i] = record[i];
count++;
}
else if(record[i]>=48 && record[i]<=57) //if record is number,break
break;
}
for(int i=count,j=0;i<strlen;i++) //get marks without space
{
if(record[i]!=' ' && i!=strlen-1)
{
mark_str[j] = record[i]; //storing the number one by one
j++;
//cout<<mark_str[j];
}
else
{
//cout<<"ELSE!!!"<<endl;
//cout<<" "<<mark_str<<endl;
//cout<<mark<<endl;
num_int[k] = atoi(mark_str);
//cout<<num_int[k]<<endl;
mark_int[rec_int] = num_int[k];
k++;
rec_int++;
j=0; //when the number finish ,default the array
}
}
for(int i=0;i<4;i++)
{
cout<<mark_int[i]<<" ";
}
cout<<endl;
}
为什么我写的代码(不是很会写,所以有点乱),有些数据后的数字后全多了个0呢(如下)
52 67 63 0
90 86 90 93
100 850 930 890
81 87 81 85
90 82 95 87
84 80 95 91
86 100 960 890 //应该是86 100 96 89
70 68 960 890 // 70 68 96 89
74 79 77 81
95 79 77 81
73 81 78 74
82 76 79 74
89 76 91 83
91 81 87 94
83 93 1023 0 //应该只有83 93 展开
1个回答
展开全部
可以用动态数组,动态分配内存。VC++ 用虚拟存储,数组大,不一定有问题。你可以试一下。
这里一次读6个数,处理6个数,给你说明如何读实数:
#include<iostream>
#include<fstream> // 头文件
using namespace std;
void main ()
{
ifstream infile;
double x[6];
int i;
infile.open ("test.txt", ifstream::in); // 打开文件
while(1){
for (i=0;i<6;i++) infile >> x[i]; // 读6个数
// 运算可以加在这里
for (i=0;i<6;i++) cout << x[i] << endl; //输出6个数
if (infile.eof()) break; //判断文件是否结束
}
infile.close(); //关闭文件。
}
===========
动态分配:
void main ()
{
ifstream infile;
double *x;
int i;
x = (double *) malloc(sizeof(double) * 10000000);
if (!x){
cout << "no enough memory for x";
}
infile.open ("test.txt", ifstream::in);
i=0;
while(1){
infile >> x[i];
if (infile.eof()) break;
cout << x[i] << endl;
i++;
}
infile.close();
}
这里一次读6个数,处理6个数,给你说明如何读实数:
#include<iostream>
#include<fstream> // 头文件
using namespace std;
void main ()
{
ifstream infile;
double x[6];
int i;
infile.open ("test.txt", ifstream::in); // 打开文件
while(1){
for (i=0;i<6;i++) infile >> x[i]; // 读6个数
// 运算可以加在这里
for (i=0;i<6;i++) cout << x[i] << endl; //输出6个数
if (infile.eof()) break; //判断文件是否结束
}
infile.close(); //关闭文件。
}
===========
动态分配:
void main ()
{
ifstream infile;
double *x;
int i;
x = (double *) malloc(sizeof(double) * 10000000);
if (!x){
cout << "no enough memory for x";
}
infile.open ("test.txt", ifstream::in);
i=0;
while(1){
infile >> x[i];
if (infile.eof()) break;
cout << x[i] << endl;
i++;
}
infile.close();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询