C語言編程中的fputs()和fgets()用于從流中寫入和讀取字符串。讓我們看看使用fgets()和fgets()函數(shù)編寫和讀取文件的示例。
fputs()函數(shù)將一行字符寫入文件。它將字符串輸出到流。
語法:
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()函數(shù)從文件讀取一行字符。它從流中獲取字符串。
語法:
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