I am trying to update a record in a text file. What happens is the program reads the file into an array it then sorts the array. The user is then asked to enter a item code for the item that they want to update. The program then searches the sorted array if the item code is found then enter new details. The program then writes the new data to disk but it is written at the EOF. here is the code i am using. is it poosible to update a record using this code. I think the fseek is the problem line but i cant work out how to get to the fseek to the current record.
{
struct stock new_s;
int s, size = 0, i;
char search[14];
char (*code)[14] = malloc(size * sizeof(char[14]));
FILE *fp;
s = sizeof(struct stock);
if((fp = fopen("stock.txt", "a+")) == NULL)
{
err_msg("*** Read error -- ensure file exists ***");
}
do
{
if(fread(&new_s, sizeof(struct stock), 1, fp) != s)
{
if(feof(fp))
{
break;
}
strcpy(code[size], new_s.itemCode);
size++;
}
}while(!feof(fp));
sortArrayS(code, size);
printf("\n%d", size);
fflush(stdin);
printf("\n\n\n\n\n");
US: getItemCode(new_s.itemCode);
strcpy(search, new_s.itemCode);
if(searchArrayS(code, size, search) >= 0)
{
get_new_code(&new_s);
fseek(fp, (long)-s, 1);
fwrite(&new_s,s,1,fp);
}
else
{
printf("\nThe item you were looking for is not on the file please try again");
goto US;
}
fclose(fp);
}