C++如何把文本文件中的数据一行一行读出后存到数组里面
C++怎么把文本文件中的数据一行一行读出后存到数组里面
如:
14.8710373022865490
14.4078392848202680
14.4461924174540820
14.0572268190424620
13.6271417430539470
13.4037407958168820
12.7490292405028750
12.2693965610416470
12.0518395076865090
11.7411374917238780
C++一点不会,着急用,麻烦从头到尾写详细点,谢谢!~
------解决方案--------------------
我给你个例子,包含了对话框的:
void CDemoDoc::ReadFromDisk(CArray <Template_info,Template_info> & ClassfyTemplate,CString & strPath)
{
Template_info a;
CFile file;
int k;
if(file.Open(strPath,CFile::modeRead,NULL))
{
CString tmpStr;
TCHAR* tBuf=tmpStr.GetBuffer(file.GetLength());
file.Read(tBuf,file.GetLength());
tmpStr=tBuf;
int l;
int k=0;
for(int j=0;j <file.GetLength();j++)
{
if(*(tBuf+j)==48)
{
float d=atof(tmpStr.Mid(j,8));
a.Center[l]=d;
l++;
j=j+9;
if(l==64)
k=1;
if(l> 64)
{
AfxMessageBox(_T( "聚类中心数据读取错误! "));
return;
}
if(ClassfyTemplate.GetSize()> 20)
{
AfxMessageBox(_T( "聚类种数读取错误! "));
return;
}
}
else
{
if(k!=0)
{
ClassfyTemplate.Add(a);
k=0;
}
Hanzi_info m;
m.name=tmpStr.Mid(j,2);
a.Template1.push_back(m);
l=0;
j++;
}
if(ClassfyTemplate.GetSize()==19 && k!=0)
{
ClassfyTemplate.Add(a);
k=0;
}
}
}
return;
}
我是按自己数据格式读取的,你自己改改就可以了。
------解决方案--------------------
C代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f;
char buf[BUF_SIZE];
f = fopen( "filename ", "r ");
if(f == NULL)
{
return;
}
while(!feof(f))
{
fgets(buf,sizeof(buf),f);
//Do something
}
return 0;
}
------解决方案--------------------
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
int main(){
FILE* file = NULL;
file = fopen( "main.cpp ", "r ");
char** str = NULL;
str = new char*[60];
for (int i = 0; i < 60; i++) {
str[i] = new char[80];
}
int row = 0;
while (!feof(file)) {
fgets(str[row++], 80, file);
}
for (int i = 0; i < row; i++) {
cout < < str[i];
}
return 0;
}
这个程序就可以
------解决方案--------------------
#include <vector>
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream ifile( "test.txt ");
vector <string> str;
string line;
while (!ifile.eof())
{
getline(ifile, line); //读取一行
//cout < < "line: " < <line < <endl;
str.push_back(line); //插入到 vector
}
cout < <endl;
int len=str.size(), i;
如:
14.8710373022865490
14.4078392848202680
14.4461924174540820
14.0572268190424620
13.6271417430539470
13.4037407958168820
12.7490292405028750
12.2693965610416470
12.0518395076865090
11.7411374917238780
C++一点不会,着急用,麻烦从头到尾写详细点,谢谢!~
------解决方案--------------------
我给你个例子,包含了对话框的:
void CDemoDoc::ReadFromDisk(CArray <Template_info,Template_info> & ClassfyTemplate,CString & strPath)
{
Template_info a;
CFile file;
int k;
if(file.Open(strPath,CFile::modeRead,NULL))
{
CString tmpStr;
TCHAR* tBuf=tmpStr.GetBuffer(file.GetLength());
file.Read(tBuf,file.GetLength());
tmpStr=tBuf;
int l;
int k=0;
for(int j=0;j <file.GetLength();j++)
{
if(*(tBuf+j)==48)
{
float d=atof(tmpStr.Mid(j,8));
a.Center[l]=d;
l++;
j=j+9;
if(l==64)
k=1;
if(l> 64)
{
AfxMessageBox(_T( "聚类中心数据读取错误! "));
return;
}
if(ClassfyTemplate.GetSize()> 20)
{
AfxMessageBox(_T( "聚类种数读取错误! "));
return;
}
}
else
{
if(k!=0)
{
ClassfyTemplate.Add(a);
k=0;
}
Hanzi_info m;
m.name=tmpStr.Mid(j,2);
a.Template1.push_back(m);
l=0;
j++;
}
if(ClassfyTemplate.GetSize()==19 && k!=0)
{
ClassfyTemplate.Add(a);
k=0;
}
}
}
return;
}
我是按自己数据格式读取的,你自己改改就可以了。
------解决方案--------------------
C代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f;
char buf[BUF_SIZE];
f = fopen( "filename ", "r ");
if(f == NULL)
{
return;
}
while(!feof(f))
{
fgets(buf,sizeof(buf),f);
//Do something
}
return 0;
}
------解决方案--------------------
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
int main(){
FILE* file = NULL;
file = fopen( "main.cpp ", "r ");
char** str = NULL;
str = new char*[60];
for (int i = 0; i < 60; i++) {
str[i] = new char[80];
}
int row = 0;
while (!feof(file)) {
fgets(str[row++], 80, file);
}
for (int i = 0; i < row; i++) {
cout < < str[i];
}
return 0;
}
这个程序就可以
------解决方案--------------------
#include <vector>
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream ifile( "test.txt ");
vector <string> str;
string line;
while (!ifile.eof())
{
getline(ifile, line); //读取一行
//cout < < "line: " < <line < <endl;
str.push_back(line); //插入到 vector
}
cout < <endl;
int len=str.size(), i;