導航:首頁 > 版本升級 > 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對文件輸入不刪除原有內容相關的資料

熱點內容
win10恢復刪除資料 瀏覽:742
e2180超頻教程 瀏覽:705
科技兔編程怎麼樣 瀏覽:822
win10設置卡住打不開 瀏覽:846
拉伸代碼 瀏覽:359
蘋果4如何查看wifi密碼 瀏覽:882
日期減去空白格數據錯誤怎麼辦 瀏覽:116
二手房網站哪個好玩 瀏覽:607
喪心病狂的網路廣告 瀏覽:257
ios9的appstore沒了 瀏覽:91
被格式化的硬碟文件能恢復嗎 瀏覽:64
php壓縮程序 瀏覽:504
借錢的單據是什麼文件 瀏覽:74
編程用什麼開發 瀏覽:236
如何投訴鷹角網路 瀏覽:838
linux空口令怎麼檢測 瀏覽:394
linux信號kill 瀏覽:834
編程軟體哪個工作好 瀏覽:424
u盤顯示木馬文件刪除怎麼恢復 瀏覽:860
蘋果相冊怎麼看照片在哪個文件夾 瀏覽:635

友情鏈接