c語言中的fclose
發布時間: 2025-06-11 05:20:54
A. c語言中 fclose(fp) 啥意思
fclose是C語言標准庫中的一個函數,功能是關閉一個流。
函數原型:int fclose(FILE *stream);
如果流成功關閉,fclose 返回 0,否則返回EOF(-1)。
如果流為NULL,而且程序可以繼續執行,fclose設定error number給EINVAL,並返回EOF。
B. 怎麼把c語言編的程序的結果輸入到一個文本文件中
c語租如旦言編橡局的程序的結果輸入到一個文本文件中可以使用fprintf;
例:
#include<stdio.h>
main(){
FILE *fpt;
fpt = fopen("wendangming.txt","w");//打開文檔弊擾,寫入
fprintf(fpt,"Hello world");
fclose(fpt);
}
(2)c語言中的fclose擴展閱讀
它打開一個文本文件,逐個字元地讀取該文件
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream testByCharFile;
int num;
char c;
testByCharFile.open("6.5.cpp",ios::in);
while(!testByCharFile.eof())
{
testByCharFile >> c;
num++;
}
testByCharFile.close();
cout << num << endl;
}
熱點內容