Hàm fputc ghi kí tự xuống file

int fputc(int c, FILE *stream);

Parameter:

C: mã ASCII của kí tự muốn ghi xuống file

stream: con trỏ file

Remark:

  • Hàm fputc( ) ghi 1 kí tự xuống file.
  • Hàm trả về mã ASCII của kí tự được ghi xuống file thành công
  • Hàm trả về mã EOF nếu lỗi xảy ra.

Ví dụ:

#include "stdio.h"
#include "conio.h"

void main()
{
    FILE *fp;
    char forum_name[] = "vncoding.net";
    char s[80];
    char *p;
    int n_byte;

    // Print line to stream using fputc. 
    p = forum_name;
    if((fp = fopen("D:\\data.txt","w+")) == NULL)
        printf("\nError in opening file");
    else
    {
        while( (*p != '\0') && fputc( *(p++), fp ) != EOF );
        fseek(fp, 0L, SEEK_SET);
        n_byte = fscanf(fp,"%s", s);
        printf("\nn_byte = %d", n_byte);
        printf("\n%s", s);
        fclose( fp );
    }
    getch(); 
}

Kết quả:

Hàm fputc ghi kí tự vào file
File data.txt

Be the first to comment

Leave a Reply