How do I use CIN to read values from a file (64 bit integer)
Let's say I have the file input.txt contain开发者_JAVA百科ing the following numbers :
2 1 1 888450282
1 2
I need to read the first line in separate variables (a,b,c,d). The big value can be big like a 64 bit integer. How do I use C++ IO to do this? And the second line can have 1 to N values.
Normally I do this in C but I want to learn the C++ library and I'm also not very confortable with 64 bit integers in C++.
You use iostream in the usual way, i.e. read it into a 64-bit-sized integer:
#include <stdint.h>
uint64_t value;
std::cin >> value;
BTW, you could also use stdio in the form
#include <inttypes.h>
#include <stdint.h>
uint64_t value;
fscanf(file, "%"PRiu64"", &value);
If you are only concerned about the first line of the file you can use something like the following to get it.
#include <fstream>
#include <iostream>
And the following code will handle the file.
ifstream file("yourfile.txt", ios::in);
int a, b, c;
long long d;
file >> a >> b >> c >> d;
printf("a: %d, b: %d, c: %d, d: %lld", a, b, c, d);
file.close();
精彩评论