08 C Io Handout
08 C Io Handout
08 C Io Handout
#include <stdio.h>
FILE *fopen(const char *path, const char *mode);
int fclose(FILE *stream);
int fprintf(FILE *stream, const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
The mode in fopen() can have the following possible values.
r Open text file for reading. The stream is positioned at the beginning
of the file.
r+ Open for reading and writing. The stream is positioned at the
beginning of the file.
w Truncate file to zero length or create text file for writing. The stream
is positioned at the beginning of the file.
w+ Open for reading and writing. The file is created if it does not exist,
otherwise it is truncated. The stream is positioned at the beginning of
the file.
a Open for appending (writing at end of file). The file is created if it
does not exist. The stream is positioned at the end of the file.
a+ Open for reading and appending (writing at end of file). The file is
created if it does not exist. The initial file position for reading is at
the beginning of the file, but output is always appended to the end of
the file.
Binary files are unformatted with data stored directly in its native format.
They take less space and are faster to read/write. However, we have to
know their layout to be able to read them. Use the fread() and
fwrite() functions in the standard library for binary I/O.
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite (const void *ptr, size_t size, size_t nmemb,
FILE *stream);
file-io/generate_text.c
file-io/generate_binary.c
0m1.410s
0m0.763s
0m0.446s
[amit@onyx
-rw-r--r--rw-r--r-[amit@onyx
The file size was about 30% bigger for text files and the time was about 18
times slower!
Databases often use random access files for storing data that
is sorted or hashed for fast access.
I
I
I
The fseek function sets the file position indicator for the stream
pointed to by stream. The new position, measured in bytes, is
obtained by adding offset bytes to the position specified by whence. If
whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is
relative to the start of the file, the current position indicator, or
end-of-file, respectively. A successful call to the fseek function clears
the end-of-file indicator for the stream.
The ftell function obtains the current value of the file position
indicator for the stream pointed to by stream.
rewind function sets the cursor back to the start of the file.
The fgetpos and fsetpos functions are alternate interfaces
equivalent to ftell and fseek (with whence set to SEEK_SET), setting
and storing the current value of the file offset into or from the object
referenced by pos
size);
I
I
I
I
The files Record.h and Record.c define the data record that is
stored in the file.
The files ExternalSearch.h and ExternalSearch.c define a linear
search and a binary search over the records stored in the data file.
The file SearchTest.c performs specified number of random linear or
binary searchers to performance comparison.
The file gendata.c generates a sorted data file that can be used as
input to the test program. The file timing.c contains functions that
allow precise timing of a section of code inside a program.
Example:
Record.h
/* C-examples/file-io/disk_search/Record.h */
#ifndef __RECORD_H
#define __RECORD_H
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
typedef struct record Record;
typedef struct record * RecordPtr;
struct record {
long int key;
double value1;
double value2;
double value3;
};
char *toString(RecordPtr record);
#endif /* __RECORD_H */
Example:
Record.c
/* C-examples/file-io/disk_search/Record.c */
#include "Record.h"
#define MAXLEN 128
char *toString(RecordPtr record)
{
char *buffer;
buffer = (char *) malloc(sizeof(char)*MAXLEN);
sprintf(buffer, "key=%ld values=[%lf, %lf, %lf]\n",
record->key, record->value1, record->value2, record->value3);
return buffer;
}
Example:
ExternalSearch.h
/* C-examples/file-io/disk_search/ExternalSearch.h */
#ifndef __EXTERNALSEARCH_H
#define __EXTERNALSEARCH_H
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
#include "Record.h"
RecordPtr extBinarySearch(FILE *dataFile, unsigned long int key);
RecordPtr extLinearSearch(FILE *dataFile, unsigned long int key);
#endif /*
__EXTERNALSEARCH_H */
Checkpointing to Disk
I
I
I
Serialization in Java
I
Serializing an object:
FileOutputStream fout = new FileOutputStream("obj.serial");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(obj);
De-serializing an object:
FileInputStream fileIn = new FileInputStream("obj.serial");
ObjectInputStream in = new ObjectInputStream(fileIn);
Record obj = (Record) in.readObject();
I
I