Jump to content
stefancosmin

Fisier binar in C

Recommended Posts

Posted

Salut!

Am un fisier binar (sa-i zicem fisier.bin) in care am stocat 'dim' valori intregi si doresc sa-i retin valorile intr-un buffer. In momentul in care vreau sa printez valorile nu obtin ceea ce am introdus in fisier. Mai jos e secventa de cod folosita:

if ((file = fopen("fisier.bin", "rb")) == NULL)

{

printf("File open error!");

exit(1);

}

//alocare memorie pentru buffer

vOut = (int*) malloc(sizeof(int)*dim);

vEnd = vOut + dim;

//citire date in buffer

fread(vOut, sizeof(int), dim, file);

//afisare date

while(vOut < vEnd)

printf("%c ", (char*)vOut++);

Posted

Am schimbat codul intre timp, in fisier se stocheaza caractere, ideea ramane aceeasi si problema la fel.

void main(){

//data

FILE *file;

char *vIn, *vOut, *vEnd;

int i = 0, dim;

//read array dim from console

printf("Introduceti dimensiunea:");

scanf("%d",&dim);

//memory alocation for integer array

vIn = (char*)malloc(dim*sizeof(char));

vEnd = vIn + dim;

//read data from console

while(vIn < vEnd)

{

printf("v[%d]=",i++);

fflush(stdin);

scanf("%c", vIn++);

}

//open binary file for write data

if(( file = fopen("fisier.bin","wb")) == NULL)

{

printf("File open error!");

exit(1);

}

//write data in binary file

fwrite(vIn, sizeof(char), dim, file);

//open file for read data

if ((file = fopen("fisier.bin", "rb")) == NULL)

{

printf("File open error2!");

exit(1);

}

vOut = (char*) malloc(sizeof(char)*dim);

vEnd = vOut + dim;

fread(vOut, sizeof(char), dim, file);

while(vOut < vEnd)

printf("%c ", (char*)vOut++);

//close the file

fclose(file);

}

Posted (edited)

FILE *file;
char *vIn, *vOut, *vEnd;
int i = 0, dim;

//read array dim from console
printf("Introduceti dimensiunea:");
scanf("%d",&dim);

//memory alocation for integer array
vIn = (char*)malloc(dim*sizeof(char));
vEnd = vIn + dim;

//read data from console
while(vIn < vEnd)
{
printf("v[%d]=",i++);
fflush(stdin);
scanf("%c", vIn++);
}
//noVaLue// Operatiile matematice pt pointeri = deplasare a locatiei in memorie,
// inspre adresa elementului urmator(sau cel ce reiese din operatie)
// cu o magnitudine de vIn = (Pointer Memory Start) SEMN Amount
// SEMN => +/-/\/*/^/%
// unde Amount = (vIn sizeof(type))*dim, si semn - (in cazul de fata)
// vIn-=dim;
vIn=vIn-dim;

//open binary file for write data
if(( file = fopen("fisier.bin","wb")) == NULL)
{
printf("File open error!");
exit(1);
}

// write data in binary file
fwrite(vIn, sizeof(char), dim, file);

/* //noVaLue// Trebuie inchis fisierul inainte de a incerca sa-l redeschidem sub alt MOD. */
fclose(file);

//open file for read data
if ((file = fopen("fisier.bin", "rb")) == NULL)
{
printf("File open error2!");
exit(1);
}

vOut = (char*) malloc(sizeof(char)*dim);
vEnd = vOut + dim;

fread(vOut, sizeof(char), dim, file);

// noVaLue // Atentie la faptul ca variabila ta este de tip char, ++ ne va muta in memorie la pozitia unui element
// iar ceea ce ramane de facut este sa printam valoarea la care acea celula puncteaza
// de aceea si printf("%c ", *vOut++); functioneaza
while(vOut < vEnd)
printf("%c ", (char)*vOut++);

//close the file
fclose(file);

Edited by noVaLue

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...