导航:首页 > 版本升级 > 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对文件输入不删除原有内容相关的资料

热点内容
百度云同步本地文件夹 浏览:413
wordpress上传目录 浏览:78
最新天天酷跑宠物代码 浏览:140
ps创建新像文件的组合键是什么 浏览:383
python抓取js 浏览:802
fckeditor数据库 浏览:467
三菱编程的bl是什么 浏览:858
jquery翻页代码 浏览:730
有手机没网络怎么办 浏览:984
微信视频聊天如何找到文件夹 浏览:951
java定时任务存取数据 浏览:45
word复制后有阴影 浏览:662
足球经理2014升级补丁 浏览:254
securecrt手机版本 浏览:27
ems寄纸质文件多少钱 浏览:296
红头文件文件号数叫什么 浏览:361
手机金山文件路径 浏览:931
数据线一安等于多少瓦 浏览:132
eclipse如何生成exe文件 浏览:601
苹果qq文件怎么打开 浏览:365

友情链接