View Single Post
Old 28-11-2008, 15:35   #2
salvodel
Senior Member
 
L'Avatar di salvodel
 
Iscritto dal: Dec 2004
Messaggi: 783
Nessuno mi da un aiutino ? Ora succede una cosa strana. Vi posto il mio programma:
Codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <float.h>

int readFILE(double *p_ID, int dimens)
{
	FILE *pFile;
	int punti;

	pFile = fopen ( "pesi.dat" , "rb" );
	if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

	// copy the file into the buffer:
	punti = fread(p_ID,sizeof(double),dimens,pFile);
	if (punti != dimens) {fputs ("Reading error",stderr); exit (3);}
	return punti;

}

int writeOUTPUT(double *buffer, int punti)
{
	FILE *pFile;
	pFile = fopen("myfile.dat","wb" );
	fwrite (buffer ,sizeof(double), punti, pFile);
	fclose (pFile);
	return 0;
}

int main()
{
	double *p_ID;
	int punti,i;
	int dimens;

	dimens=20;
	p_ID =(double *)calloc(dimens,sizeof(double));
	if (p_ID == NULL) {fputs ("Memory error",stderr); exit (2);}
	punti=readFILE(p_ID, dimens);

	for(i=0;i<punti;i++)
		printf("p_ID[%d]=%g\n",i,p_ID[i]);

	writeOUTPUT(p_ID, punti);
	system("PAUSE");
	return 1;
}
Il mio programma legge il file ma quando faccio la printf di p_ID vedo dei numeri casuali ma quando apro il file che ho salvato vedo i numeri giusti MA solo e sempre la metà di dimens. Mi spiego: se gli dico di legere 20 numeri lui mi legge 20 numeri, mi plotta 20 numeri sbagliati ma salva 10 numeri corretti. Perché fa cosi ? Un aiutino ! Non riesco a capire !
Grazie
__________________
"May the wind always be at your back and the sun upon your face.
And may the wings of destiny carry you aloft to dance with the stars...."
salvodel è offline   Rispondi citando il messaggio o parte di esso