944,066 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1869
  • C++ RSS
Jul 11th, 2006
0

file input don't know where to start

Expand Post »
Hello, this is my first post here. I'm taking c++ as an elective course, and I'm doing great until now. The problem I'm having is taking data from a txt file formatted line by line #### # ## for product id, store number, and quantity. I'm supposed to read this data and store it in three different arrays for each token. I think I can get it from there, but I don't know where to start, which library to use, confused by existing tutorials I've read, getting frustrated. Plz help.
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Jul 11th, 2006
0

Re: file input don't know where to start

You don't need anything complex. You will only have to use the <string>, <fstream> and <iostream> libraries. You will have to read some file input tutorials in C++. Even that is not that complex.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jul 11th, 2006
0

Re: file input don't know where to start

some progress made. i may have just needed the personal note --have been working on this stuff all day, the end of the term is coming on fast and we just took test 1 of 3 yesterday. here she is:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6.  
  7. bool loadArrays(const char fileName[], long idArray[],
  8. int storeArray[], int qtyArray[], int & count, int maxCells);
  9.  
  10. void printArrays(ostream & where, const long idArray[],
  11. const int storeArray[], const int qtArray[], int count);
  12.  
  13. bool extractData(const char newFileName[],int requestId, int baseQty,
  14. const long idArray[], const int storeArray[],
  15. const int qtArray[], int count, int & newcount);
  16.  
  17. const int ORDER_VALUE = 500;
  18.  
  19. int main() {
  20.  
  21.  
  22. /*
  23. fileName - the Windows name of the file created in step 1
  24. newFileName - the Windows file name of the file of extracted records
  25.  
  26. requestId - the product id number used to extract data
  27.   ORDER_VALUE - the quantity value used to extract data, make this
  28.   a global constant with a value of 500
  29.  
  30. idArray - the array of id numbers
  31. storeArray - the array of store numbers
  32. qtyArray - the array of quantities
  33.  
  34. count - the actual number of cells filled in the arrays
  35. newcount - the number of extracted records written to
  36.  
  37. */
  38. char fileName[] = "products.txt";
  39. char newFileName[] = "list.txt";
  40.  
  41. int requestID;
  42. int idArray[ORDER_VALUE], storeArray[ORDER_VALUE], qtyArray[ORDER_VALUE];
  43. int count = 0, newCount = 0;
  44.  
  45.  
  46. if( loadArrays(fileName, idArray, storeArray, qtyArray, count, 20) )
  47. printArrays(cout, idArray, storeArray, qtyArray, count);
  48.  
  49. return 0;
  50.  
  51. } // end main
  52.  
  53.  
  54.  
  55. bool loadArrays(const char fileName[],long idArray[],
  56. int storeArray[], int qtyArray[], int & count, int maxCells) {
  57.  
  58. ifstream in;
  59. in.open(fileName);
  60.  
  61. if(!in) return false;
  62.  
  63. for(int i = 0; !in.eof(); i++) {
  64. in >> idArray[i];
  65. in >> storeArray[i];
  66. in >> qtyArray[i];
  67. count = i;
  68. }
  69. in.close();
  70.  
  71. return true;
  72. }
  73.  
  74. void printArrays(ostream & where, const long idArray[],
  75. const int storeArray[], const int qtyArray[], int count) {
  76.  
  77. for(int i = 0; i <= count; i++)
  78. where << idArray[i] << " " << storeArray[i] << " " << qtyArray[i] << endl;
  79.  
  80.  
  81. }
  82.  
  83. bool extractData(const char newFileName[],int requestId, int baseQty,
  84. const long idArray[], const int storeArray[],
  85. const int qtArray[], int count, int & newcount) {
  86.  
  87.  
  88. }

which gives the following errors:
Quote ...
project03.cpp: In function ‘int main()’:
project03.cpp:48: error: invalid conversion from ‘int*’ to ‘long int*’
project03.cpp:48: error: initializing argument 2 of ‘bool loadArrays(const char*, long int*, int*, int*, int&, int)’
project03.cpp:49: error: invalid conversion from ‘int*’ to ‘const long int*’
project03.cpp:49: error: initializing argument 2 of ‘void printArrays(std::ostream&, const long int*, const int*, const int*, int)’
I'm using gnu compiler if this matters.. what am i doing wrong? thx
Last edited by Dave Sinkula; Jul 11th, 2006 at 5:33 pm.
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Jul 11th, 2006
0

Re: file input don't know where to start

That is because you have declared the second arguments of loadArray and printArray as a long array but you are inputing an int array.

Change the declaration of idArray from int to long, or just use int in the function declarations. I dont think you will want it to use long arrays. The range of int should be enough.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Jul 11th, 2006
0

Re: file input don't know where to start

oh, now i see that. thx
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006

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: Stacks using doubly linked lists
Next Thread in C++ Forum Timeline: Data Structures???





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


Follow us on Twitter


© 2011 DaniWeb® LLC