一: – fgets( )
#include <stdio.h>
char *fgets(char *s, int size, FILE *stream);
功能:從文件中讀取內容
參數:
s:保存讀取到的內容
size:每次讀取的最大個數
stream:文件指針
返回值:
成功:讀取的數據的首地址
失敗:NULL 如果文件內容讀取完畢,也返回NULL
案例:
#include <stdio.h>#include <string.h>
int main(int argc, char const *argv[]){
//使用fgets從終端讀取數據#if 0
//注意事項:
// 如果輸入的字符串的長度小於fgets的第二個參數,則會將換行符也當做一個字符保存在第一個參數裏面
//char buf[32] = {0};
//fgets(buf, sizeof(buf), stdin); //"hello world\n"
//處理由fgets從終端獲取字符串中的\n
//buf[strlen(buf) - 1] = '\0';
// 如果輸入的字符串的長度大於fgets的第二個參數,則只能讀取到第二個參數-1個字符,最後一個位置自動補\0
//char buf[32] = {0}; //fgets(buf, 5, stdin);
// 如果保存fgets讀取數據的字符串不夠大,則會出現內存溢出的錯誤,所以保證第一個參數足夠大
char buf[5] = {0};
fgets(buf, 10, stdin);
printf("buf = %s\n", buf);#endif
if(argc < 2)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
return -1;
}
//使用fgets從文件中讀取數據
FILE *fp;
if((fp = fopen(argv[1], "r")) == NULL)
{
perror("fail to fopen");
return -1;
}
//注意:如果fgets從文件中讀取內容
// 1、不管一次讀多少個字節,只要遇到每一行的行結束符\n,就會立即結束讀取
// 2、如果fgets的第二個參數小於一行的內容,只會讀取fgets的第二個參數-1個字符,最後一個位置補\0
#if 0
char buf[32] = {};
fgets(buf, 8, fp);
printf("[%s]\n", buf);
fgets(buf, 8, fp);
printf("[%s]\n", buf);
#endif
char buf[32] = {0};
while(fgets(buf, sizeof(buf), fp) != NULL)
{
printf("%s", buf);
}
return 0;
}
二:-- fputs( )
#include <stdio.h>
int fputs(const char *s, FILE *stream);
功能:向文件寫入數據
參數:
s:要寫入的內容
stream:文件指針
返回值:
成功:寫入文件內容的字節數
失敗:EOF
案例:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char const *argv[]){
//使用fputs向終端寫入數據
//fputs("hello world", stdout);
//fflush(stdout);
//sleep(10);
//使用fputs向文件寫入數據
FILE *fp;
if((fp = fopen("file.txt", "w+")) == NULL)
{
perror("fail to fopen");
return -1;
}
fputs("hello world\n", fp);
fputs("nihao beijing\n", fp);
//由於fputs寫入內容後改變了文件的偏移量,所以此時文件的偏移量在最後一個字節的下一個位置
//所以讀取數據是什麼也讀取不到
// char buf[32] = {0};
// fgets(buf, 32, fp);
// printf("buf = %s\n", buf);
return 0;}
三:-- fread( )
#include <stdio.h>
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:從文件中讀取數據
參數:
ptr:保存讀取的數據
size:每次讀取的字節數
nmemb:一共讀取的次數
stream:文件指針
返回值:
成功:實際讀取的次數(對象數、塊數)
失敗:0 如果文件內容讀取完畢,返回0
案例:
#include <stdio.h>
typedef struct{
int a;
int b;
char c;
char d[32];}MSG;
int main(int argc, char const *argv[]){
//使用fread從文件中讀取數據
FILE *fp;
if((fp = fopen("file.txt", "r")) == NULL)
{
perror("fail to fopen");
return -1; }
// char buf[32] = {0};
// fread(buf, 2, 5, fp);
// printf("buf = %s\n", buf);
// int num;
// fread(&num, 4, 1, fp);
// printf("num = %d\n", num);
// int b[4] = {0};
// fread(b, 4, 4, fp);
// printf("%d %d %d %d\n", b[0], b[1], b[2], b[3]);
MSG msg;
fread(&msg, sizeof(msg), 1, fp);
printf("%d - %d - %c - %s\n", msg.a, msg.b, msg.c, msg.d);
return 0;
}
四:-- fwrite( )
#include <stdio.h>
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
功能:向文件中寫入數據
參數:
ptr:要寫入的數據
size:一次寫入的字節數
nmemb:一共寫入的次數
stream:文件指針
返回值:
成功:實際寫入的次數
失敗:0
案例
#include <stdio.h>
typedef struct{
int a;
int b;
char c;
char d[32];}MSG;
int main(int argc, char const *argv[])
{
//使用fwrite向文件寫入數據
FILE *fp;
if((fp = fopen("file.txt", "w")) == NULL)
{
perror("fail to fopen");
return -1;
}
//寫入字符串
//char buf[] = "1234567890";
//fwrite(buf, 2, 5, fp);
//寫入整數
//int a = 97868;
//fwrite(&a, 4, 1, fp);
//寫入數組
//int a[4] = {100, 200, 300, 400};
//fwrite(a, 4, 4, fp);
//寫入結構體
MSG msg = {666, 888, 'w', "zhangsan"};
fwrite(&msg, sizeof(msg), 1, fp);
return 0;
}
五:-- ftell( )
#include <stdio.h>
long ftell(FILE *stream);
功能:獲取當前文件的偏移量
參數:
stream:文件指針
返回值:
獲取當前文件的偏移量
六:-- fseek()
#include <stdio.h>
int fseek(FILE *stream, long offset, int whence);
功能:設置文件位置指針的偏移量
參數:
stream:文件指針
offset:偏移量 可正可負也可爲0
whence:相對位置 SEEK_SET 文件起始位置 SEEK_CUR 文件當前位置 SEEK_END 文件末尾位置(最後一個字符後面一個位置)
返回值:
成功:0
失敗:-1
七:-- rewind( )
#include <stdio.h>
void rewind(FILE *stream);
功能:將文件位置定位到起始位置
參數:
stream:文件指針
返回值:無
rewind(fp) <==> fseek(fp, 0, SEEK_SET);
案例:
#include <stdio.h>
int main(int argc, char const *argv[]){
//寫操作的文件偏移量#if 0
FILE *fp;
if((fp = fopen("file.txt", "w")) == NULL)
{
perror("fail to fopen");
return -1;
}
fputs("abcdefghijklmnopq", fp);
//使用ftell獲取文件偏移量
printf("offset = %ld\n", ftell(fp));
//使用fseek修改文件偏移量
//rewind(fp);
//fseek(fp, 0, SEEK_SET);
fseek(fp, -5, SEEK_END);
fputs("12345", fp);#endif
#if 0
FILE *fp;
if((fp = fopen("file.txt", "r")) == NULL)
{
perror("fail to fopen");
return -1;
}
char buf[32] = {0};
fgets(buf, 6, fp);
printf("buf = %s\n", buf);
printf("offset = %ld\n", ftell(fp));
fseek(fp, 3, SEEK_SET);
printf("offset = %ld\n", ftell(fp));
fgets(buf, 6, fp);
printf("buf = %s\n", buf);
#endif
FILE *fp;
//如果fopen打開或者創建一個文件時使用的是的a或者a+權限
//則寫操作的偏移量無法改變,只能在文件的末尾位置
//但是讀操作沒有任何影響
if((fp = fopen("file.txt", "a+")) == NULL)
{
perror("fail to fopen");
return -1;
}
// char buf[32] = {0};
// fgets(buf, 6, fp);
// printf("buf = %s\n", buf);
// printf("offset = %ld\n", ftell(fp));
// fseek(fp, 3, SEEK_SET);
// printf("offset = %ld\n", ftell(fp));
// fgets(buf, 6, fp);
// printf("buf = %s\n", buf);
fputs("888888888", fp);
printf("offset = %ld\n", ftell(fp));
fseek(fp, 3, SEEK_SET);
printf("offset = %ld\n", ftell(fp));
fputs("666666", fp);
return 0;}