1.5 printf 和 scanf
- printf()
#include <stdio.h>
int printf(const char *format, ...);
int fprintf(FILE *stream, const char *format, ...);
int dprintf(int fd, const char *format, ...);
int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);
int atoi(const char *nptr);
long atol(const char *nptr);
long long atoll(const char *nptr);
#include <stdarg.h>
int vprintf(const char *format, va_list ap);
int vfprintf(FILE *stream, const char *format, va_list ap);
int vdprintf(int fd, const char *format, va_list ap);
int vsprintf(char *str, const char *format, va_list ap);
int vsnprintf(char *str, size_t size, const char *format, va_list ap);
- scanf()
#include <stdio.h>
int scanf(const char *format, ...);
int fscanf(FILE *stream, const char *format, ...);
int sscanf(const char *str, const char *format, ...);
#include <stdarg.h>
int vscanf(const char *format, va_list ap);
int vsscanf(const char *str, const char *format, va_list ap);
int vfscanf(FILE *stream, const char *format, va_list ap);
1.6 fseek、ftell 和 rewind
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence); //设置指针位置
long ftell(FILE *stream); //返回当前指针位置
void rewind(FILE *stream); //重定向到文件开头
DESCRIPTION
The fseek() function sets the file position indicator for the stream
pointed to by stream. The new position, measured in bytes, is obtained
by adding offset bytes to the position specified by whence. If whence
is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to
the start of the file, the current position indicator, or end-of-file,
respectively. A successful call to the fseek() function clears the
end-of-file indicator for the stream and undoes any effects of the
ungetc(3) function on the same stream.
The ftell() function obtains the current value of the file
position indicator for the stream pointed to by stream.
The rewind() function sets the file position indicator for the stream
pointed to by stream to the beginning of the file. It is equivalent
to:
(void) fseek(stream, 0L, SEEK_SET)
except that the error indicator for the stream is also cleared (see
clearerr(3)).
-----------------------------------------------------------------------------
#include <stdio.h>
int fseeko(FILE *stream, off_t offset, int whence);
off_t ftello(FILE *stream);
#define _FILE_OFFSET_BITS 64
CFLAGS+=-D_FILE_OFFSET_BITS=64
- fseek
int fseek(FILE *stream, long offset, int whence);
------------------------------------------------------------
stream 流指针
offset 偏移量
whence 起始位置
|SEEK_SET | 0 |文件开头|
|SEEK_CUR | 1 |文件当前|
|SEEK_END | 2 |文件结尾|
1.7 fflush
#include <stdio.h>
int fflush(FILE *stream);
DESCRIPTION
For output streams, fflush() forces a write of all user-space
buffered data for the given output or update stream via the stream's
underlying write function.
For input streams associated with seekable files (e.g., disk files,
but not pipes or terminals), fflush() discards any buffered data that
has been fetched from the underlying file, but has not been consumed
by the application.
The open status of the stream is unaffected.
If the stream argument is NULL, fflush() flushes all open output
streams.
For a nonlocking counterpart, see unlocked_stdio(3).
RETURN VALUE
Upon successful completion 0 is returned. Otherwise, EOF is returned
and errno is set to indicate the error.
- 示例
#include <stdio.h>
#include <string.h>
int main()
{
char buff[1024];
memset( buff, '\0', sizeof( buff ));
fprintf(stdout, "启用全缓冲\n");
setvbuf(stdout, buff, _IOFBF, 1024);
fprintf(stdout, "该输出将保存到 buff\n");
fflush( stdout );
fprintf(stdout, "这将在编程时出现\n");
fprintf(stdout, "最后休眠五秒钟\n");
sleep(5);
return(0);
}
------------------------------------------------------------------------
让我们编译并运行上面的程序,这将产生以下结果。在这里,程序把缓冲输出保存到 buff,直到首次调用 fflush() 为止,然后开始缓冲输出,最后休眠 5 秒钟。它会在程序结束之前,发送剩余的输出到 STDOUT。
____引用 runoob.com
1.8 getline
SYNOPSIS
#include <stdio.h>
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
DESCRIPTION
getline() reads an entire line from stream, storing the address of
the buffer containing the text into *lineptr. The buffer is null-
terminated and includes the newline character, if one was found.
If *lineptr is set to NULL and *n is set 0 before the call, then
getline() will allocate a buffer for storing the line. This buffer
hould be freed by the user program even if getline() failed.
Alternatively, before calling getline(), *lineptr can contain a
pointer to a malloc(3)-allocated buffer *n bytes in size. If the
buffer is not large enough to hold the line, getline() resizes it
with realloc(3), updating *lineptr and *n as necessary.
In either case, on a successful call, *lineptr and *n will be updated
to reflect the buffer address and allocated size respectively.
- 示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main (int argc, char *argv[]){
FILE *fp;
char *linebuf;
size_t linesize;
int count = 0;
if (argc < 2){
fprintf(stderr, "Usage:%s <src_file>\n", argv[0]);
exit(-1);
}
fp = fopen(argv[1], "r");
if (fp == NULL){
perror("fopen()");
exit(-1);
}
ssize_t read;
linebuf = NULL;
linesize = 0;
read = 0;
while (1){
if ((read = getline(&linebuf, &linesize, fp)) < 0){
break;
}
printf("%d\n", strlen(linebuf));
}
// free(linebuf);
// linebuf = NULL;
fclose(fp);
return 0;
}
----------------------------------------------------------------------
统计文件每行字符数量
2 临时文件
问题:
1.如何不冲突地创建文件。
2.没有及时销毁临时文件。
- 函数
tmpnam tmpfile
char *tmpnam(char *s); //返回一个可用的名字 可能被大断
The tmpnam() function returns a pointer to a string that is a valid
filename, and such that a file with this name did not exist at some
point in time, so that naive programmers may think it a suitable name
for a temporary file.If the argument s is NULL, this name is generated
in an internal static buffer and may be over‐written by the next call
to tmpnam(). If s is not NULL, the name is copied to the character
array (of length at least L_tmpnam) pointed to by s and the value s is
returned in case of success.
The created pathname has a directory prefix P_tmpdir. (Both L_tmpnam
and P_tmpdir are defined in <stdio.h>, just like the TMP_MAX mentioned
below.)
-----------------------------------------------------------------------------
FILE *tmpfile(void); //匿名文件 关闭文件时直接销毁 不会冲突 不会被打断
DESCRIPTION
The tmpfile() function opens a unique temporary file in binary
read/write (w+b) mode. The file will be automatically deleted when
it is closed or the program terminates.