能不能写个程序,
随机产生一个>1而且<9999的数字,打开文件aa.txt,找到5678替换成随机产生的数字。
这样的程序偶在行啊呵呵
主要两个模块
随机数产生模块和文档查找替换(废话!?)
后者楼上的斑竹说的比较清楚了,不过具体的还是让高手做吧
再补充一句,偶建议用子串判断。
[此贴子已经被作者于2004-11-10 04:58:49编辑过]
随机数产生模块
#include <stdlib.h> #include <stdio.h> #include <time.h> int main(void) { int k; srand(time(NULL)); k=random(9999); printf("%d",k); }
哪位高手帮忙写个文件读写模块?
直接在你的程序上修改了一下:
#include <stdio.h> #include <time.h> #include <stdlib.h> int main(void) { int k; FILE * file,*file1; char msg[80],*ptr;
if ( (file = fopen("1.txt", "r")) == NULL) { perror("Error:"); return 1; } if ( (file1 = fopen("11.txt", "w")) == NULL) { perror("Error:"); return 1; } fgets(msg, sizeof(msg)-1, file); srand(time(NULL)); k=random(9999); ptr=msg; while(*ptr)ptr++; ptr-=2; while(k)*ptr--=k%10+48,k/=10; fputs(msg,file1); fclose(file); fclose(file1); }
谢大侠的指点,
可是还没能解决问题。
是要修改一个文件,而不是两个。
大侠的程式是按顺序找到第五个字符修改,
我原意是读文件,如果读到5678,就修改。
文件内容可能是123456789,要是12749382756789124呢?这程式不行哟!