By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,201 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Help with program reading a data file with multiple columns

P: 2
I'm trying to read one column at a time from this data file:

24551 145.5 62.3 2.13
24582 153.7 63.0 2.52
26553 160.4 58.8 2.51
26613 159.5 58.9 2.02
26624 160.5 61.5 1.98
27725 170.9 62.5 2.03
27832 147.8 65.6 2.12
27976 155.4 71.0 2.34
28061 162.7 57.9 2.61
28251 171.3 59.2 2.23
28347 164.8 65.0 2.45
28782 152.8 58.6 2.05
28788 167.2 67.3 1.89
29192 149.6 60.9 2.26
29227 172.0 67.8 2.33
29482 155.2 73.1 2.47
29522 148.5 72.1 2.27
29617 168.3 49.7 2.63
29785 169.4 64.4 1.87
29885 146.6 55.9 2.42
29943 170.3 66.8 2.31
29957 166.3 56.8 2.11
29968 162.8 64.6 2.68
29987 149.2 69.7 2.22

I'm starting with the second column, and have to eventually do the third and fourth do. I have to take the data from the second column, and find the percent of data that is over 170 or under 150. This is what I have so far:
Expand|Select|Wrap|Line Numbers
  1. #include <fstream>
  2. #include <cmath>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <stdio.h>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     char str [80];
  12.     int temp, pressure, dwell, data;
  13.  
  14. ifstream infile;
  15. infile.open("sutures.dat");
  16. if (!infile)
  17. {cout << "Unable to open problem1.data file";
  18. exit(0);}
  19. else
  20. {infile >> data;
  21. cout << "Successfully read data: ";
  22. ;
  23.  
  24.  
  25. printf("Rejected due to temperature: ");
  26. scanf("("%*d %d %*d %*d", &int1[24], &int2[24], &int3[24], &int4[24]);
Mar 22 '07 #1
Share this Question
Share on Google+
4 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
1) Please use code tags around your code snippets
2) Check out our Posting Guidelines and ask a pointed question. Do you want help with your algorithm, with getting the columns? Filehandles?
Mar 22 '07 #2

P: 2
Oops, sorry. I'd like help with getting the columns.
Mar 22 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Oops, sorry. I'd like help with getting the columns.
Using C++, you can use getline() and strtok(), or filehandle >> variable.

If you want to get them as strings, I'd say use getline() and strok, you can pull the whole thing apart right there, if your'e looking to pull in floats, you might consider the 'filehandle >> float_variable' way (and just ignore the first number - cin 2x: filehandle >> ignore_var >> float_var or something like that).
Mar 22 '07 #4

Expert 100+
P: 1,510
I would have thought if you were going to need the information from other columns later in the program the simplest thing would be to read all the columns in to a 2D array or vector - or create a structure to hold a row then read the rows into an array or vector. Otherwise you have to reread the file several times.
Mar 22 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.