Single entry edit in Random Access file

Getting crazy over it while i'm pretty sure it's quite easy to do T_T

As the title claim i have a random access file and would like to edit only 2 entry of a selected record ( "giacenza" and "venduti" )

I managed to edit all the entry of a record but when i try to edit only 1 or 2 of them the rest get deleted

Hope you guys can help me

Te record it's structured this way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
FILE *fp;
struct magazzino magazzinoStato;
int  offst;
if ((fp = fopen("magazzino.dat", "r+")) == NULL)
    printf("Non possibile aprire il file !!!.\n");
else
    {
        printf("Immetti i codice identificativo del prodotto (da 1 a 300, 0 per terminare)\n");
        scanf("%d", &magazzinoStato.codice);
        while (magazzinoStato.codice != 0)
            {
                printf("Nome Prodotto:\n ");
                scanf("\n%s", &magazzinoStato.nome);
                printf("Nome Produttore:\n ");
                scanf("\n%s", &magazzinoStato.produttore);
                printf("Costo unitario del prodotto in Euro:\n ");
                scanf("\n%f", & magazzinoStato.costo);
                printf("Valore di vendita unitario del prodotto in Euro:\n");
                scanf("\n%f", & magazzinoStato.vendita);
                printf("Giacenza Magazzino:\n ");
                scanf("\n%d", & magazzinoStato.giacenza);
                printf("Numero prodotti venduti:\n ");
                scanf("\n%d", & magazzinoStato.venduti);
                offst = (magazzinoStato.codice -1 ) * (sizeof(struct magazzino));
                fseek(fp, offst, SEEK_SET);
                fwrite(&magazzinoStato, sizeof(struct magazzino), 1, fp);
                printf("Immetti i codice identificativo del prodotto (da 1 a 300, 0 per terminare)\n");
                scanf("%d", &magazzinoStato.codice);
}}
fclose (fp);}
Last edited on
would like to edit only 2 entry of a selected record

1) do you need to do it in C? As the title says, this is a C++ forum.

2) I don’t know C, but perhaps I’ve spotted an error in lines 25-26 of your snippet:
1
2
offst = (magazzinoStato.codice -1 ) * (sizeof(struct magazzino));
fseek(fp, offst, SEEK_SET);

Reading here:
http://en.cppreference.com/w/c/io/fseek
int fseek( FILE *stream, long offset, int origin );

2nd argument should be a long, not an int (in your code: int offst;).

What’s more:
If the stream is open in text mode, the only supported values for offset are zero (which works with any origin) and a value returned by an earlier call to ftell on a stream associated with the same file (which only works with origin of SEEK_SET).

School homework. I'm just following what the exercise ask.

1) I'm currently using C++, that's why i ended up here :P, but the exercise specifically ask to create, initialize and edit the file inside the program with that procedure. that's like 1/10 of the actual program i created and sadly it asks but don't give any info on how to edit a single entry.

2) Since i'm using C++ the fseek should be able to manage an "int"

http://www.cplusplus.com/reference/cstdio/fseek/

At least on my note and teacher slide an int is used.
Last edited on
Oh well man, thank you very much. Since you said it was actually C and not C++ i searched on google and managed to find a workaround.

Seems you where right, i was impropery using the fflush and fseek

i went with:

fflush(stdin);
fseek(fp, -sizeof(struct magazzino), SEEK_CUR);

and it now work smootly.
Edit: when I posted this, last Deneviel's post hadn't appeared yet.

- - -

In the page you linked I can read:
int fseek ( FILE * stream, long int offset, int origin );
...
For streams open in text mode, offset shall either be zero or a value returned by a previous call to ftell, and origin shall necessarily be SEEK_SET.

Anyway, if you fancy better help, you could evaluate posting a compilable excerpt of your code along with a minimum example of your data file to test it on (and possibly some more details about its expected behaviour).
Last edited on
After the polish and some fix ( it still has some bugs when creating a random access file ) i will post the whole code as for the workaround i can post it now since seems to work pretty well but lack comments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    FILE *fp;
    struct magazzino magazzinoStato;
    int codice;
    if ((fp = fopen("magazzino.dat", "r+")) == NULL)
        printf("Non possibile aprire il file !!!.\n");
    else
        {
        printf("Immetti i codice identificativo del prodotto (da 1 a 300 )\n");
        scanf("%d", &codice);
            while (!feof(fp))
        {
            fread(&magazzinoStato, sizeof(struct magazzino), 1, fp);
            if (magazzinoStato.codice == codice)
        {
                printf("%-15d%-15s%-15s%-15.2f%-15.2f%-15d%-15d\n",magazzinoStato.codice,magazzinoStato.nome,magazzinoStato.produttore,magazzinoStato.costo, magazzinoStato.vendita, magazzinoStato.giacenza, magazzinoStato.venduti);
                printf("\nInserire nuova giacenza: ");
                fflush(stdin);
                scanf("%d", &magazzinoStato.giacenza);
                printf("\nInserire nuovo valore dei prodotti venduti: ");
                fflush(stdin);
                scanf("%d", &magazzinoStato.venduti);
                fseek(fp, -sizeof(struct magazzino), SEEK_CUR);
                fwrite(&magazzinoStato, sizeof(struct magazzino), 1, fp);
                fclose(fp);
                printf("\nModifica Effettuata");
                break;
        }}}

Last edited on
Topic archived. No new replies allowed.