Page 51
// open to append ("a"), which also tests if the USB stick is plugged in
if
((f = fopen(chkf,
"a"
)) == NULL) {
printf(
"No USB stick detected\n"
);
return
-
1
;
// exit the program
}
// file is now open for append; if it didn't exist it has been created
printf(
"Sending %s, %d\n"
,
"Field "
, data);
fprintf(f,
"Field "
);
// use fprintf to send a text string to chkf
fprintf(f,
"%d"
,data);
// now send formatted numeric data using fprintf
fclose(f);
// close the file to make sure the output is sent
// now read it back
f = fopen(chkf,
"r"
);
// it exists since we just created it
fscanf(f,
"%s %d"
,s, &x);
// read the two data items from the file
fclose(f);
// done with file, so close it
printf(
"Data read is %s: %d\n"
, s, x);
}
The USB drive can now be removed from the KIPR Link. A file named
myUSBfile
will now be present
on the USB drive and can be accessed using a text editor to verify file contents .