stefancosmin Posted April 12, 2013 Report Posted April 12, 2013 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 buffervOut = (int*) malloc(sizeof(int)*dim);vEnd = vOut + dim;//citire date in bufferfread(vOut, sizeof(int), dim, file);//afisare datewhile(vOut < vEnd) printf("%c ", (char*)vOut++); Quote
em Posted April 12, 2013 Report Posted April 12, 2013 Salutare. Posteaza tot codul. (pastebin sau foloseste ) Quote
stefancosmin Posted April 12, 2013 Author Report Posted April 12, 2013 Am schimbat codul intre timp, in fisier se stocheaza caractere, ideea ramane aceeasi si problema la fel.void main(){//dataFILE *file;char *vIn, *vOut, *vEnd;int i = 0, dim;//read array dim from consoleprintf("Introduceti dimensiunea:");scanf("%d",&dim);//memory alocation for integer arrayvIn = (char*)malloc(dim*sizeof(char));vEnd = vIn + dim;//read data from consolewhile(vIn < vEnd){ printf("v[%d]=",i++); fflush(stdin); scanf("%c", vIn++);}//open binary file for write dataif(( file = fopen("fisier.bin","wb")) == NULL){ printf("File open error!"); exit(1);}//write data in binary filefwrite(vIn, sizeof(char), dim, file);//open file for read dataif ((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 filefclose(file);} Quote
noVaLue Posted April 13, 2013 Report Posted April 13, 2013 (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 April 13, 2013 by noVaLue Quote
stefancosmin Posted April 14, 2013 Author Report Posted April 14, 2013 Multam fain! Functioneaza cum trebuie acum. Quote