944,067 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1208
  • C++ RSS
Oct 17th, 2009
0

Input files with multiple columns

Expand Post »
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++.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NICEGUY123 is offline Offline
18 posts
since Oct 2009
Oct 18th, 2009
0
Re: Input files with multiple columns
Here's your code, translated in to C++.
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  1. fscanf(inputfile, "%d","%d", &color[i], &value[i]);
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: How does one tolower an entire string?
Next Thread in C++ Forum Timeline: Help with the number guessing game!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC