#include <fstream>
#include <string>
#include <limits>
using namespace std;
ifstream fin ("tv.in");
ofstream fout("tv.out");
struct time {
int PublicityTime;
int hh, mm, ss;
};
void getTime(const string& s, time& t) {
int x = 0;
int i = 0;
for (;s[i] != ' ';i ++)
x = x * 10 + s[i] - '0';
i ++;
t.PublicityTime = x;
x = 0;
for (;s[i] != ':';i ++)
x = x * 10 + s[i] - '0';
i ++;
t.hh = x;
x = 0;
for (;s[i] != ':';i ++)
x = x * 10 + s[i] - '0';
i ++;
t.mm = x;
x = 0;
for (;i < s.size();i ++)
x = x * 10 + s[i] - '0';
t.ss = x;
}
int main() {
int q, n;
char c;
string s;
time arr[100];
fin >> q;
fin >> n;
for (int i = 1;i <= n;i ++) {
getline(fin, s);
fout << s << '\n';
getTime(s, arr[i]);
}
return 0;
}
I want to read from a file that looks something like this
6
1
120 12:00:00
200 12:01:50
1000 13:00:00
2000 13:01:00
100 14:05:05
10 23:59:49
if i run the above code for the this file,i will get something like this
94217584 -1388646703:121:0
120 12:0:0
200 12:1:50
1000 13:0:0
2000 13:1:0
100 14:5:5
Can someone help undestand what is happening ? I've searched on google in order to find what would cause such an error and found that the problem would be that there are some '\n' that remain in the buffer, because the file looks something like this: "6\n1\n120 12:00:00\n200 12:01:50\n1000 13:00:00\n2000 13:01:00\n100 14:05:05\n10 23:59:49" I tried extracting both '\n' but the problem still isn't solved.
fin >> n;
fin >> c;
fin >> q;
fin >> c;