寻求问题 [英] problem with seekg

查看:72
本文介绍了寻求问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在我的程序中使用的函数出现问题

从''命令文件'中读取句子并解析它们进入命令。

令人惊讶的是,该程序在某些计算机上工作正常,而在其他计算机上则不然。我试过调试,无法理解它。我将
缩小到了seekg函数并制作了这个简单的程序

(据我所知)似乎没有按预期在所有

到目前为止我试过的电脑。

如果我出错了请告诉我...


#include< iostream> ;

#include< fstream>

使用命名空间std;


int _tmain(int argc,_TCHAR * argv [] )

{

ifstream是(test.txt);

char test [256];

是> test;

cout<<测试<< endl;

is.seekg(-2,ios :: cur);

是> test;

cout<<测试<< endl;

返回0;

}


这里是test.txt的不同版本及其相应的输出

在所有情况下,我预计输出将是

createModel

el

我做错了什么?


案例1:test.txt的内容:

createModel


输出:

createModel

del


案例2:test.txt的内容

createModel

测试

/ *

* /

输出:

createModel

eModel


案例3:test.txt的内容:

createModel

测试

/ *

* /

CreateElements

196

DMElasticElement

0 195 1

-1

exitCreateElements


输出:

createModel


谢谢

Julian。

解决方案

Julian写道:




我在我的程序中使用的函数出现问题

读句子从''命令文件''并将它们解析为命令。

令人惊讶的是,该程序在某些计算机上工作正常,而在其他计算机上则不然。我试过调试,无法理解它。我将
缩小到了seekg函数并制作了这个简单的程序

(据我所知)似乎没有按预期在所有

到目前为止我试过的电脑。

如果我出错了请告诉我...


#include< iostream> ;

#include< fstream>

使用命名空间std;


int _tmain(int argc,_TCHAR * argv [] )

{

ifstream是(test.txt);

char test [256];



test [0] = 0;


是> test;

cout<<测试<< endl;

is.seekg(-2,ios :: cur);



test [0] = 0;


是> test;

cout<<测试<< endl;

返回0;

}


这里是test.txt的不同版本及其相应的输出

在所有情况下,我预计输出将是

createModel

el

我做错了什么?


案例1:test.txt的内容:

createModel


输出:

createModel

del


案例2:test.txt的内容

createModel

测试

/ *

* /

输出:

createModel

eModel


案例3:test.txt的内容:

createModel

测试

/ *

* /

CreateElements

196

DMElasticElement

0 195 1

-1

exitCreateElements


输出:

createModel


谢谢

朱利安。



记得考虑(看不见)

向后搜索$&b
每行末尾的换行符号(&\\; \\\在Windows上,\ n>

在unix / linux上," \ r"在Mac上(?))。


如果''test.txt''只有一行,则必须以换行符结尾;

否则结果不像预期的那样。例如for Windows:


createModel \\ n


c

" Larry Smith" ; < ls **** @ nospam.comwrote in message

news:yzCOh.26810


xE.11979@trnddc08 ...
< blockquote class =post_quotes>
Julian写道:


>
我遇到的问题我一直在使用在我的程序中
从''命令文件'中读取句子并将其解析为命令。令人惊讶的是,该程序在某些计算机上运行良好,而在其他计算机上则不然。我试过调试,无法理解它。我把它缩小到了seekg函数并制作了这个简单的程序
(根据我的理解)似乎没有像我预期的那样在所有的计算机中工作到目前为止尝试了。
如果我出错了,请告诉我。

#include< iostream>
#include< fstream>
使用namespace std;

int _tmain(int argc,_TCHAR * argv [])
{if / ifstream是(test.txt);
char test [ 256];



test [0] = 0;


>是> test;
cout<<测试<< endl;
is.seekg(-2,ios :: cur);



test [0] = 0;


>是> test;
cout<<测试<< endl;
返回0;
}
这里有不同版本的test.txt及其相应的
输出
在我预期的所有情况下输出将是
createModel
我在做错了什么?

案例1:test.txt的内容:
createModel

输出:
createModel
del

案例2:test.txt的内容
createModel
测试
/ *
* /
输出:
createModel
eModel

案例3:test.txt的内容:
createModel
测试 / *
* /
CreateElements
196
DMElasticElement
0 195 1
-1
exitCreateElements

输出:
createModel

感谢
朱利安。



记得考虑(看不见)
每行结束时
换行符号

向后搜索(在Windows上为\\\\ nn,\ n"
unix / linux上的
,Mac上的\ r(?))。


如果''test.txt''只有一行,则必须以换行结束;

否则结果不符合预期。例如对于Windows:


createModel \\\\ n



感谢回复...但我是不确定这是否解释了一切。即使

如果我需要考虑隐形角色,结果也不会产生

感觉......我尝试了各种组合..但它似乎它正在寻求超过2个补偿,但是我刚刚要求偏移2.如果我认为

这个不可见的字符,它应该有效地仅偏移1个可见

字符......相反它是抵消3


另一个问题:为什么结果随着

文件中的行数而变化? ...即使我只是在读第一行!

我拥有的行数越多,抵消的就越多。

Hi,
I am having problems with a function that I have been using in my program to
read sentences from a ''command file'' and parse them into commands. the
surprising thing is that the program works fine on some computers and not so
fine on others. I tried debugging and cannot make any sense of it. I
narrowed it down to the seekg function and made this simple program which
(from what I understand) does not seem to be working as expected in all the
computers I have tried so far.
please let me know if I have got something wrong...

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

int _tmain(int argc, _TCHAR* argv[])
{
ifstream is("test.txt");
char test[256];
is >test;
cout << test << endl;
is.seekg(-2,ios::cur);
is >test;
cout << test << endl;
return 0;
}

here are the different versions of test.txt and their corresponding outputs
in all the cases I expected that the output would be
createModel
el
what am I doing wrong ?

case 1: contents of test.txt :
createModel

OUTPUT:
createModel
del

case 2: contents of test.txt
createModel
testing
/*
*/
OUTPUT:
createModel
eModel

case 3: contents of test.txt :
createModel
testing
/*
*/
CreateElements
196
DMElasticElement
0 195 1
-1
exitCreateElements

OUTPUT:
createModel

thanks
Julian.

解决方案

Julian wrote:

Hi,
I am having problems with a function that I have been using in my program to
read sentences from a ''command file'' and parse them into commands. the
surprising thing is that the program works fine on some computers and not so
fine on others. I tried debugging and cannot make any sense of it. I
narrowed it down to the seekg function and made this simple program which
(from what I understand) does not seem to be working as expected in all the
computers I have tried so far.
please let me know if I have got something wrong...

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

int _tmain(int argc, _TCHAR* argv[])
{
ifstream is("test.txt");
char test[256];

test[0] = 0;

is >test;
cout << test << endl;
is.seekg(-2,ios::cur);

test[0] = 0;

is >test;
cout << test << endl;
return 0;
}

here are the different versions of test.txt and their corresponding outputs
in all the cases I expected that the output would be
createModel
el
what am I doing wrong ?

case 1: contents of test.txt :
createModel

OUTPUT:
createModel
del

case 2: contents of test.txt
createModel
testing
/*
*/
OUTPUT:
createModel
eModel

case 3: contents of test.txt :
createModel
testing
/*
*/
CreateElements
196
DMElasticElement
0 195 1
-1
exitCreateElements

OUTPUT:
createModel

thanks
Julian.

Remember to account for the (invisible)
newline chars at the end of each line when
seeking backwards ("\r\n" on Windows, "\n"
on unix/linux, "\r" on Mac(?) ).

If ''test.txt'' has just one line, it must end with a newline;
otherwise the result is not as expected. e.g. for Windows:

createModel\r\n


c
"Larry Smith" <ls****@nospam.comwrote in message
news:yzCOh.26810


xE.11979@trnddc08...

Julian wrote:

>Hi,
I am having problems with a function that I have been using in my program
to
read sentences from a ''command file'' and parse them into commands. the
surprising thing is that the program works fine on some computers and not
so
fine on others. I tried debugging and cannot make any sense of it. I
narrowed it down to the seekg function and made this simple program which
(from what I understand) does not seem to be working as expected in all
the
computers I have tried so far.
please let me know if I have got something wrong...

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

int _tmain(int argc, _TCHAR* argv[])
{
ifstream is("test.txt");
char test[256];


test[0] = 0;

>is >test;
cout << test << endl;
is.seekg(-2,ios::cur);


test[0] = 0;

>is >test;
cout << test << endl;
return 0;
}

here are the different versions of test.txt and their corresponding
outputs
in all the cases I expected that the output would be
createModel
el
what am I doing wrong ?

case 1: contents of test.txt :
createModel

OUTPUT:
createModel
del

case 2: contents of test.txt
createModel
testing
/*
*/
OUTPUT:
createModel
eModel

case 3: contents of test.txt :
createModel
testing
/*
*/
CreateElements
196
DMElasticElement
0 195 1
-1
exitCreateElements

OUTPUT:
createModel

thanks
Julian.


Remember to account for the (invisible)
newline chars at the end of each line when
seeking backwards ("\r\n" on Windows, "\n"
on unix/linux, "\r" on Mac(?) ).

If ''test.txt'' has just one line, it must end with a newline;
otherwise the result is not as expected. e.g. for Windows:

createModel\r\n

thanks for the reply... but I''m not sure if that explains everything. even
if I need to account for the invisible characters, the results don''t make
sense... I tried all kinds of combinations.. but it seems like it is seeking
more than 2 offsets although I have just asked to offset 2. if I account for
the invisible characters, it should effectively offset only 1 visible
character... instead it is offsetting 3

another problem: why does the result change with the number of lines in the
file? ... even though I am only reading the first line!
the more number of lines I have, the more it is offsetting.


这篇关于寻求问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆