#include
#include
#include
void read_txt_with_blank_space(std::string path, std::vector<float> &elements)
{
std::ifstream infile(path);
if (infile.good())
{
std::string line;
while(getline(infile, line))
{
std::stringstream data(line);
float n1, n2, n3;
std::cout << "line: " << line << std::endl;
if(data >> n1 >> n2 >> n3)
{
std::cout<< "elem: " << n1 << " " << n2 << " " << n3 << std::endl;
elements.push_back(n1);
elements.push_back(n2);
elements.push_back(n3);
}
}
infile.close();
}
}
void save_txt_with_blank_space(std::vector<float> elements)
{
std::ofstream f_box;
f_box.open("Demo_ReWrite.txt",std::ios::out|std::ios::app);
for (int i=0; i<elements.size(); i=i+3)
{
float n1=elements[i];
float n2=elements[i+1];
float n3=elements[i+2];
f_box << n1<< " " << n2<< " " << n3 << std::endl;
}
f_box.close();
}
void read_txt_with_comma(std::string path, std::vector<float> &nums)
{
std::ifstream file(txt_path);
std::string line;
std::vector<float> nums;
while(getline(file, line)) {
std::istringstream iss(line);
std::string num_str;
while(getline(iss, num_str, ',')) {
float num;
std::istringstream(num_str) >> num;
nums.push_back(num);
}
}
}
int main()
{
std::string path="demo.txt";
std::vector<float> elements1;
read_txt_with_blank_space(path,elements1);
save_txt_with_blank_space(elements1);
std::string path="demo1.txt";
std::vector<float> elements2;
read_txt_with_comma(path,elements2);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82