奇怪的问题,我跟踪了几遍,实在看不出来问题在哪里!!

shuirh 2009-03-09 12:52:13
#include <iostream>
using namespace std;

int count(FILE *fp)
{
int sum_ = 0;

char ch = fgetc(fp);
while (ch != EOF)
{
ch = fgetc(fp);
sum_++;
}
return sum_;
cout << "该文件中的字节总数为:" << sum_ << endl;
}
main()
{
FILE *fp = fopen("update.txt", "r");
//char buf[100] = "";
char *pBuf = NULL;
char *pLookFor;
int location = 1;
int sum= count(fp);

pBuf = new char(sum + 1);
rewind(fp);
fread(pBuf, sum, 1, fp);
cout << pBuf << endl;

for (pLookFor = pBuf; pLookFor < pBuf + sum; pLookFor++)
{
if (*pLookFor == 10)
{
cout << "回车换行符的位置为:" << location << endl;
}
location++;
}
}

新建update.txt,内容为:
file1=1.0
file2=2.0
file3=1.1
file4=1.3

程序运行到cout << "回车换行符的位置为:" << location << endl;报错,跟踪变量,没看出什么,大家帮忙看看,谢谢了.
...全文
118 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
topwork 2009-03-09
  • 打赏
  • 举报
回复
pBuf = new char(sum + 1);
写错了,因该写成
pBuf = new char[sum + 1]; 中括号



  • 打赏
  • 举报
回复
运行完没错误...楼主什么编译器啊?
youyifang 2009-03-09
  • 打赏
  • 举报
回复
把这句pBuf = new char(sum + 1); 改成pBuf = new char【sum + 1】; 看看
别忘了,释放资源。
kevinnick 2009-03-09
  • 打赏
  • 举报
回复
运行成功,没有显示问题,你要把你报的什么错贴出来
lingyin55 2009-03-09
  • 打赏
  • 举报
回复
up
ljmscsq 2009-03-09
  • 打赏
  • 举报
回复
我在codeblocks下运行了下,没错误
ysysbaobei 2009-03-09
  • 打赏
  • 举报
回复
yangch_nhcmo 2009-03-09
  • 打赏
  • 举报
回复
linux GCC 4.3.2 下正常运行输出

#include <iostream>
using namespace std;

int count(FILE *fp)
{
int sum_ = 0;

char ch = fgetc(fp);
while (ch != EOF)
{
ch = fgetc(fp);
sum_++;
}
cout << "该文件中的字节总数为:" << sum_ << endl;
return sum_;

}
int main()
{
FILE *fp = fopen("update.txt", "r");
if(fp == NULL)
{
cout << "OPEN ERROR" << endl;
return 1;
}
//char buf[100] = "";
char *pBuf = NULL;
char *pLookFor;
int location = 1;
int sum = count(fp);

//pBuf = new char(sum + 1);
pBuf = new char[sum + 1];
rewind(fp);
fread(pBuf, sum, 1, fp);
cout << pBuf << endl;

for (pLookFor = pBuf; pLookFor < pBuf + sum; pLookFor++)
{
if (*pLookFor == 10)
{
cout << "回车换行符的位置为:" << location << endl;
}
location++;
}
return 0;
}

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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