Re: [Dev-C++] read numeric data in C++
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jeremy H. <jer...@co...> - 2005-03-10 18:20:55
|
Well, for C++ (as stated in the subject), this code works:
Code:
-----------------------------------------------
#include <iostream> //for basic C++ stuff
#include <sstream> //for stringstream
#include <fstream> //for ifstream
using namespace std;
int main()
{
int i;
ifstream fin("file.txt");
stringstream istr;
string holder;
if (!fin)
{
cerr << "The world has ended! Abort!" << endl;
exit(1);
}
while (fin >> holder)
{
istr << holder;
istr >> i;
cout << holder << "/" << i << endl;
istr.clear(); //stringstreams suck, you have to do
//this to get it to work properly.
}
};
----------------------------------------------
Where file.txt contains:
10
20
30
40
50
60
70
80
90
100
Incidently, anyone want to take a crack at why I have to clear the
string of the stringstream for it to work properly? If I don't, I get
this:
10/10
20/10
30/10
40/10
50/10
60/10
70/10
80/10
90/10
100/10
-- Jeremy
Johan Parmler wrote:
> Hello
>
> I am a new user of C and I need to load and save numeric data from and
> to an ascii file.
>
> Does someone have an example or any tips to make this?
>
> Johan parmler
>
|