C++文件操作错误

shenwenbin05 2010-11-10 11:11:16
我想通过文件操作将test.txt的内容读出并显示在屏幕上,其中test.txt的内容如下:
#include <iostream>
#include <string>
#include <algoritm>
#include <vector>
#include <functional>
#include <iterator>
using namespace std;

#include <stdio.h>
int const MAXLINE = 5;

int main(int argc,char **argv)
{
char buff[MAXLINE];
int len;

fgets(buff, MAXLINE, stdin);
len = strlen( buff );
if ( buff[len - 1] == '\n' )
{
len--;
}

cout << "fgets:" << buff << endl;
cout << "fgets end" << endl;

system("pause");
return 0;
}

现在出现了下面两个问题:
1、下面这段程序可以在VS2008上正常运行,但是在gcc下却只显示:
open test.txt successfully!
#include <iostream>

源程序:

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

int const MAXLINE = 20;

int main(int argc,char **argv)
{
char buff[MAXLINE] = "test.txt";

fstream infile(buff, ios::in);
if ( infile == NULL)
{
cerr << "can't open file " << buff << endl;
exit(0);
}
cout << "open " << buff << " successfully!" << endl;

infile.getline(buff, MAXLINE);
cout << buff << endl;

char ch;
while ( infile.get(ch) ) //读取文件里的内容
{
cout << ch;
}
infile.close();

system("pause");
return 0;
}


2、文件读取错误
将源程序修改成下面的:

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

int const MAXLINE = 20;

int main(int argc,char **argv)
{
char buff[MAXLINE] = "test.txt";

fstream infile(buff, ios::in);
if ( infile == NULL)
{
cerr << "can't open file " << buff << endl;
exit(0);
}
cout << "open " << buff << " successfully!" << endl;

while ( !infile.eof() )
{
memset(buff, 0, MAXLINE);
infile.getline(buff, MAXLINE);
cout << buff << endl;

}
infile.close();

system("pause");
return 0;
}

这时会进入死循环,经过单步调试,发现,在读到#include <functional>这句话的时候出现了错误
如果将int const MAXLINE = 20;改为int const MAXLINE = 50;则程序就一点错误也没有了。


不知道如何处理,请各位高手帮忙!
...全文
301 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
昵称很不好取 2010-11-11
  • 打赏
  • 举报
回复
判断下,如果流状态不是good,则重新设置下流的状态
昵称很不好取 2010-11-11
  • 打赏
  • 举报
回复
如果一行数据过多,导致geiline的缓冲区不够容纳的时候就会设置failbit流标志,所以getline调用一直不会成功,所以文件feof一直不能返回true,进而导致死循环了
	_Myt& __CLR_OR_THIS_CALL getline(_Elem *_Str,
streamsize _Count, _Elem _Delim)
{ // get up to _Count characters into NTCS, discard _Delim
_DEBUG_POINTER(_Str);
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);

if (_Ok && 0 < _Count)
{ // state okay, use facet to extract
int_type _Metadelim = _Traits::to_int_type(_Delim);

_TRY_IO_BEGIN
int_type _Meta = _Myios::rdbuf()->sgetc();

for (; ; _Meta = _Myios::rdbuf()->snextc())
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_State |= ios_base::eofbit;
break;
}
else if (_Meta == _Metadelim)
{ // got a delimiter, discard it and quit
++_Chcount;
_Myios::rdbuf()->sbumpc();
break;
}
else if (--_Count <= 0)
{ // buffer full, quit
_State |= ios_base::failbit;
break;
}
else
{ // got a character, add it to string
++_Chcount;
*_Str++ = _Traits::to_char_type(_Meta);
}
_CATCH_IO_END
}

*_Str = _Elem(); // add terminating null character
_Myios::setstate(_Chcount == 0 ? _State | ios_base::failbit : _State);
return (*this);
}
赵4老师 2010-11-11
  • 打赏
  • 举报
回复
摒弃fstream
使用FILE *
#include <stdio.h>
FILE *f;
#define MAX_LINE_LEN 256
char ln[MAX_LINE_LEN];
int main() {
f=fopen("test.txt","r");
if (NULL==f) {
printf("Can not open file test.txt!\n");
return 1;
}
while (1) {
if (NULL==fgets(ln,MAX_LINE_LEN,f)) break;
printf("%s",ln);
}
fclose(f);
return 0;
}

记不得哪位C++大牛在哪本学习C++的书的前言里面说过
“用C语言1000行源码能完成的工作千万不要用C++重写!”
shenwenbin05 2010-11-11
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 pink9527 的回复:]
buff, MAXLINE刚好12个字节,而在gcc下至少要定义MAXLINE为12
[/Quote]

我这里定义的MAXLINE =20啊
xspace_time 2010-11-11
  • 打赏
  • 举报
回复
buff, MAXLINE刚好12个字节,而在gcc下至少要定义MAXLINE为12
昵称很不好取 2010-11-10
  • 打赏
  • 举报
回复
调用rdstate打印下流的状态,看是否设置了出错的位
gules 2010-11-10
  • 打赏
  • 举报
回复
用string替换buf
shenwenbin05 2010-11-10
  • 打赏
  • 举报
回复
难道就这样了?
herman~~ 2010-11-10
  • 打赏
  • 举报
回复
用string直接getline出来吧
shenwenbin05 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ptrunner 的回复:]
所以你出现的问题肯定是越界问题所致
[/Quote]

可不可以说的详细点,到底是如何越界啊........
shenwenbin05 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liudong1105 的回复:]
infile.getline(buff, MAXLINE);这句话就到头了,后面再获取就越界了infile.get(ch)
[/Quote]

MAXLINE = 20,
infile.getline(buff, MAXLINE);这句话怎么会到头呢?文件明明没有结束啊
pstrunner 2010-11-10
  • 打赏
  • 举报
回复
while ( !infile.eof() )
{
memset(buff, 0, MAXLINE);
infile.getline(buff, MAXLINE);
cout << buff << endl;

}
=====================================
函数ifstream.getline()是读入文件的一行,就是直到回车'\n'为止。
所以你出现的问题肯定是越界问题所致,其实你说的下面这句话就已经说明问题。
“如果将int const MAXLINE = 20;改为int const MAXLINE = 50;则程序就一点错误也没有了。”
me115 2010-11-10
  • 打赏
  • 举报
回复
infile.getline(buff, MAXLINE);这句话就到头了,后面再获取就越界了infile.get(ch)
这样是没问题的
//infile.getline(buff, MAXLINE);
//cout << buff << endl;

char ch;
string strr;
while (infile.get(ch) ) //读取文件里的内容
{
//cout <<strr;
cout << ch;
}
me115 2010-11-10
  • 打赏
  • 举报
回复
这样写:
//infile.getline(buff, MAXLINE);
//cout << buff << endl;

//char ch;
string strr;
while ( getline(infile,strr))//infile.get(ch) ) //读取文件里的内容
{
cout <<strr;
//cout << ch;
}
shenwenbin05 2010-11-10
  • 打赏
  • 举报
回复
各位前辈,可不可以说的详细点啊

64,654

社区成员

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

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