16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

C Libraries<br />

"r+"<br />

"w+"<br />

"a+"<br />

"b"<br />

open an existing text file for reading & writing at beginning<br />

open a new text file for reading & writing at beginning<br />

open an existing text file in append mode & allow reading<br />

binary file<br />

int fclose(FILE *stream)<br />

int fflush(FILE *stream)<br />

int fgetc(FILE *stream)<br />

int fputc(int c, FILE *stream)<br />

char *fgets(char *s, int n, FILE *stream)<br />

int fputs(char *s, FILE *stream)<br />

int fprintf(FILE *stream, char *format)<br />

int fscanf(FILE *stream, char *format)<br />

/* random access of file */<br />

long ftell(FILE *stream)<br />

int fseek(FILE *stream, long offset, int wherefrom)<br />

wherefrom = 0 beginning (SEEK_SET)<br />

1 current (SEEK_CUR)<br />

2 end (SEEK_END)<br />

int getc(FILE *stream)<br />

int putc(char c, FILE *stream)<br />

int ungetc(int c, FILE *stream)<br />

int fread(void *buffer, unsigned element size, unsigned count, FILE *stream)<br />

/* read a block of binary data from a stream */<br />

int fwrite(void *buffer, unsigned element size, unsigned count, FILE *stream)<br />

void rewind(FILE *stream)<br />

/* open test.c */<br />

#include <br />

FILE *open_test(void)<br />

FILE *fp;<br />

fp = fopen("test","r");<br />

if (fp == NULL)<br />

fprintf(stderr, "Error opening file test\n");<br />

return fp;<br />

/* if ((fp=fopen("test", "r")) == NULL)<br />

fprintf(stderr, "Error opening file test\n"); */<br />

/* test_copy.c */<br />

#include <br />

#define FAIL 0<br />

#define SUCCESS 1<br />

int copyfile(char *infile, char *outfile)<br />

{<br />

FILE *fp1, *fp2;<br />

if ((fp1=fopen(infile, "r")) == NULL)<br />

return FAIL;<br />

if ((fp2=fopen(outfile,"w")) == NULL)<br />

{ fclose(fp1); return FAIL;<br />

}<br />

65

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!