У меня возникла проблема с импортом некоторых данных из txt-файла.Не удается импортировать все данные файла txt
форматированных данных в файл .txt файла:
(INT INT дважды), например, (16 21 18.0)
Я создал функцию для получения данных из файла, и она работает, но она просто дает мне первую строку (то есть первый объект чтения), затем она останавливается с возвратом 0 без ошибок. Наверное, у меня проблема с входным циклом, но я не понимаю, какая проблема. Я должен импортировать все 50 значений, хранящихся в txt-файле (в 50 строк и один столбец). Спасибо заранее.
Header3.h
#pragma once
struct Reading
{
int day, hour;
double temperature;
char ch1, ch2;
Reading();
Reading(char c1, int d, int h, double t, char c2);
};
istream& operator>>(istream& is, Reading& r); // Setting helper input function
ostream& operator<<(ostream& os, const vector<Reading>& r); // Setting helper output function
void vec_import(vector<Reading>& v); // Read data from txt file
temp_stats.cpp (исходный файл header3 в)
#include "stdafx.h"
#include "std_lib_facilities.h"
#include "Header3.h"
void vec_import(vector<Reading>& v)
{
string iname;
cout << "Type input filename: ";
cin >> iname;
ifstream ist{ iname };
if (!ist) error("Cannot read from filename ", iname);
while (true)
{
Reading r;
ist >> r;
if (!ist) break;
v.push_back(r);
}
}
Reading::Reading()
:ch1{ ' ' }, day{ 0 }, hour{ 0 }, temperature{ 0 }, ch2{ ' ' } {};
Reading::Reading(char c1, int d, int h, double t, char c2)
:ch1{ c1 }, day{ d }, hour{ h }, temperature{ t }, ch2{ c2 } {};
istream& operator>>(istream& is, Reading& r)
{
int d, h;
double t;
char c1;
char c2;
is >> c1;
switch (c1)
{
case '(':
break;
default:
break;
return is;
}
is >> d >> h >> t;
is >> c2;
switch (c2)
{
case ')':
break;
default:
break;
return is;
}
r = Reading(c1, d, h, t, c2);
return is;
}
ostream& operator<<(ostream& os, const vector<Reading>& r)
{
for (int i = 0; i < r.size(); ++i)
{
return os << r[i].ch1 << " " << r[i].day << " " << r[i].hour
<< " " << setprecision(1) << fixed << r[i].temperature
<< " " << r[i].ch2 << endl;
}
}
main.cpp
// Temperature Reading 2.0.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "std_lib_facilities.h"
#include "Header3.h"
int main()
{
Reading r;
vector<Reading> v;
vec_import(v);
cout << v;
keep_window_open();
return 0;
}
Txt файла:
Большое спасибо ... Я имею дело от нескольких часов со вспомогательными функциями, и я не смог правильно установить его. –