Need Help on...

closed account (Doj23TCk)
I need help on
1. Saving a list of parts to a text file.
2. Load the parts from a text file.

This is part of the code i have now but just unsure how to save it to a text file and then load that text file.

int readPartsFromFile( char *fileName, int partId[MAX_PARTS], int partQ[MAX_PARTS])
{
int count = 0;
int id, q;
FILE *filePtr;

filePtr = fopen(fileName, "r");
if( filePtr == NULL)
{
printf("Unable to find the data file\n");
return 0;
}

do{
fscanf(filePtr, "%d%d", &id, &q);
if(id > -1){
partId[count] = id;
partQ[count] = q;
++count;
}
}while(id > -1 && count < MAX_PARTS);

fclose(filePtr);
return count;
}

void writePartsToFile( char *fileName, int partId[MAX_PARTS], int partQ[MAX_PARTS], int n)
{
int i;
FILE *filePtr;
filePtr = fopen(fileName, "w");
if( filePtr == NULL)
{
printf("Unable to open the data file to write\n");
return; //bail out
}

fclose(filePtr);
}

void printParts(const int id[MAX_PARTS],const int quantity[MAX_PARTS], int n)
{
int i;
printf("ID\tQuantity\n");
printf("----\t----\n");
for(i = 0; i < n; ++i){
printf("%d\t%d\n", id[i], quantity[i]);
}
}

int findPartID(int partNum, const int id[], int n)
{
int loc = -1;
int i;
for(i = 0; i < n; ++i){
if(partNum == id[i]){
loc = i;
break;
}
}

return loc;
}
Last edited on
Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

You've been asked this before, in previous threads. If you can't be bothered to make your posts readable, why should we bother to try and read them?
Topic archived. No new replies allowed.