[C++] Read numbers from file.
Hi i have a file "numberData.dat" with integers, one on each line :
Quote:
154
584
740
543
180
635
(ETC)
The code i am using to read it is:
Code:
int value = 0;
ifstream myFile("numberData.dat");
while (myFile >> value)
{
cout << value << endl;
}
myFile.close();
The problem is that it starts reading the numbers from the middle of the file, not at '154' which is the first number in the file. I tried many ways to read data from file but it all ends up the same. Any suggestions? Thanks!
Re: [C++] Read numbers from file.
Is there any other related code? In disbelief I just tested your code as it is and it works fine.
Re: [C++] Read numbers from file.
you can use StreamReader
or fread
Re: [C++] Read numbers from file.
Quote:
Originally Posted by
zolamu
you can use StreamReader
or fread
Wrong answer. Let's just figure out where the problem with this one is and not offer alternative solutions to where they're not needed.
Re: [C++] Read numbers from file.
Well I don't know if it matters or not but there are thousands of numbers in the .dat file, each on it's own line, that range from 0-1000. There is more code but I commented it out so that there's only that.
Posted via Mobile Device
Re: [C++] Read numbers from file.
Well how do you verify that not all numbers were printed? Could it be that your program works, but you just can't scroll back far enough for all of them to show because of limitation in your console window?
Re: [C++] Read numbers from file.
You should try and have to program copy these numbers in to a new file, that way you'll know if it gets all the numbers, and if the problem is just a line-limit in the console window.
Re: [C++] Read numbers from file.
Before I go on with my post, I'd like to say that I'm not familiar with C++, but what I would do is making the "numberData.dat" something like
Code:
177,
492,
216,
784,
983
and so on
and then load the file into a string and then split the string.
But as I said, I'm not familiar with C++.
Re: [C++] Read numbers from file.
You know what, I never even thought about a line limit, I'm new and just assumed you could view everything! I'll try it when I get back to school, by copyingthe numbers to a new file.
Posted via Mobile Device
Re: [C++] Read numbers from file.
Quote:
Originally Posted by
Andertraaks
Before I go on with my post, I'd like to say that I'm not familiar with C++, but what I would do is making the "numberData.dat" something like
Code:
177,
492,
216,
784,
983
and so on
and then load the file into a string and then split the string.
But as I said, I'm not familiar with C++.
That's really inefficient, in any language. Loading the entire file into a big string, splitting which requires allocation of lots of little strings, then memory management becomes a pain in the ass.
Just read each line into a buffer and convert to integer, the way the rest of the world does it in a low level language.
Re: [C++] Read numbers from file.
This is what I used:
Code:
#include <fstream.h>
#include <iostream.h>
int main()
{
int read;
ifstream in("file.in");
while(!in.eof())
{
in >> read;
cout << read;
}
return 0;
}
Re: [C++] Read numbers from file.
Not sure if this will work 100% or not.
PHP Code:
#include <stdio.h>
int main (int argc, char *argv[])
{
int nValue = 0, nIndex = 0;
FILE *pFile = fopen("numberData.dat", "r");
while (fscanf (pFile, "%i\n", &nIndex)) printf ("Value: %i\n", nValue);
return 0;
}
Have fun testing it.