文件写入 初学者谁教教我啊

seeerrrrrsfsafsdf 2010-07-25 08:12:48
编写一个程序,从一个文件中读出每一行信息,然后把2到10位置的数据换成00000001-xxxxxxxx ,写入另一个文件.



比如源文件内容是:

Abcdefg12345333333333

333334567878555555566

转换后的内容应该是:

Ab0000000145333333333

330000000278555555566

...全文
117 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
elegant87 2010-07-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 swapljp111 的回复:]

我按4楼的试了 怎么直接显示Can not open the file! 我创建了文件了啊
[/Quote]
你先在当前目录建一个文件,保持你要读的数据
tracyjk 2010-07-25
  • 打赏
  • 举报
回复
路过接分…
alandingking 2010-07-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cattycat 的回复:]

简单写了个,你看看。
C/C++ code
#include <stdio.h>
#include <string.h>

void main ()
{
FILE *fp,*fp1;
char buf[128];
char i='1';
int j;
fp=fopen("a.txt","r");
fp1=fopen("b.txt","w");

while(!feof(fp))
{
fgets(buf,128,fp);
for(j=2;j<9;j++)
buf[j]='0';
buf[j]=i;
i++;//这里是不是只考虑了9行之内的?i大于9应该就有问题了吧?
fprintf(fp1,"%s\n",buf);
}
fclose(fp);
fclose(fp1);
}

[/Quote]
cattycat 2010-07-25
  • 打赏
  • 举报
回复
文件要和你的程序可执行文件在同一目录。不知道你用的什么ide和编译器。
我写的那个我这里执行没有问题。
seeerrrrrsfsafsdf 2010-07-25
  • 打赏
  • 举报
回复
我是初学者啊 才看1个多星期书 2楼的没编完 谁帮我编完啊。。。。。
zxk860611 2010-07-25
  • 打赏
  • 举报
回复
2楼的就挺好的,简洁,用4楼的做什么
wade_2003 2010-07-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 swapljp111 的回复:]
我按4楼的试了 怎么直接显示Can not open the file! 我创建了文件了啊
[/Quote]
你要在当前工程目录下创建文件
swapljp111 2010-07-25
  • 打赏
  • 举报
回复
我按4楼的试了 怎么直接显示Can not open the file! 我创建了文件了啊
elegant87 2010-07-25
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *fp;
char str1[50],str2[50];
int i;
//从文件中读取
fp=fopen("data.txt","r");
if(!fp)
{
printf("Can not open the file!");
exit(0);
}
fgets(str1,50,fp);
fgets(str2,50,fp);

printf("%s\n",str1);
printf("%s\n",str2);

fclose(fp);

for(i=2;i<10;++i)
{
str1[i]='0';
str2[i]='0';
}
str1[10]='1';
str2[10]='1';

//写入文件
fp=fopen("another.text","w");
if(!fp)
{
printf("Can not open the file!");
exit(0);
}

fputs(str1,fp);
fputs(str2,fp);
printf("成功写入文件!");
fclose(fp);

system("pause");
return 0;
}
wxwlll 2010-07-25
  • 打赏
  • 举报
回复
楼主发了两个帖子,用循环替换后,就是最基本的文件写入操作!
wuyu637 2010-07-25
  • 打赏
  • 举报
回复
fopen(file1);
fopen(file2);
char buffer[100];
char buffer2[100];
while(getline(buffer,infile))
{
strncpy(buffer2,buffer,2);
strncpy(buffer2+2,"00000001",8);
strncpy(buffer+10,buffer+10,strlen(buffer)-10);

fwrite(...);
}
cattycat 2010-07-25
  • 打赏
  • 举报
回复
简单写了个,你看看。
#include <stdio.h>
#include <string.h>

void main ()
{
FILE *fp,*fp1;
char buf[128];
char i='1';
int j;
fp=fopen("a.txt","r");
fp1=fopen("b.txt","w");


while(!feof(fp))
{
fgets(buf,128,fp);
for(j=2;j<9;j++)
buf[j]='0';
buf[j]=i;
i++;
fprintf(fp1,"%s\n",buf);
}
fclose(fp);
fclose(fp1);
}

70,024

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧