首先我们来看fgets()和fputs()
char * fgets ( char * str, int num, FILE * stream );
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 a the End-of-File is reached, whichever comes first.
从流中读取字符,并将它们作为C字符串存储到str中,直到(number -1)字符被读取,或者到达换行符或文件结束符(无论哪个先到达)。
str Pointer to an array of chars where the string read is stored. num Maximum number of characters to be read (including the final null-character). Usually, the length of the array passed as str is used. stream Pointer to a FILE object that identifies the stream where characters are read from. |
int fputs ( const char * str, FILE * stream );
Write string to stream
Writes the string pointed by str to the stream.
The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This final null-character is not copied to the stream.
将被str指向的字符串写到流里面。
函数从str特定的地址拷贝一个字符串直到遇到特定字符\0,这个结束符不会被拷贝到流当中去。
str An array containing the null-terminated sequence of characters to be written. stream Pointer to a FILE object that identifies the stream where the string is to be written. |
举个栗子:
#include<stdio.h>
#include<stdlib.h>
int main()
{char *buf=NULL;
FILE *rp = NULL;
FILE *wp = NULL;
int a = 0;
rp = fopen("C:\\Users\\Z7M\\Desktop\\1.txt", "r");
wp = fopen("C:\\Users\\Z7M\\Desktop\\2.txt", "w");
if (NULL == rp)
{
printf("打开错误\n");
return 0;
}
if (NULL == wp)
{
printf("打开错误\n");
return 0;
}
while (fgetc(rp) != EOF)
{
a++;
}
rewind(rp);//将指针移到文件开头
buf = (char*)malloc(a + 2);
if (buf == NULL)
{
return 0;
}
fgets(buf, a+1, rp);//向左只读了四个要读取的最大字符数(包括最后的空字符)。通常,使用作为str传递的数组长度。
fputs(buf, wp);//向右和puts的区别就是puts输出终端是dos而fputs输出终端是一个文件
puts(buf);
fclose(rp);
rp = NULL;
fclose(wp);
wp = NULL;
free(buf);//释放只要不超出这块空间就行
buf = NULL;
system("pause");
return 0;
}
注意到文件的读写遇到换行符就会结束。
接下来看fscanf()和fprintf()
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 tag within the format string.
读一个格式化数据到流里
从流中读取数据,并根据参数格式将数据存储到附加参数所指向的位置。附加参数应该指向已经分配的对象,该对象的类型由它们在格式字符串中的对应格式标记指定。
int fprintf ( FILE * stream, const char * format, ... );
Write formatted output to stream
Writes to the specified stream a sequence of data formatted as the format argument specifies. After the format parameter, the function expects at least as many additional arguments as specified in format.
将格式化输出写入流
将按格式参数指定格式的数据序列写入指定的流。在format参数之后,函数期望至少有与格式中指定的一样多的附加参数。
举个栗子:
#include<stdio.h>
#include<stdlib.h>
int main()
{int a, b, c;
FILE *rp = NULL;
FILE *wp = NULL;
rp = fopen("C:\\Users\\Z7M\\Desktop\\1.txt", "r");
wp = fopen("C:\\Users\\Z7M\\Desktop\\2.txt", "w");
if (NULL == rp)
{
printf("打开错误\n");
return 0;
}
if (NULL == wp)
{
printf("打开错误\n");
return 0;
}
/*scanf("%2d%3d%1d", &a, &b, &c);
printf("a=%2d,b=%3d,c=%1d", a, b, c);*/
fscanf(rp,"%2d%3d%1d", &a, &b, &c);
fprintf(wp,"a=%2d,b=%3d,c=%1d", a, b, c);
fprintf(wp, "you are beautiful");
fclose(rp);
rp = NULL;
fclose(wp);
wp = NULL;
system("pause");
return 0;
}