Standard I/O library common functions use
1.fopen functions: Input and output for files and terminals.
#include <stdio.h>
FILE * fopen (const char* filename, const char *mod);
2.fclose function: Closes the specified file stream stream.
#include <stdio.h>
int fclose (FILE *stream);
3.GETCWD function: Writes the name of the current directory to BUF.
#include <unistd.h>
char* getcwd (char* buf, size_t size);
#include <stdio.h>#include<unistd.h>int main () { char Acfilepath [[] = {0}; sizeof (Acfilepath)); printf ("path is%s \ r \ n", acfilepath); return 0 ;}
Ex
4.fgets function: Reads a string from the input file stream stream.
#include <stdio.h>
Char *fgets (char *s, int n, FILE *stream);
Fgets writes a string into the string that s points to, until one of the following occurs:
- Encountering line breaks
- N-1 characters have been transferred
- Reach the end of the file.
It also passes the newline symbol encountered to the receiving string , plus a null byte to the end. You can transfer up to n-1 characters at a time.
5 format input and output
#include <stdio.h>
int fprintf (FILE *stream, const char *format, ...)
Sends its own output to a specified file stream.
#include <stdio.h>int main () { *pfile = fopen ("/home/xiaodeyao/wzh/code/3_ Io/new.txt","wb+"); " The NUM is%d and the string is%s " 1024x768 " Hello world! " ); return 0 ;}
Ex
#include <stdio.h>
int fscanf (FILE *stream, const char *format, ...)
Reads formatted input from stream stream.
#include <stdio.h>intMain () {FILE*pfile = fopen ("/home/xiaodeyao/wzh/code/3_io/fscanf.txt","R"); intA =0; Characdate[ -] = {0}; floatDou =0; Charc =0; FSCANF (Pfile,"%d%s%c%f", &a, Acdate, &c, &Dou); printf ("%d,%s,%c,%f\r\n", A, acdate, C, Dou); return 0;}
Ex
PS: Here is the file mod if "W", there will be a problem. But in some cases, "w" can be used. Example: http://www.runoob.com/cprogramming/c-function-fscanf.html
Here's a little bit of a dig.
6.fseek ()
Specifies the location for the next file read and write operation.
int fseek (FILE *stream, long int offset, int whence)
Stream-This is a pointer to the file object that identifies the stream.
Offset-This is the relative whence, in bytes.
Whence-This is where you start adding offset offsets. It is generally specified as one of the following constants:
Constant description
The beginning of the Seek_set file
Seek_cur the current position of the file pointer
Seek_end the end of the file
#include <stdio.h>intMain () {FILE*FP; FP= fopen ("file.txt","w+"); Fputs ("This is w3cschool.cc", FP); Fseek (FP,7, Seek_set); Fputs ("C Programming Langauge", FP); Fclose (FP); return(0);}
Ex
7.ftell ()
Long int ftell (FILE *stream)
Parameters
Stream-This is a pointer to the file object that identifies the stream.
return value
The function returns the current value of the position identifier. If an error occurs, -1l is returned, and the global variable errno is set to a positive value.
#include <stdio.h>intMain () {FILE*FP; intLen; FP= fopen ("file.txt","R"); if(fp = =NULL) {Perror ("Open File Error"); return(-1); } fseek (FP,0, Seek_end); Len=Ftell (FP); Fclose (FP); printf ("Total size of file.txt =%d bytes \ n", Len); return(0);}
Ex
Reference:
Http://www.runoob.com/cprogramming/c-function-ftell.html
Linux Learning 7-file operations