導航:首頁 > 版本升級 > c對文件輸入不刪除原有內容

c對文件輸入不刪除原有內容

發布時間:2023-06-18 13:45:31

㈠ C語言中有沒有先清除原文件中內容再在該文件中讀入新的內容的函數

fopen("文件名","w")就可以了
C語言中規定,打開時以"w"方式打開文件時,如果源文件中有內容,先清空源文件的內容再供寫入

#include "stdio.h"
main()
{ FILE *fp;
int n;
scanf("%d",&n);
fp=fopen("temp.txt","w");
fprintf(fp,"%d",n);
fclose(fp);
}
我給你個程序,你運行多次,每次運行時輸入不同的值,每次運行完成後查看「temp.txt」文件中的內容,再有問題,和我聯系!

㈡ 在C語言中,fopen一個文件 如何能夠在寫入新的數據覆蓋原文件中指定長度的內容

程序示例

程序示例1#include#include //為了使用exit()int main(){char ch;FILE* fp;char fname[50]; //用於存放文件名printf("輸入文件名:");scanf("%s",fname);fp=fopen(fname,"r"); //只供讀取if(fp==NULL) //如果失敗了{printf("錯誤!");exit(1); //中止程序}//getc()用於在打開文件中獲取一個字元while((ch=getc(fp))!=EOF)putchar(ch);fclose(fp); //關閉文件return 0;}注意!初學者往往會犯一個錯誤,即在輸入文件名時不加後綴名,請注意加上!程序示例2[2]#includeFILE *stream, *stream2;int main( void ){int numclosed;// Open for read (will fail if file "crt_fopen.c" does not exist)if( (stream = fopen( "crt_fopen.c", "r" )) == NULL ) // C4996// Note: fopenis deprecated; consider usingfopen_sinsteadprintf( "The file 'crt_fopen.c' was notopened " );elseprintf( "The file 'crt_fopen.c' wasopened " );// Open for writeif( (stream2 = fopen( "data2", "w+" )) == NULL ) // C4996printf( "The file 'data2' was not opened " );elseprintf( "The file 'data2' was opened " );// Close stream if it is not NULLif( stream){if (fclose( stream ) ){printf( "The file 'crt_fopen.c' was not closed " );}}// All other files are closed:numclosed = _fcloseall( );printf( "Number of files closed by _fcloseall: %u ", numclosed );}[3]


閱讀全文

與c對文件輸入不刪除原有內容相關的資料

熱點內容
一個虛擬主機怎麼建多少網站 瀏覽:825
和家親app為什麼老是掉線 瀏覽:990
兒童編程思維課有什麼用 瀏覽:600
魔百和網路機頂盒怎麼連接電視 瀏覽:611
國產顯示器icc配置文件 瀏覽:52
java編程常見的語法糖有哪些 瀏覽:41
jspmysql選課源碼 瀏覽:877
ipadmini2下載app等待 瀏覽:399
creo工程圖配置文件 瀏覽:699
編程和鋼琴哪個貴 瀏覽:841
移動進銷存app哪個好 瀏覽:600
編程製作游戲什麼原理 瀏覽:97
linux如何查看是否有共享文件夾 瀏覽:264
u盤拷貝文件以後為空 瀏覽:917
快雲主機資料庫連接方法 瀏覽:756
javagsp定位 瀏覽:384
jsp頁面表格導出excel 瀏覽:976
imagetest教程 瀏覽:244
怎樣將一個cad文件包圖紙兼容 瀏覽:898
論文有什麼好的網站 瀏覽:581

友情鏈接