#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
/*
* EOF: The symbolic constant with a value of-1.
* Fgets () reads up to int-1 characters and returns when a newline or EOF is encountered.
* Fputs () When writing a file, ignore the string cutoff character ' \ '.
* Fread () and fwrite () data blocks are read and written, and are used in structure arrays (sequential stored structures).
*
* Function prototype:
Read
* int fgetc (FILE *stream);
* Char *fgets (char *s, int size, FILE *stream);
* size_t fread (void *ptr, size_t size, size_t nmemb, FILE *stream);
* int fscanf (FILE *stream, const char *format, ...);
Write
* int FPUTC (int c, FILE *stream);
* int fputs (const char *s, FILE *stream);
* size_t fwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream);
* int fprintf (FILE *stream, const char *format, ...);
Other
* Void Rewind (FILE *stream); Points the location pointer inside the file back to the beginning of a stream (data flow/file).
*
* Open mode:
* R (Read): Reading
* W (write): Write
* A (Append): Append
* +: Read and Write
* t (Text): Literal file, can be omitted without writing
* B (banary): binary file
*/
FILE *FP = NULL;
READ
void Getcharfromfile (FILE *fp)
{
int ch = 0;
while ((CH=FGETC (FP) = EOF)//return EOF on Failure
{
printf ("Fget: [%c]\n", ch);
}
}
void Getlinefromfile (FILE *fp)
{
Char line[1024] = "";
while (fgets (line, sizeof (line), FP)! = NULL)//return NULL on Failure
{
printf ("Fgets: [%s]\n", line);
}
}
void Getblockfromfile (FILE *fp)
{
int i = 0;
int block[10] = {0};
int count = 1;
while (fread (block, sizeof (block), count, fp) = = count)//failure return value! = Count
{
printf ("fread:");
for (i=0; i<10; i++)
{
printf ("%d", block[i]);
}
printf ("\ n");
}
}
void Readformattofile (FILE *fp)
{
Char file[32] = "";
Char func[32] = "";
Char date[32] = "";
int line = 0;
FSCANF (FP, "%s%s%d%[^\n]", File, Func, &line, date); Returns the number of Read elements, Eg:4
printf ("File:%s\n", file);
printf ("Func:%s\n", func);
printf ("Line:%d\n", line);
printf ("Date:%s\n", date);
}
WRITE
int Writechartofile (FILE *fp, Char ch)
{
int ret = 0;
ret = FPUTC (CH,FP); return EOF upon failure
return ret!=eof?0:-1;
}
int Writestrtofile (FILE *fp, char *str)
{
int ret = 0;
ret = fputs (str, FP);
return ret!=eof?0:-1; return EOF upon failure
}
int Writeblocktofile (FILE *fp, const void *block, int size, int count)
{
int ret = 0;
ret = fwrite (block, size, count, FP); return value on Failure ' = Count '
return ret!=count?-1:0;
}
Int Writeformattofile (FILE *fp)
{
int ret = 0;
ret = fprintf (FP, "%s%s%d%s\n", __file__, __func__, __line__, __date__);
return ret<0?-1:0;//fail with a negative value
}
MAIN
int main (int argc, char **argv)
{
char ch = ' R ';
Char *str = "Hello world.\n";
int block[10] = {1,2,3,4,5,6,7,8,9,0};
Char *filepath = "./ll";
fp = fopen (FilePath, "w+"); Do not care whether the file exists or not, each time the file is rewritten and readable
if (NULL = = fp)
{
Perror ("fopen");
return-1;
}
WRITE
printf ("Writechartofile:%s\n", Writechartofile (FP, ch)? " Fail ":" Success ");
printf ("Writestrtofile:%s\n", Writestrtofile (FP, str)? " Fail ":" Success ");
printf ("Writeblocktofile:%s\n", Writeblocktofile (FP, block, sizeof (block), 1)? " Fail ":" Success ");
printf ("Writeformattofile:%s\n", Writeformattofile (FP)? " Fail ":" Success ");
Rewind (FP);
READ
Getcharfromfile (FP);
Getlinefromfile (FP);
Getblockfromfile (FP);
Readformattofile (FP);
Fclose (FP);
return 0;
}
Basic knowledge of C language files