C 語言基礎教程

C 語言流程控制

C 語言函數

C 語言數組

C 語言指針

C 語言字符串

C 語言結構體

C 語言文件

C 其他

C 語言參考手冊

C 文件 fputs()和fgets()

C語言編程中的fputs()和fgets()用于從流中寫入和讀取字符串。讓我們看看使用fgets()和fgets()函數編寫和讀取文件的示例。

寫入文件:fputs()函數

fputs()函數將一行字符寫入文件。它將字符串輸出到流。

語法:

int fputs(const char *s, FILE *stream)

#include<stdio.h>  
#include<conio.h>  
void main(){  
    FILE *fp;  
    clrscr();  
      
    fp=fopen("myfile2.txt","w");  
    fputs("hello c programming",fp);  
      
    fclose(fp);  
    getch();  
}

myfile2.txt

hello c programming

讀取文件:fgets()函數

fgets()函數從文件讀取一行字符。它從流中獲取字符串。

語法:

char* fgets(char *s, int n, FILE *stream)

#include<stdio.h>  
#include<conio.h>  
void main(){  
    FILE *fp;  
    char text[300];  
    clrscr();  
      
    fp=fopen("myfile2.txt","r");  
    printf("%s",fgets(text,200,fp));  
      
    fclose(fp);  
    getch();  
}

輸出:

hello c programming
丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清