-
updating a file
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);
}
-
Because this error isn't in your code, I find it highly unlikely that this isn't just a problem with your compiler, but I know that there are different file settings you can have where it overwrites, continues the file, clears and then writes, etc.. But you didn't specify a mode (at least no where that I could find) so it really should've defaulted to clear and write. I know this doesn't solve the problem but it's all I could think of and is at least food for thought. Good luck.
-
Well apart from the lack of tags, the main problem seems to be that size==0 when the malloc is called.
-
Quote:
Originally posted by Salem
Well apart from the lack of tags, the main problem seems to be that size==0 when the malloc is called.
And ...
>goto <- yuck!
>fflush(stdin); <- yuck
>if (fread(&new_s, sizeof(struct stock), 1, fp) != s)
Incorrect comparison! If the read is successful, the return code is 1.
>} while (!feof(fp));
Don't control the loop like this, use the rc from fread.
>char(*code)[14] = malloc(size * sizeof(char[14]));
>strcpy(code[size], new_s.itemCode);
What are you upto here?
>fseek(fp, (long) -s, 1);
try and use the #defines that came with compiler, it makes reading easier (SEEK_CUR)
>if ((fp = fopen("stock.txt", "a+")) == NULL)
>{
> err_msg("*** Read error -- ensure file exists ***");
Mode a+ will create the file if it doesn't exist. Also, the internal stream is set to the end of the file, so subsequent reads won't work.
-
>>I am trying to update a record in a text file.
No guarantee the record to be replaced is the same size on the file as the old one. Use binary or rewrite the whole file. If you are sorting it then why not rewrite the whole file?
Or start from begining of file, read a record, compare the the ones flaged for change, write as needed.
>>What happens is the program reads the file into an array it then sorts the array.
Are you searching the unsorted array (or file) for the correct record number to replace or the sorted array? (where the records may have changed order)