How do I fill arrays with numbers from a .dat file (in C++)?
-
I have a two dimensional array that I need to fill with data from a .dat file. The .dat file contains its first segment as three columns of numbers and 522 rows. 522 is a global constant named M. The second segment has two columns and 891 rows. 891 is a global constant named N. The first two columns of the first segment are supposed to be in the 2D array called data[M][3] in the main function. The third column of the first segment of the .dat file is to be ignored. The first column of the second segment is supposed to be in a 1D array called color[N]. and the second column of the second segment is supposed to be in a 1D array called vertex[N]. M and N are unsigned ints. The problem is that when I cout the array values, the're all random (probably junk). Thanks for taking a look :-) Here's how my function is called in the main: int main() { double data[M][3]; unsigned int vertex[N]; int color[N]; read_dat(data, vertex, color); ....... Here's my function that's supposed to do it: void read_dat(double data[M][3], unsigned int vertex[N], int color[N]) { ifstream infile; unsigned int i, j, k, p, q; infile.open("shuttle.dat"); if(infile.fail()) { cout << "ERROR: input file opening failed." << endl; exit(1); } for(q = 0; q < M; q = q + 3) { infile.ignore(); // should get rid of the unnecessary third column } for(i = 1; i < M; ++i) { for(j = 0; j < 2; ++j) { infile >> data[i][j]; } } for(k = M + 1; k < M + N; ++k) { infile >> vertex[2*k]; // counts all even numbers (1st collumn) } for(p = M + 1; p < M + N; ++p) { infile >> color[2*p + 1]; // counts all odd numbers (2nd collumn) } }
-
Answer:
std::basic_istream::operator>>() and std::basic_ifstream::operator>>() on which your favorite classes are based will bounce once on the last item if you don't do this after reading your numbers or strings: infile >> data[i][j] >> std::ws; //be sure to eat whitespace after reading data so we can detect EOF! ifstream and cin both ignore whitespace BETWEEN data items (not including .getline()). use .good() to see if you have reached end of file or not or to see if something has failed. it's hard for me to tell without looking at your .dat file. I HOPE it has whitespace between the numbers. you should also .close() when you are finished with the file. when you do ignore(), exactly how much do you want to ignore? I think the default is 1 character. so you are probably going to have to munch some data and throw it away. this may be why you are getting garbage....
Tagg R at Yahoo! Answers Visit the source
Related Q & A:
- How to open a DAT file?Best solution by Yahoo! Answers
- If I have a form, such as an application, how can I fill it out using my computer?Best solution by answers.yahoo.com
- How would I fill out a MoneyGram to myself?Best solution by wikihow.com
- How is .txt file different from a .dat file in C?Best solution by mycplus.com
- How do I fill a CVS application?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.