467,081 Members | 1,275 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,081 developers. It's quick & easy.

Help with program reading a data file with multiple columns

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
  • viewed: 4003
Share:
4 Replies
sicarie
Expert Mod 4TB
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
Oops, sorry. I'd like help with getting the columns.
Mar 22 '07 #3
sicarie
Expert Mod 4TB
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 1GB
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.

Similar topics

4 posts views Thread by Roberto Dias | last post: by
1 post views Thread by Rahul | last post: by
8 posts views Thread by skumar434@gmail.com | last post: by
5 posts views Thread by Sam | last post: by
3 posts views Thread by Don | last post: by
118 posts views Thread by Chuck Cheeze | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.