2009-05-10 31 views

Trả lời

8
#include "stdio.h" 

int main(void) 
{ 
    FILE* f = fopen("so-data.dat", "r+b"); // Error checking omitted 
    fseek(f, 5, SEEK_SET); 
    fwrite("x", 1, 1, f); 
    fclose(f); 
} 
+0

A (f! = NULL) là bắt buộc. fclose (NULL) gọi UB. – dirkgently

+0

fwrite ("x", 1, 1, f); Điều này không ghi byte đầu tiên của địa chỉ của chuỗi "x"? –

+0

Không, nó không - bỏ qua tôi :-) –

5
FILE* fileHandle = fopen("filename", "r+b"); // r+ if you need char mode 
fseek(fileHandle, position_of_byte, SEEK_SET); 
fwrite("R" /* the value to replace with */, 1, 1, fileHandle); 
+1

"rw" cắt ngắn tệp. Bạn cần "rb". –

+0

sizeof (char) == 1, theo định nghĩa. – dirkgently

+1

Mọi người đều đúng :) Thời gian dài không có C. –

3
#include <stdio.h> /* standard header, use the angle brackets */ 

int main(void) 
{ 
    char somechar = 'x'; /* one-byte data */ 
    FILE* fp = fopen("so-data.txt", "r+"); 
    if (fp) { 
     fseek(fp, 5, SEEK_SET); 
     fwrite(&somechar, 1, 1, fp); 
     fclose(fp); 
    } 
    return 0; /* if you are on non-C99 systems */ 
} 
Các vấn đề liên quan