/*
Programmazione II - Esempio di codice
Example code: file write demonstration. (20000808 prelz@mi.infn.it)
*/
#include
#include
/* This include file contains structure definitions and #defines. */
#include "ex1.h"
int
main(int argc, char *argv[])
{
char in[800]; /* Generic input storage */
FILE *out_file;
int record_count = 0;
measurement new_record;
printf("Please enter the name of the output file ---> ");
fgets(in, sizeof(in), stdin);
in[strlen(in)-1] = '\000'; /* Remove trailing newline */
out_file = fopen(in,"w");
if (out_file == NULL)
{
fprintf(stderr, "Cannot open file %s. ", in);
perror(""); /* Prints the current error status */
exit(1);
}
/* The output file begins with the number of records (currently zero). */
if (fwrite(&record_count, sizeof(int), 1, out_file) < 1)
{
fprintf(stderr, "Error writing to output file. ");
perror("");
fclose(out_file);
exit(1);
}
for (;;)
{
/* Infinite data entry loop. Will exit with break. */
printf ("********* Now processing record # %d.\n",record_count+1);
printf ("Would you like to add a new record ? (Y/N): ");
fgets(in, sizeof(in), stdin);
if (in[0] != 'y' && in[0] != 'Y') break;
printf ("Please enter your name --->");
fgets(in, sizeof(in), stdin);
in[strlen(in)-1] = '\000'; /* Remove trailing newline */
SAFE_ASSIGN(new_record.observer_name,in);
printf ("Measurement date (DD-MM-YYYY) --->");
fgets(in, sizeof(in), stdin);
sscanf(in,"%d-%d-%d",&new_record.dt.day,
&new_record.dt.month,
&new_record.dt.year);
printf ("Measurement time (hh:mm) --->");
fgets(in, sizeof(in), stdin);
sscanf(in,"%d:%d",&new_record.dt.hours,
&new_record.dt.minutes);
printf ("Temperature (C) --->");
fgets(in, sizeof(in), stdin);
new_record.temperature = atof(in);
printf ("Measurement value --->");
fgets(in, sizeof(in), stdin);
new_record.value = atof(in);
printf ("Any comments ? --->");
fgets(in, sizeof(in), stdin);
in[strlen(in)-1] = '\000'; /* Remove trailing newline */
SAFE_ASSIGN(new_record.comments,in);
printf ("\n\nI have the following data:\n");
printf ("Observer name: %s\n", new_record.observer_name);
printf ("Date and time : %02d-%02d-%04d %02d:%02d\n",
new_record.dt.day,
new_record.dt.month,
new_record.dt.year,
new_record.dt.hours,
new_record.dt.minutes);
printf ("Temperature: %5.5g C\n", new_record.temperature);
printf ("Meas. value: %g\n", new_record.value);
printf ("Comments: %s\n", new_record.comments);
printf ("\nIs it OK to write them on disk ? (Y/N): ");
fgets(in, sizeof(in), stdin);
if (in[0] != 'y' && in[0] != 'Y') continue;
printf("\n");
record_count++;
/* First of all, write the record (we are now positioned at the
end of file) */
if (fwrite(&new_record, sizeof(measurement), 1, out_file) < 1)
{
fprintf(stderr, "Error writing to output file.");
perror("");
record_count--;
continue;
}
else
{
/* Successful write. */
/* Now go to the beginning of the file and update the record count */
fseek(out_file, 0, SEEK_SET);
if (fwrite(&record_count, sizeof(int), 1, out_file) < 1)
{
fprintf(stderr, "Error writing to output file.");
perror("");
record_count--;
/* Try to move to the last known good position */
fseek(out_file, record_count*sizeof(measurement) + sizeof(int),
SEEK_SET);
continue;
}
/* Now move to the end of file, after the last record. */
fseek(out_file, record_count*sizeof(measurement), SEEK_CUR);
}
}
fclose(out_file);
exit(0);
}