0
Q:

file binari c

/*
  Mette a 0 il massimo elemento di un vettore.
*/

#include<stdlib.h>
#include<stdio.h>

int main() {
  FILE *fd;
  int res;

  int x;
  int max;		/* massimo trovato finora */
  int posmax;		/* posizione del massimo nel file */


			/* apre il file */
  fd=fopen("test.dat", "r+");
  if( fd==NULL ) {
    perror("Errore in apertura del file");
    exit(1);
  }


			/* ciclo di lettura */
  max=0;
  while(1) {
    res=fread(&x, sizeof(int), 1, fd);
    if( res!=1 ) 
      break;

    if( x>max ) {
      max=x;
      posmax=ftell(fd)-sizeof(int);
    }
  }


			/* torna nella posizione del massimo
			   e ci scrive sopra 0 */
  fseek(fd, posmax, SEEK_SET);
  x=0;
  fwrite(&x, sizeof(int), 1, fd);


			/* chiude il file */
  fclose(fd);

  return 0;
}

0

New to Communities?

Join the community