English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
No C, as funções fputs() e fgets() são usadas para escrever e ler strings do fluxo. Vamos ver um exemplo de como escrever e ler arquivos usando as funções fgets() e fputs().
A função fputs() escreve uma linha de caracteres no arquivo. Ela envia a string para o fluxo.
Sintaxe:
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
A função fgets() lê uma linha de caracteres do arquivo. Ela obtém a string do fluxo.
Sintaxe:
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(); }
Saída:
hello c programming