C++ 从 txt 读取数字直到换行(逻辑回归)
C++ reading numbers from txt until change of line (logistic regression)
我的编程真的很糟糕,但我想做这个,我不想停止 trying.It 我的课程 A.I。
所以,我有一个 txt 文件 (train1.txt),其中包含这样的一系列数字:
0 0 0 12 14 55 250 0 0 1 14 44 5 4 0 0
0 0 0 0 1 2 55 89 201 4
0 45 78 98 65 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
.
.
.
它大约有 6000 行,每行有 1700 个数字。每行应该是 28*28 像素的图像(我不太明白这一点)。
我需要做一个逻辑回归算法(将手写字符分类为一和七)所以我需要将这些数字保存到 array/vector 中并进行一些计算。
到目前为止我所做的是这个
int main()
{
ifstream file("train1.txt");
string line;
vector<int> train1 (20000);//if i don't give a size it won't work
if (file.is_open())
{
int i=0;
int k=0;
int tens,hundreds;
if(getline(file,line))
{
while (line[i] !='\n')//i want to do this till the end of the line
{
if(line[i]==' ')//if it's a space just go to the next one
{i++;}
else //it has for sure 1 digit
{
tens=0;
hundreds=0;
i++; //check the next one
if(line[i] !=' ') //does it have 2 digits?
{
i++; //check the next one
if (line[i] !=' ') //does it have 3 digits?
{ //3 digit number
cout<<line[i-2]<<line[i-1]<<line[i]<<endl;
hundreds = (line[i-2] - '0')*100;
tens = (line[i-1] - '0')*10;
train1[k]=hundreds + tens + line[i]- '0';;
k++;
i++;
}
else//just 2digit number
{
cout<<line[i-1]<<line[i]<<endl;
tens = (line[i-1] - '0')*10;
train1[k]=tens +line[i]- '0';
k++;
i++;
}
}
else
{//just 1digit number
cout<<line[i-1]<<endl;
train1[k]=line[i-1] - '0';
k++;
i++;
}
}
}
}
}
system("PAUSE");
return 0;
}
问题是当换行时我无法阻止它。它工作正常我认为但最后它说 "string subscript out of range"。那是因为当我处于行的最后一个数字时,我仍然执行 i++ 并尝试访问行 [i],对吗?
您可以从字符串 ("stringstream") 构建流并使用它来读取数字 - 它的工作方式与任何其他流一样。
你会得到这样的结果:
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::ifstream file("train1.txt");
std::string line;
while (std::getline(file,line))
{
std::vector<int> numbers;
std::stringstream linestream(line);
int value = 0;
while (linestream >> value)
{
numbers.push_back(value);
}
// Do something with the data from this line...
do_something_with(numbers);
}
}
我的编程真的很糟糕,但我想做这个,我不想停止 trying.It 我的课程 A.I。 所以,我有一个 txt 文件 (train1.txt),其中包含这样的一系列数字:
0 0 0 12 14 55 250 0 0 1 14 44 5 4 0 0
0 0 0 0 1 2 55 89 201 4
0 45 78 98 65 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
.
.
.
它大约有 6000 行,每行有 1700 个数字。每行应该是 28*28 像素的图像(我不太明白这一点)。 我需要做一个逻辑回归算法(将手写字符分类为一和七)所以我需要将这些数字保存到 array/vector 中并进行一些计算。 到目前为止我所做的是这个
int main()
{
ifstream file("train1.txt");
string line;
vector<int> train1 (20000);//if i don't give a size it won't work
if (file.is_open())
{
int i=0;
int k=0;
int tens,hundreds;
if(getline(file,line))
{
while (line[i] !='\n')//i want to do this till the end of the line
{
if(line[i]==' ')//if it's a space just go to the next one
{i++;}
else //it has for sure 1 digit
{
tens=0;
hundreds=0;
i++; //check the next one
if(line[i] !=' ') //does it have 2 digits?
{
i++; //check the next one
if (line[i] !=' ') //does it have 3 digits?
{ //3 digit number
cout<<line[i-2]<<line[i-1]<<line[i]<<endl;
hundreds = (line[i-2] - '0')*100;
tens = (line[i-1] - '0')*10;
train1[k]=hundreds + tens + line[i]- '0';;
k++;
i++;
}
else//just 2digit number
{
cout<<line[i-1]<<line[i]<<endl;
tens = (line[i-1] - '0')*10;
train1[k]=tens +line[i]- '0';
k++;
i++;
}
}
else
{//just 1digit number
cout<<line[i-1]<<endl;
train1[k]=line[i-1] - '0';
k++;
i++;
}
}
}
}
}
system("PAUSE");
return 0;
}
问题是当换行时我无法阻止它。它工作正常我认为但最后它说 "string subscript out of range"。那是因为当我处于行的最后一个数字时,我仍然执行 i++ 并尝试访问行 [i],对吗?
您可以从字符串 ("stringstream") 构建流并使用它来读取数字 - 它的工作方式与任何其他流一样。
你会得到这样的结果:
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::ifstream file("train1.txt");
std::string line;
while (std::getline(file,line))
{
std::vector<int> numbers;
std::stringstream linestream(line);
int value = 0;
while (linestream >> value)
{
numbers.push_back(value);
}
// Do something with the data from this line...
do_something_with(numbers);
}
}