请大家帮我看看问题何在?

meteor135 2003-05-18 10:34:46
//这时一个简单的文件处理小工具,功能是输入一个文本文件,
//经处理后删掉逗号,句号,和制表符代之以空格。
//但是有问题。
//我的编译环境是VC6.0。
#include <iostream>
#include <fstream>
using namespace std;
char* getDir(char *filepath)
{
char *dir;
char *temp;
char *fileName = strrchr(filepath, '\\');
int dirLen=strlen(filepath)-strlen(fileName)+1;
temp=dir=new char[dirLen+1];
for(int i=0;i<dirLen;i++)
*temp++=*filepath++;
*temp='\0';
return dir;
}

bool creatZipedFile(char *agv[])
{
ifstream infile(agv[1]);
if(!infile)
{
cerr<<"Can't open "<<agv[1]<<endl;
cin.get();
return false;
}

ofstream outfile;

char *ZipedFilePath = getDir(agv[0]);
strcat(ZipedFilePath,"ZipedFile.cpp");
cout<<ZipedFilePath<<endl;
outfile.open(ZipedFilePath);
if(!outfile)
{
cerr<<"Can't creat ZipedFile "<<ZipedFilePath<<endl;
cin.get();
return false;
}

char word;
bool space=false;
while(infile>>word)
{
while(word==','||word=='.'||word=='\t')
//这里我并没有删除回车,回车符怎么也不见了?
{
infile>>word;
space = true;
}
if(space)
{
outfile<<' ';
space=false;
}
outfile<<word;
}

cout<<"success!"<<endl;

infile.close();
outfile.close();
delete[] ZipedFilePath;
return true;
}

void main(int agr, char *agv[])
{
if(agr<2)
{
cerr<<"Parameters Error!"<<endl
<<"It needs two parameters."<<endl
<<"First is the source file name"<<endl
<<"and the second is the dest file name."<<endl
<<"Press any key to quit"<<endl;
}
else if(!creatZipedFile(agv))
{
cerr<<"There's something wrong when processing."<<endl
<<"Please contact to the author:"<<endl
<<"Meter@mail.biti.edu.cn"<<endl;
}
cout<<"Bye!";//程序还没到这里就报错了,为什么?
}
...全文
56 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
meteor135 2003-05-19
  • 打赏
  • 举报
回复
steedhorse(晨星) 能不能帮我看看这里的问题?
http://expert.csdn.net/Expert/topic/1796/1796515.xml?temp=.7543299
晨星 2003-05-18
  • 打赏
  • 举报
回复
个人觉得这种在函数内分配空间,函数外释放的风格不太好,最好把路径信息作为参数传给getDir,而getDir仅负责获取路径信息。即将getChar声明为:
void getDir(char *dir, contst char *filepath)
{
……
}

然后这样调用。
char dir[128];//当然,也可以动态的精确分配dir的的长度,那样既有灵活性,健壮性也好。
getDir(dir , argv[1])
strcat(dir , "……")
……
huigll 2003-05-18
  • 打赏
  • 举报
回复
//我的编译环境是VC6.0。
//试试看
#include <iostream>
#include <fstream>
using namespace std;
char* getDir(char *filepath)
{
char *dir;
char *temp;
char *fileName = strrchr(filepath, '\\');
int dirLen=strlen(filepath)-strlen(fileName)+1;
temp=dir=new char[dirLen+1];
for(int i=0;i<dirLen;i++)
*temp++=*filepath++;
*temp='\0';
return dir;
}

bool creatZipedFile(char *agv[])
{
ifstream infile(agv[1]);
if(!infile)
{
cerr<<"Can't open "<<agv[1]<<endl;
cin.get();
return false;
}

ofstream outfile;

char *ZipedFilePath = getDir(agv[0]);
strcat(ZipedFilePath,"ZipedFile.cpp");
cout<<ZipedFilePath<<endl;
outfile.open(ZipedFilePath);
if(!outfile)
{
cerr<<"Can't creat ZipedFile "<<ZipedFilePath<<endl;
cin.get();
return false;
}

char word;
bool space=false;
while(infile.get(word))//原来的是读取的可见字符
{
while(word==','||word=='.'||word=='\t')
//这里我并没有删除回车,回车符怎么也不见了?
{
infile>>word;
space = true;
}
if(space)
{
outfile<<'\t';
space=false;
}
outfile<<word;
}

cout<<"success!"<<endl;

infile.close();
outfile.close();
// delete[] ZipedFilePath;//不能delete
return true;
}

void main(int agr, char *agv[])
{
#if 1
agr=2;
agv[1]="dd.txt";
#endif
if(agr<2)
{
cerr<<"Parameters Error!"<<endl
<<"It needs two parameters."<<endl
<<"First is the source file name"<<endl
<<"and the second is the dest file name."<<endl
<<"Press any key to quit"<<endl;
}
else if(!creatZipedFile(agv))
{
cerr<<"There's something wrong when processing."<<endl
<<"Please contact to the author:"<<endl
<<"Meter@mail.biti.edu.cn"<<endl;
}
cout<<"Bye!";//程序还没到这里就报错了,为什么?
}
晨星 2003-05-18
  • 打赏
  • 举报
回复
报错原因:你在函数getDir中仅仅为dir分配了dirLen+1大小的空间,该指针返回给ZipedFilePath后,却进行了strcat操作,strcat函数不负责额外空间的分配,所以追加上去的字符串造成了数组越界,所以出现错误。
解决方法是在getDir中为dir分配更多的空间。

至于回车,那是因为流操作“>>”读取字符时,会自动跳过回车。所以应改用其他方法,我对流式读取不太熟悉,如果使用文件指针,则影视用fgetc()函数。
晨星 2003-05-18
  • 打赏
  • 举报
回复
空格和制表符我没有注意,但通过VC60对程序的单步跟踪,我确定会跳过回车。
meteor135 2003-05-18
  • 打赏
  • 举报
回复
//原来的问题已经解决
//这时一个简单的文件处理小工具,功能是输入一个文本文件,
//经处理后删掉逗号,句号,和制表符代之以空格。
//我的编译环境是VC6.0。

#include <iostream>
#include <fstream>
using namespace std;

char* getDir(char *filepath)
{
char *dir;
char *temp;
char *fileName = strrchr(filepath, '\\');
int dirLen=strlen(filepath)-strlen(fileName)+1;
temp=dir=new char[dirLen+14];
for(int i=0;i<dirLen;i++)
*temp++=*filepath++;
*temp='\0';
return dir;
}

bool creatZipedFile(char *agv[])
{
ifstream infile(agv[1]);
if(!infile)
{
cerr<<"Can't open "<<agv[1]<<endl;
cin.get();
return false;
}

ofstream outfile;

char *ZipedFilePath = getDir(agv[0]);
strcat(ZipedFilePath,"ZipedFile.cpp");

outfile.open(ZipedFilePath);
if(!outfile)
{
cerr<<"Can't creat ZipedFile "<<ZipedFilePath<<endl;
cin.get();
return false;
}

char word;
bool space=false;
while(infile.get(word))
{
while(word==','||word=='.'||word=='\t')
//这里我并没有删除回车,回车符怎么也不见了?
{//问题解决,将原来的<<改为get(char),一切OK.
//2003.5.18
infile.get(word);
space = true;
}
if(space)
{
outfile<<' ';
space=false;
}
outfile<<word;
}

infile.close();
outfile.close();
delete[] ZipedFilePath;
return true;
}

void main(int agc, char *agv[])
{
if(agc<2)
{
cerr<<"Parameters Error!"<<endl
<<"It needs two parameters."<<endl
<<"First is the source file name"<<endl
<<"and the second is the dest file name."<<endl
<<"Press any key to quit"<<endl;
}
else if(creatZipedFile(agv))
cout<<"success!"<<endl
<<"Bye!"<<endl;
else
cerr<<"There's something wrong when processing."<<endl
<<"Please contact to the author:"<<endl
<<"Meter@mail.biti.edu.cn"<<endl;
}
meteor135 2003-05-18
  • 打赏
  • 举报
回复
谢谢楼上关心!说实话,我等人回复就等了好久了。
================================================================
报错原因:你在函数getDir中仅仅为dir分配了dirLen+1大小的空间,该指针返回给ZipedFilePath后,却进行了strcat操作,strcat函数不负责额外空间的分配,所以追加上去的字符串造成了数组越界,所以出现错误。
解决方法是在getDir中为dir分配更多的空间。
====================================================================
一语惊醒梦中人啊!
====================================================================
至于回车,那是因为流操作“>>”读取字符时,会自动跳过回车。所以应改用其他方法,我对流式读取不太熟悉,如果使用文件指针,则影视用fgetc()函数。
===================================================================
你确定会跳过会车?
我可是使用读取char操作阿,这也会跳过回车?那那些空格还有制表符是不是也不用判断了?
期待回答……

70,023

社区成员

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

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