User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,802 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,455 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 1092 | Replies: 4
Reply
Join Date: Jul 2006
Posts: 155
Reputation: tefflox is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

file input don't know where to start

  #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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: file input don't know where to start

  #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  
Join Date: Jul 2006
Posts: 155
Reputation: tefflox is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Question Re: file input don't know where to start

  #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:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;


bool  loadArrays(const char fileName[], long idArray[], 
	int  storeArray[], int qtyArray[], int & count, int maxCells);

void printArrays(ostream & where, const long idArray[], 
           const int storeArray[], const int qtArray[], int  count);

bool extractData(const char newFileName[],int requestId, int baseQty,
          const long idArray[], const  int storeArray[],
		const int qtArray[], int  count, int & newcount);

const int ORDER_VALUE = 500;

int main() {


/*
fileName 	 - the Windows name of the file created in step 1
newFileName	 - the Windows file name of the file of extracted records

requestId	 - the product id number used to extract data
   ORDER_VALUE  - the quantity value used to extract data, make this
                     a global constant with a value of 500	

	idArray	 - the array of id numbers
	storeArray	 - the array of store numbers
	qtyArray	 - the array of quantities

	count		 - the actual number of cells filled in the arrays
	newcount	 - the number of extracted records written to 

*/
	char fileName[] 	= "products.txt";
	char newFileName[]  = "list.txt"; 

	int requestID;
	int idArray[ORDER_VALUE], storeArray[ORDER_VALUE], qtyArray[ORDER_VALUE];
	int count = 0, newCount = 0;
	

	if( loadArrays(fileName, idArray, storeArray, qtyArray, count, 20) )
		printArrays(cout, idArray, storeArray, qtyArray, count);

	return 0;

} // end main



bool  loadArrays(const char fileName[],long idArray[], 
				int  storeArray[], int qtyArray[], int & count, int maxCells) {
	
	ifstream in;
	in.open(fileName);

	if(!in) return false;

	for(int i = 0; !in.eof(); i++) {
		in >> idArray[i];
		in >> storeArray[i];
		in >> qtyArray[i];
		count = i;
	}
	in.close();

	return true;
}

void printArrays(ostream & where, const long idArray[], 
                const int  storeArray[], const int qtyArray[], int count) {

	for(int i = 0; i <= count; i++) 
		where << idArray[i] << " " << storeArray[i] << " " << qtyArray[i] << endl;


}

bool extractData(const char newFileName[],int requestId, int baseQty,
               	const long idArray[], const  int storeArray[],
		   		const int qtArray[], int  count, int & newcount) {


}

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 4:33 pm.
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 98
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: file input don't know where to start

  #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  
Join Date: Jul 2006
Posts: 155
Reputation: tefflox is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: file input don't know where to start

  #5  
Jul 11th, 2006
oh, now i see that. thx
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC