哭笑不得
这个问题太奇怪了,请各位试着运行我下面的代码,工作很简单,就是读一个文件,然后用system call来以特定的方式写进另一个新建的文件里,linux下运行
#include "unistd.h"
#include "fcntl.h"
#include <sys/types.h>
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
//step 1:
int f1,o1,r,w,rf1,rf2,fp,o2, o3, fx, w_xxx, w_www, del, total, l;
char buffer[100], print1[150], print2[150];
o1 = creat("XYZ.doc",0755); //create own file
f1 = open("xx.xx",0); //open file
while (r>0)
{
r = read(f1,&buffer,100); //read file
if (r <0) //if read operation failed...
{
cout<<"A read error has occurred!"<<endl;
exit(0);
}
w = write(o1, &buffer, 100); //write data into own file from xx.xx
char string[] = "X1Y2A3";
w = write(o1, &string, 6);
if(w<0) //if write operation failed...
{
cout<<"A write error has occurred!"<<endl;
exit(0);
}
lseek(f1, 0, 1); //locate to next character
}
close(o1); //close own file
fp = open("XYZ.doc", 0); //open file
lseek(f1,0,0); //locate to the beginning of file
memset(print1,'\0',sizeof(print1)); //intialize array for printing
memset(print2,'\0',sizeof(print2));
rf1 = read(f1, &print1, 120); //read data for printing
rf2 = read(fp, &print2, 120);
printf("%s\n",print1); //print
printf("%s\n",print2);
//cout<<print2<<endl;
close(fp);
close(f1); //close file xx.xx
return 0;
}
注意红字部分,程序应该是没有问题的,现在我们随便在红色部分变量声明的地方加一个变量,比如加个xx,使红字部份变为int f1,o1,r,w,rf1,rf2,fp,o2, o3, fx, w_xxx, w_www, del, total, l, xx;
请再运行看看,变量名可以随便取,反正只要你加个变量,不论是否用到,这个程序就出错了。。。
帮忙想想办法。。。