int atoi(const char *nptr);
double atof(const char *nptr);
long atol(const char *nptr);
long int strtol(const char *nptr,char **endptr,int base);
unsigned long int strtoul(const char *nptr,char **endptr,int base);
double strtod(const char *nptr,char **endptr);
char *strchr(const char *s,char c); /* find first position of c in s */
int sscanf( const char *, const char *, ); /* read data from string matching format */
int scanf( const char *, );
int fprintf( FILE *stream, const char *format, );
int sprintf( char *buffer, const char *format [, argument] );
FILE I/O FUNCTIONS
/************************************************************************/
/* FILE* FUNCTIONS
/************************************************************************/
FILE* fopen(const char* filepath, const char* mode); //mode: "r": read; "w": write, create if not exist; "a": append, create if not exist;
int fread (void* buf, size_t size, size_t nitems, FILE* stream); // read nitems chars or to the EOF. so it could read multi-lines.
int fwrite(void* buf, size_t size, size_t nitems, FILE* stream); // write nitems chars or to the '\0' of buf.
char* fgets(char *buf, int nitems, FILE* stream); // read at most one line with '\n'(holds it) or terminated by EOF.
int fputs(char *buf, FILE* stream); // write strlen(buf) chars into stream and return real written char number.
int getc(FILE* stream); // read next char and return EOF on end of file or error.
int putc(int c, FILE* stream); // return c on success, else EOF(-1).
int fgetc(FILE* stream); // read next char and return EOF on end of file or error.
int fputc(int c, FILE* stream); // return c on success, else EOF(-1).
int fflush(FILE *stream); // return 0 on success, else EOF(-1).
int feof(FILE *stream); // return 1 if reach EOF, else 0.
int fclose(FILE *stream); // return 0 on success, else EOF(-1).
FILE* freopen(char* filename, char* type, FILE* stream); // redirect stream to filename file
int fgetpos(FILE *stream,*fpos_t filepos); // get current file pointer position.
int fscanf(FILE *stream, char *format,[argument]);
int fseek(FILE *stream, long offset, int fromwhere); // SEEK_SET, SEEK_CUR, SEEK_END.
void setbuf(FILE *steam, char *buf);
int setvbuf(FILE *stream, char *buf, int type, unsigned size); // call it just after opening file.
long ftell(FILE *stream); // get current file pointer position.
/************************************************************************/
/* INT FD FUNCTIONS
/************************************************************************/
int close(int fd);
int fcntl(int fd , int cmd); // #include <fcntl.h>
int fcntl(int fd,int cmd,long arg);
int fcntl(int fd,int cmd,struct flock * lock);
int ioctl(int fd, int cmd,[int *argdx, int argcx]); // #include <sys/ioctl.h>
int read(int fd, void *buf, int nbyte);
int write(int fd, void *buf, int nbyte);
long lseek(int fd, long offset, int fromwhere); // SEEK_SET, SEEK_CUR, SEEK_END.
/************************************************************************/
/* STDIO FUNCTIONS
/************************************************************************/
char* gets( char *buffer ); // from stdio, over at '\n' and EOF and set '\n' to '\0'.
int fgetchar(void); // from stdio and return EOF when error occurs.
int getch(void); // #include <conio.h>, no echoing.
int getchar(void); // #define getchar() fgetc(stdin)
int getche(void); // echo char from stdio and continue at '\n'.
int putchar(int ch); // put char into stdout
int puts(char *string); // put string into stdout.
int scanf(char *format[,argument,]);
/************************************************************************/
/* TEST ON WINDOWS
/************************************************************************/
#include<stdio.h>
#include <assert.h>
/************************************************************************/
/* read line_length chars into line from sourcefile
/* return number of chars really read, else -1 for error.
/************************************************************************/
int read_line(char* line, int line_length, FILE* sourcefile)
{
assert(sizeof(char) == 1);
int n = 0;
while (1)
{
char c = fgetc(sourcefile);
if(ferror(sourcefile)) return -1;
if(c == EOF || line_length-- == 0) break;
*line++ = c;
n++;
}
return n;
}
FILE* open_file_demo(const char* filepath, const char* options)
{
FILE* fp = fopen(filepath, options);
if(fp == NULL)
{
perror("fopen failure!");
}
return fp;
}
void write_file_demo()
{
char* filepath = "D:/xxx.txt";
char* options = "a";
FILE* fp = open_file_demo(filepath, options);
assert(fp != NULL);
char content[][16] = {"write ", "file ", "demo"};
int i = 0;
for(i = 0; i < 3; i++)
{
char* str = content[i];
//int siz = fwrite(str, 1, strlen(str), fp);
//if(ferror(fp)) perror("fwrite failure!");
/* write strlen(str) chars into fp. */
int rtv = fputs(str, fp); /* rtv = 0 means success. */
if(rtv == EOF) perror("fputs failure!"); /* rtv = EOF(-1) means error. */
}
fputs("\n", fp);
if(ferror(fp)) perror("fputs failure!");
if(fclose(fp) != 0) perror("fclose failure!");
}
void read_file_demo()
{
char* filepath = "D:/xxx.txt";
char* options = "r";
FILE* fp = open_file_demo(filepath, options);
assert(fp != NULL);
while (feof(fp) == 0)
{
char line[128] = {0};
/* make sure sizeof(line)-1 */
/* fread could read many lines at one time. */
/* it will read sizeof(line)-1 chars or to the EOF. */
// int siz = fread(line, 1, sizeof(line)-1, fp);
/* fgets just read a line or sizeof(line)-1 chars at one time. */
// char* rtv = fgets(line, sizeof(line)-1, fp);
int siz = read_line(line, sizeof(line)-1, fp);
if (ferror(fp))
{
perror("fread failure!");
break;
}
printf("%s", line);
}
if(fclose(fp) != 0) perror("fclose failure!");
}
int main()
{
write_file_demo();
read_file_demo();
puts("Exit");
getchar();
return 0;
}