Input files with multiple columns

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 14
Reputation: NICEGUY123 is an unknown quantity at this point 
Solved Threads: 0
NICEGUY123 NICEGUY123 is offline Offline
Newbie Poster

Input files with multiple columns

 
0
  #1
Oct 17th, 2009
I need some help, fairly new to C++ and I know a little about C. What i am trying to do is get a txt file that looks like this

4 //first number tells how many rows are coming next.
3 54
2 51
9 32
2 34

what i want to do is get the numbers into a array so that i can use them later on in a function. Here is what i wrote so far, and is there a better way in C++.

  1.  
  2. # include <stdio.h>
  3.  
  4.  
  5. void READFile(char* filename)
  6. {
  7. int num;
  8.  
  9.  
  10.  
  11. FILE* inputfile;
  12. if ((inputfile = fopen(filename, "r")) ==NULL)
  13. {
  14. printf("file not found, unable to open\n");
  15.  
  16. }
  17.  
  18. fscanf( inputfile, "%d", num);
  19.  
  20. int color[num];
  21. int value[num];
  22.  
  23. for (int i = 0; i< num; i++);
  24. {
  25. fscanf(inputfile, "%d","%d", &color, &value);
  26. }
  27.  
  28.  
  29. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is online now Online
Posting Virtuoso
 
0
  #2
Oct 18th, 2009
Here's your code, translated in to C++.
  1. #include <fstream>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. void READFile(char* filename)
  7. {
  8. int num;
  9.  
  10. int color[100]; //make this large enough for any problem set
  11. int value[100];
  12.  
  13. ifstream inputfile;
  14.  
  15. inputfile.open( filename );
  16. if ( !inputfile )
  17. {
  18. cout << "file not found, unable to open\n";
  19. return; //added
  20. }
  21.  
  22. inputfile >> num ;
  23.  
  24. /*
  25. int color[num]; //oops, not yet legal in C++
  26. int value[num]; //although array declaration with variables
  27. //is supported in some compilers
  28. */
  29. for (int i = 0; i < num; i++);
  30. {
  31. inputfile >> color[i] >> value[i];
  32. }
  33. }

Note that by declaring the color and value arrays in the function, they only exist in that function. Perhaps they should be declared in main( ) and passed to the function?

I don't think your C input was quite correct, you input to the address of the array, but never incremented to fill other array elements. Should that have been
  1. fscanf(inputfile, "%d","%d", &color[i], &value[i]);
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC