file input don't know where to start

Reply

Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

file input don't know where to start

 
0
  #1
Jul 11th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: file input don't know where to start

 
0
  #2
Jul 11th, 2006
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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: file input don't know where to start

 
0
  #3
Jul 11th, 2006
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:

  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:
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: file input don't know where to start

 
0
  #4
Jul 11th, 2006
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.
バルサミコ酢やっぱいらへんで
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: file input don't know where to start

 
0
  #5
Jul 11th, 2006
oh, now i see that. thx
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC