Static and extern:
static modifier variable (can be decorated with local variables), only the source code file containing that variable definition can be accessed (internal variables)
Extends the life cycle of local variables, from program initiation to exit
Function: variable scope defined by static is extended
Static statements that define variables are only executed once
extern-defined variables which source files can be used as long as they are declared (external variables)
extern can not modify local variables
The effect of static on global variables
Static modified global variables can only be used in the current file, not in other files
The static modifier is an intrinsic function (it can only be used in a defined file)
extern-modified functions can be used both internally and externally (can be declared in a header file or before use)
12) file Operation:
The fopen function is used to open a file in the general form:
File pointer variable name = fopen (filename, file usage)
FILE *FP = NULL;
fp = fopen ("A.txt", "R"); Open a file as read-only
if (fp! = NULL) {
Manipulating files
}else{
Prompt user
}
Fclose (FP);
Gracefully closes the file fclose returns 0, and if other returns, the shutdown error occurs. Failure to write file if not closed
File operation steps:
1) Create a file pointer
FILE *FP = NULL;
2) Open File
fopen (file name, operation mode);
If the opening succeeds, returns the first address of the file, and fails to return null;
character read and write functions:
FPUTC (character amount, file pointer);//The amount of characters to write can be a character constant or a variable
FPUTC (' A ', FP);
Cases:
char ch = ' X ';
FILE *FP = NULL;
fp = fopen ("Fputs.txt", "w");
if (fp! = NULL) {
FPUTC (CH, FP);
printf ("Write success");
ch = fget (FP);
}
Fclose (FP);
char s = fgetc (FP);
while (s! = EOF) {//eof file at the end of the flag
Putchar (s);
s = fgetc (FP); The system automatically reads the next character
}
Char ch;
FILE *FP = fopen ("InputString.txt", "w+");
if (fp = NULL) {
printf ("Please enter string:");
ch = getchar ();
int count = 0;
while (ch! = ' \ n ') {
count++;
FPUTC (CH, FP);
ch = getchar ();
}
printf ("entered altogether%d characters \ n", count);
Rewind (FP); Point the file pointer back to the file header
ch = fgetc (FP);
while (ch! = EOF) {
Putchar (CH);
ch = fgetc (FP);
}
Fclose (FP);
return 0;
}
Count = fputs (str, FP);//count returns the length of this write string
Char str[50]; Fgets read, when the contents of the file is read, it will automatically add the
Fget (str, sizeof (STR), FP); Fgets read, reading ends when read to \ n or EOF
Char *s = "ASDFASD";
Fputs (S, FP);
Char str[3];//length is 3, one man reads half, length is 4 read all
Fgets (str, sizeof (STR), FP);
Fread (buffer, size, count, FP)
fwrite (buffer, size, count, FP)
Buffer: Represents the first address that holds the input and output data.
Size: The number of bytes representing the block of data
Count: Indicates the number of blocks of data to read and write
FP: Represents a file pointer
int a= 1, b = 2;
fprintf (FP, "%d#%d;", A, b);
FSCANF (FP, "%d,%d", &arr[i][0], &arr[i][1]);
struct struct Stu;
fseek (file pointer, displacement amount, starting point)
Pointer positioning:
Fseek (FP, sizeof (struct Student), seek_set); Seek_set file First
To read information from a file:
Freak (&str, sizeof (struct Student), 1, FP);
C language files and special modifiers