| 文件test.txt的相关信息 |
| 文件test.txt的相关信息 |
fgetc 读取一个字符 (读取一个后会自动来到下一个字符)
int fgetc ( FILE * stream ); 从文件中读取一个字符,并返回字符的ASCII码值,实参为文件指针,或者标准输入流,stdin 键盘
从流中获取字符
返回指定
流的内部文件位置指示器当前所指向的字符。然后,内部文件位置指示器将前进到下一个字符。
如果调用流时位于文件末尾,则该函数返回
EOF 并设置流 (
feof)
的文件结束指示符。
如果发生读取错误,该函数将返回
EOF 并设置流(
ferror)
的错误指示器。
fgetc 和
getc 是等效的,只是
getc 在某些库中可以作为宏实现。
参数
流
指向标识输入流的 FILE 对象的指针。
返回值
成功后,将返回读取的字符(提升为 int 值)。即返回字符的ASCII码值
fputc 输入一个字符
int fputc ( int character, FILE * stream ); 可以输入字符,也可以输入字符的ASCII码值,向流输出成功后,会返回字符的ASCII码值,流,包括文件指针流,实参为文件指针,和标准输出流,stdout 即屏幕
Write character to stream
Writes a
character to the
stream and advances the position indicator.
The character is written at the position indicated by the
internal position indicator of the
stream, which is then automatically advanced by one.
Parameters
character
The int promotion of the character to be written.
The value is internally converted to an unsigned char when written.
stream
Pointer to a FILE object that identifies an output stream.
Return Value On success, the character written is returned. 输入成功返回字符的ASCII码值 fgets 每次读取文件的一行,再次读取会自动读取下一行, 适用于所有流 fgets char * fgets ( char * str, int num, FILE * stream ); str是数据的起始存储位置,即从str指向的地址开始存储,一般就是字符数组首元素的地址, num是最多可存放多少个字符,这由字符数组大小决定 Get string from stream Reads characters from stream and stores them as a C string into str until ( num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first. A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str. Parametersstr
Pointer to an array of chars where the string read is copied.
num
Maximum number of characters to be copied into str (including the terminating null-character).
fputs 写一个字符串到流里面去fputs int fputs ( const char * str, FILE * stream ); ( 传入字符串,流 ) (“hello,world”,pf)也可直接传入字符数组首元素的地址 ( arr ,流 )流包括文件输入流,和标准输出流 Write string to stream Writes the C string pointed by str to the stream. The function begins copying from the address specified ( str) until it reaches the terminating null character (' '). This terminating null-character is not copied to the stream. Notice that fputs not only differs from puts in that the destination stream can be specified, but also fputs does not write additional characters, while puts appends a newline character at the end automatically. Parameters
str
C string with the content to be written to stream.
stream
Pointer to a FILE object that identifies an output stream.
格式化的输出输入 格式化的输出 ,意思就是,你想以什么样的格式输出,就自己定好,输出进去便可 struct S { int n; float score; char arr[10]; }; int main() 3.14f 表示是float类型,防止被默认为double { struct S s={100,3.14f,"bit"}; FILE* pf=fopen("test.txt","w"); if(pf==NULL) { return 0; } //格式化的形式写文件 fprintf ( pf ,"%d %f %s ",s.n,s.score,s.arr);想写啥类型的数据,自己在括号里定好 fprintf 可以把一长串字符串输入到文件里面去 fclose(pf); pf=NULL; return 0; }printf
int printf ( const char * format, ... );
Print formatted data to stdout 针对标准输出流
fprintf
int fprintf ( FILE * stream, const char * format, ... ); 针对流
Write formatted data to stream
Writes the C string pointed by
format to the
stream. If
format includes
format specifiers (subsequences beginning with %), the additional arguments following
format are formatted and inserted in the resulting string replacing their respective specifiers.
After the
format parameter, the function expects at least as many additional arguments as specified by
format.
Parameters
stream
Pointer to a FILE object that identifies an output stream.
格式化的输入 struct S { int n; float score; char arr[10]; }; int main() { struct S s={0}; //表示未初始化 FILE* pf=fopen("test.txt","w"); if(pf==NULL) { return 0; } //格式化的形式读文件 //格式化的输入数据 fscanf(pf,"%d %f %s ",&(s.n),&(s.score),s.arr);//取结构体变量的地址,可以直接用 s.n 访问结构体变量,然后直接 &(s.n)取其地址 printf( "%d %f %s ",s.n,s.score,s.arr); fclose(pf); pf=NULL; return 0; }scanf
int scanf ( const char * format, ... );
fscanf
int fscanf ( FILE * stream, const char * format, ... );
Read formatted data from stream
Reads data from the
stream and stores them according to the parameter
format into the locations pointed by the additional arguments.
The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the
format string.
Parameters
stream
Pointer to a FILE object that identifies the input stream to read data from.
scanf/printf 是针对标准输入流/标准输出流的格式化输入/输出语句 fscanf/fprintf 是针对所有输入流/所有输出流的格式化输入输出语句 sscanf / sprintf 是从字符串中读取格式化的数据/把格式化的数据存储到字符串sscanf 从一个字符串中拿出一个格式化的数据 s表示字符串操作
int sscanf ( const char * s, const char * format, ...); (字符串起始地址,格式,地址........)
Read formatted data from string
sprintf 把格式化的数据写到一个字符串里面去
int sprintf ( char * str, const char * format, ... ); ( 字符串起始地址, 格式 数据.........)
Write formatted data to string
struct
S
{
int
n;
float
score;
char
arr[10];
};
int
main()
{
struct
S
s = { 100,3.14f,
"abcdef"
};
//能不能将结构体里存的数据转化成一个字符串
struct S temp = {0};
char
buf[1024] = { 0 };
//结构体里的数据,就是我们所说的格式化的数据
//把格式化的数据存储到 buf
sprintf(buf,
"%d %f %s"
, s.n, s.score, s.arr);
// s.arr 表示访问数组
// printf(
"%sn"
, buf);
//从buf中读取格式化的数据到tmp结构体中
sscanf(buf, "%d %f %s",&(tmp.n),&(tmp.score),tmp.arr);
return
0;
}
打印结果为 100 3.140000 abcdef 这里打印时,就已经是在打印字符串了
二进制的输入和输出
二进制输出 fwrite 写 输出指向文件输出
fwrite
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream ); ( ptr 当前存储数据的位置 , 元素的字节大小 , 最多写入多少个 , 指针指向的文件结构 ) ,最多写入多少个由文件大小决定
Write block of data to stream
struct
S
{
char
name[20];
int
age;
double
score;
};
int main()
{
struct S s = { "张三",20,55.6 };
FILE* pf = fopen("D:\QQ\test.txt", "wb"); //以二进制形式写,用wb
if (pf == NULL)
{
return 0;
}
//二进制的形式写文件
fwrite(&s, sizeof(struct S), 1, pf);
fclose(pf);
pf = NULL;
return 0;
}
二进制输入
fread 读 输入指向外界输入
fread
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream ); ( ptr 拿出的数据应该存放的位置 , 元素的大小 , 最多读取多少个 , 指针指向的文件结构 ) ptr用于存储数据的位置也可以是数组,数组的大小决定了最多可以读取多少个 返回值类型 size_t 其实就是返回整型数值,该数值是成功读取的元素个数
Read block of data from stream
int
main()
{
struct
S
tmp = { 0 };
FILE
* pf = fopen(
"D:\QQ\test.txt"
,
"rb"
);
//以二进制形式读,用rb
if
(pf ==
NULL
)
{
return
0;
}
//二进制的形式读文件
fread(&tmp,
sizeof
(
struct
S
), 1, pf);//以二进制的形式读出数据
printf(
"%s %d %lfn"
, tmp.name, tmp.age, tmp.score);//输出到屏幕上,电脑自然能读懂二进制
fclose(pf);
pf =
NULL
;
return
0;
}
数据库
MySQL
..........................
文件存放数据 1.不安全
2.大量数据,不高效
C语言操作 MySQL 数据库
下面这个非常重要,以后常会用到
文件的随机读取 fseek 根据文件指针的位置和偏移量来定位文件指针
解释一下: 文件里面存放了, a b c d e f 上来我就要从e开始读取 以上的函数显然是做不到的
a b c d e f 刚打开时,文件指针默认指向开始位置
fseek
int fseek ( FILE * stream, long int offset, int origin ); ( 文件指针 , 偏移量 ,文件指针的设定位置 ) 若起始位置在 a 选择偏移量为 2 则文件指针会被移动到 c ,当然偏移量可以为负
Move the file pointer to a specified location 移动文件指针到特定的地方
origin 是说明从什么位置开始定位指针 SEEK_END 末尾处
fwrite
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream ); ( ptr 当前存储数据的位置 , 元素的字节大小 , 最多写入多少个 , 指针指向的文件结构 ) ,最多写入多少个由文件大小决定
Write block of data to stream
struct
S
{
char
name[20];
int
age;
double
score;
};
int main()
{
struct S s = { "张三",20,55.6 };
FILE* pf = fopen("D:\QQ\test.txt", "wb"); //以二进制形式写,用wb
if (pf == NULL)
{
return 0;
}
//二进制的形式写文件
fwrite(&s, sizeof(struct S), 1, pf);
fclose(pf);
pf = NULL;
return 0;
}
二进制输入
fread 读 输入指向外界输入
fread
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream ); ( ptr 拿出的数据应该存放的位置 , 元素的大小 , 最多读取多少个 , 指针指向的文件结构 ) ptr用于存储数据的位置也可以是数组,数组的大小决定了最多可以读取多少个 返回值类型 size_t 其实就是返回整型数值,该数值是成功读取的元素个数
Read block of data from stream
int
main()
{
struct
S
tmp = { 0 };
FILE
* pf = fopen(
"D:\QQ\test.txt"
,
"rb"
);
//以二进制形式读,用rb
if
(pf ==
NULL
)
{
return
0;
}
//二进制的形式读文件
fread(&tmp,
sizeof
(
struct
S
), 1, pf);//以二进制的形式读出数据
printf(
"%s %d %lfn"
, tmp.name, tmp.age, tmp.score);//输出到屏幕上,电脑自然能读懂二进制
fclose(pf);
pf =
NULL
;
return
0;
}
数据库
MySQL
..........................
文件存放数据 1.不安全
2.大量数据,不高效
C语言操作 MySQL 数据库
下面这个非常重要,以后常会用到
文件的随机读取 fseek 根据文件指针的位置和偏移量来定位文件指针
解释一下: 文件里面存放了, a b c d e f 上来我就要从e开始读取 以上的函数显然是做不到的
a b c d e f 刚打开时,文件指针默认指向开始位置
fseek
int fseek ( FILE * stream, long int offset, int origin ); ( 文件指针 , 偏移量 ,文件指针的设定位置 ) 若起始位置在 a 选择偏移量为 2 则文件指针会被移动到 c ,当然偏移量可以为负
Move the file pointer to a specified location 移动文件指针到特定的地方
origin 是说明从什么位置开始定位指针 SEEK_END 末尾处
fseek int fseek ( FILE * stream, long int offset, int origin ); ( 文件指针 , 偏移量 ,文件指针的设定位置 ) 若起始位置在 a 选择偏移量为 2 则文件指针会被移动到 c ,当然偏移量可以为负 Move the file pointer to a specified location 移动文件指针到特定的地方 origin 是说明从什么位置开始定位指针 SEEK_END 末尾处
| Constant | Reference position |
| SEEK_SET | Beginning of file起始位置 |
| SEEK_CUR | Current position of the file pointer当前位置 |
| SEEK_END | End of file *末尾位置 |



