C - Modify a struct from a file

0

I just started with C and I have no idea the truth. The thing is to make structs and put them in a file and then read and modify them. I have no idea how to modify a struct of the particular file. I hope you can help me, I'm a little bad at this.

#include <stdio.h>
#include <string.h>
#include <time.h>
#pragma warning(disable:4996)


int const MAX=100;

typedef struct {

char name[256];
char email[256];
int number;
char adress[256];
int urgent;
time_t time;
} order;


void add();
void read();

int main() {

int a=0;
while (a != 4) {
    printf("type the number of the operation you would like to make\n");
    printf("1-Add new order\n");
    printf("2-List all orders\n");
    printf("3-Modify order\n");
    scanf_s("%d", &a);

    switch (a) {
    case(1): {add(); break; }
    case(2): {read(); break; }
    case(3): {}
    default: break;
    }
}

return 0;

}

void read() {

order pedido;
FILE * fp;
fp = fopen("text.txt", "rb");

    while(fread(&pedido, sizeof(pedido), 1, fp)==1)
    {

    printf("\nName: %s", pedido.name);
    printf("\nEmail: %s", pedido.email);
    printf("\nNumber: %d", pedido.number);
    printf("\nAdress: %s", pedido.adress);
    if(pedido.urgent==1) printf("\nUrgent:yes");
    if (pedido.urgent==0) printf("\nUrgent:no");
    printf("\nTimeSys: %ld", pedido.time);
        printf("\n********************\n");
}
printf("\nFinished\n");

fclose(fp);
return;
}

void add()

{

order ord;
char temp[256];
printf("insert Order data\n");
printf("Name\n");
scanf("%s", &ord.name);
printf("Email\n");
scanf("%s", &ord.email);
printf("Phone number\n");
scanf("%d", &ord.number);
printf("Adress\n");
scanf("%s", &ord.adress);
printf("Urgent(y/n)\n");
scanf("%s", &temp);
if (temp[0]== 'y')
    ord.urgent= 1;
else if (temp[0] == 'n')
    ord.urgent = 0;
else {
    printf("error"); return;
}

time_t tiempo = time(NULL);
ord.time=tiempo;

FILE *sourcefile= fopen("text.txt", "a");


fwrite(&ord, sizeof(ord), 1, sourcefile);

fclose(sourcefile);

return;

}
    
asked by eisklat 02.04.2018 в 03:00
source

1 answer

2
  

I just started with C and I have no idea the truth.

So I suggest you do not use things you do not understand:

#pragma warning(disable:4996)

Warnings warn you that you are doing things that, while they may work, are not recommended and may start to give trouble at any time. If you silence warnings you run the risk that your brand-new program fails to present it ... you are warned.

On the other hand, I suggest you start to introduce yourself in C for simpler things ... if you have missed class or you have not paid attention for whatever reason it is your turn to put your batteries and make up for lost time ... complain that you do not have a level is not an option and it is better that you listen to it now than you realize it at the time of the exams.

  

No I have no idea how to modify a struct of the particular file

A struct only exists in your program. A file is just a sequence of bytes ... there are no structures or strings of characters or numbers ... they are only bytes and it is your program that knows how to make sense of that information.

What you are being asked is that you know how to locate the record to be modified, read it, apply the changes requested, and save it again in the file.

To read it you already know ... you are doing it with the method read and to save it too, method write ... you have almost everything done. You need to enter a search. You will have to ask some key information to the user that allows you to locate the record to be modified ... for example the name:

char nombre[255];
print("Indica el nombre a buscar: ");
scanf("%s,nombre);

And after this you are reading all the records until you find one whose name matches the order. Note that the flags with which we open the file are different from those indicated in read . This is because we need write privileges to modify the file.

FILE * fp;
fp = fopen("text.txt", "rb+");

while(fread(&pedido, sizeof(pedido), 1, fp)==1)
{
  if( strcmp(pedido.name,name) == 0 )
    break;
}

if( feof(fp) )
{
  puts("Nombre no encontrado\n");
  fclose(fp);
  return; 
}

// Ya tenemos localizado el registro

Now you allow the user to modify some parameter ...

printf("Correo actual: %s\n",pedido.email);
puts("Introduce nuevo correo: ");
scanf("%s",pedido.email);

And you save the record in the file. For this, the first thing to do is to move the file cursor back to the beginning of the record. If we do not do it, we will crush a record that we do not want to touch:

fseek(fp,-sizeof(order),SEEK_CUR);
fwrite(&pedido, sizeof(order), 1, fp);

And we're done ... we just need to close the file.

fclose(fp);
    
answered by 02.04.2018 в 09:12