首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

(新手请问)关于为什么添加换行符就使代码无法正常动作的情况

2013-07-09 
(新手请教)关于为什么添加换行符就使代码无法正常动作的情况本人是新手,请求高手支招:我编了一个查询时时

(新手请教)关于为什么添加换行符就使代码无法正常动作的情况
本人是新手,请求高手支招:
我编了一个查询时时彩连续三十期未出0的历史记录程式,此程式已能正常动作:
程式代码如下:

//时时彩三十期未出0,历史查询

#include<iostream>
#include<string>
#include<fstream>
using namespace std; 
struct lottery
{
string phase;    //声名字符串,储存时时彩的期号
char number[5];//声名数组,储存每期时时彩的开奖号码
}property[300000];//声名三十万个成员的结构数组,储存时时彩每期的历史记录  
int main() 
{    
char filename[50] = "shishicai.txt";//声名字符型数组储存时时彩历史数据文档名
ifstream infile;//声名ifstream变量
infile.open(filename);//与文件关联
if(!infile.is_open())//检查文件是否正常打开
{
cout<<"could not open the file "demo6_9"\n";
cout<<"program terminating!\n";
exit(EXIT_FAILURE);
}
for( long i=0; i<300000 && infile.good() ; i++)//将文件数据导入结构
{
infile>>property[i].phase;
char ch;
infile.get(ch);
for( int j=0; j<10 && infile.good() ;j++)
{
if(ch!=',')
{
property[i].number[j/2]=ch;
}
infile.get(ch);
}
}
if (infile.eof())
cout<<"End of file reached.\n";
else if (infile.fail())
cout<<"Input terminated by data mismatch.\n";
else
cout<<"Input terminated for unknown reason.\n";
infile.close();
int count;
for(long j=0;j<300000;j++)//逐个查询property.number[]中的第一个字符,看是否等于‘0’
{
for(int k=0;k<5;k++)
{
if(property[j].number[k]!='0')
count++;
if(property[j].number[k]=='0')
count=0;
if(count==150)
{
cout<<property[j].phase;//如果等于三十期未出0,也就是150个连续开奖号没有0,就显示期号
count=0;
}
}
}
return 0;
}

此程式能正常显示期号。

现在我的问题是,为什么我在显示期号代码后面加上   <<endl;   程式就无法正常显示了,程式将会连续显示换行,直到程式崩溃。
[解决办法]
推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。

不要把
fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待



fopen("...","...b");fread,fwrite,fclose  //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待
弄混了

热点排行