LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

write/read struct in binary file

Solved!
Go to solution

Hi to all,

 

I created a struct profile defined as follows:

typedef struct profile {

char name [30];
float power;
double frequency;
struct profile * next;

}

I can write the structures in a file, but I should do so that when you try to insert a struct, you control the field name and only if there is no other struct with that name,it is inserted in the file .
Please help me !!!

 

 

0 Kudos
Message 1 of 3
(3,196 Views)

I tried this way

 

#include <ansi_c.h>
#include <cvirte.h>


char nuovo[50];
int cmp;
FILE *fp;

struct profilo
{
char nome[50];
float potenza;
double frequenza;
}p;

int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */


fp = fopen ("c:\\Program Files (x86)\\National Instruments\\CVI81\\ESERCITAZIONI\\scrittura file 2\\profili.txt", "ab+");
 printf("Insert name: \n");
scanf("%s",nuovo);
do
{
fseek(fp,sizeof(struct profilo),SEEK_SET);
fread(&p,sizeof(struct profilo),1,fp);
//printf("nome: %s\n",nuovo);
//printf("p.nome: %s\n",p.nome);
p.nome==nuovo;
printf("p.nome dopo: %s\n",p.nome);
cmp=strcmp(p.nome,nuovo);
if(cmp=0)
{
  printf("Already exists\n");
fclose(fp);
}
else
{
p.nome==nuovo;
scanf("%f%lf",&p.potenza,&p.frequenza);
}
}while(!feof(fp));

fclose(fp);
return 0;
}

but don't work . 

0 Kudos
Message 2 of 3
(3,184 Views)
Solution
Accepted by topic author ViperNaples

I solved !!!  Smiley Very Happy

0 Kudos
Message 3 of 3
(3,166 Views)