从文件中读取数字 [英] Reading numbers from a file

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

问题描述



我正在从一个文件中读取一串数字(使用Borland C ++ Builder

6),我这样做是这样的:首先我使用FileRead将文件中的所有

数据存储到char *变量(适当地称为''data'')。

然后,我读取每个数字使用


char * ptr;

int value;


[...]


ptr =& data [position];

sscanf(ptr,"%i",& value);

position + = IntToStr(value).Length();

numbers [i] = value;


(当然,所有这些都在循环中)。

这个方法似乎工作正常,但是Borland CodeGuard告诉我

每个sscanf调用都有一个访问超限,所以我猜有一个

更好的方式。你能帮帮我吗?

非常感谢,


LuTHieR

Hi,
I''m reading a string of numbers from a file (using Borland C++ Builder
6), and I''m doing it like this: first I use FileRead to store all the
data in the file to a char* variable (appropriately called ''data'').
Then, I read every number using

char *ptr;
int value;

[...]

ptr = &data[position];
sscanf (ptr, "%i", &value);
position += IntToStr(value).Length();
numbers[i] = value;

(All of it inside of a loop, of course).
This method seems to be working OK, but Borland CodeGuard tells me that
there''s an access overrun in each sscanf call, so I guess there is a
better way of doing it. Could you please help me?
Big thanks,

LuTHieR

推荐答案

LuTHieR写道:
LuTHieR wrote:

我正在从一个文件中读取一串数字(使用Borland C ++ Builder
6),我就是我这样做:首先我使用FileRead将文件中的所有数据存储到char *变量(适当地称为''data'')。
然后,我使用
char * ptr;
int value;

[...]

ptr =& data [position];
sscanf(ptr,"%i"& value);
position + = IntToStr(value).Length();
numbers [i] = value;
(当然所有这些都在循环中)。
这种方法似乎工作正常,但是Borland CodeGuard告诉我,每次sscanf调用都有访问溢出,所以我猜测有更好的方法。你能帮帮我吗?
非常感谢,

LuTHieR
Hi,
I''m reading a string of numbers from a file (using Borland C++ Builder
6), and I''m doing it like this: first I use FileRead to store all the
data in the file to a char* variable (appropriately called ''data'').
Then, I read every number using

char *ptr;
int value;

[...]

ptr = &data[position];
sscanf (ptr, "%i", &value);
position += IntToStr(value).Length();
numbers[i] = value;

(All of it inside of a loop, of course).
This method seems to be working OK, but Borland CodeGuard tells me that
there''s an access overrun in each sscanf call, so I guess there is a
better way of doing it. Could you please help me?
Big thanks,

LuTHieR




在C ++中你应该尽量避免使用CI / O函数除非你有充足的理由使用它们。 sscanf及其变种众所周知

易于使用不正确。这是一个如何使用

C ++的字符串流来做同样的例子。修改你的需求:


#include< iostream>

#include< sstream>


int main()

{

char data [] =" 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47" ;

std :: istringstream ss(数据);

int i;

while(ss>> i)

std :: cout<< i<< std :: endl;

}


-

Alan Johnson



In C++ you should try avoid using the C I/O functions unless you have a
good reason to be using them. sscanf and its variants are notoriously
easy to use incorrectly. Here is an example of how to do the same using
C++''s stringstreams. Modify to your needs:

#include <iostream>
#include <sstream>

int main()
{
char data[] = "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47" ;
std::istringstream ss(data) ;
int i ;
while (ss >> i)
std::cout << i << std::endl ;
}

--
Alan Johnson


谢谢:)

还有一个问题......如果数据在实际数据之前包含一个字符串

(类似data [] =" label 1 2 3 4 5 6 7 8),我怎么能轻松地读取它?b / b
再次感谢


LuTHieR


Alan Johnson ha escrito:
Thanks :)
One more question...if data contains a String before the actual data
(something like data[] = "label 1 2 3 4 5 6 7 8"), how can I easily
read it?
Thanks again

LuTHieR

Alan Johnson ha escrito:
LuTHieR写道:
LuTHieR wrote:

我正在读一串文件中的数字(使用Borland C ++ Builder
6),我这样做:首先我使用FileRead将文件中的所有数据存储到char *变量中(适当地称为''数据'')。
然后,我使用

char * ptr读取每个数字;
int value;

[...]

ptr =& data [position];
sscanf(ptr,"%i",& value);
position + = IntToStr(value).Length( );
数字[i] =价值;

(当然所有这些都在一个循环中。
这个方法似乎工作正常,但是Borland CodeGuard告诉我,每个sscanf调用都有一个访问超限,所以我想那里是一种更好的方法。你能帮帮我吗?
非常感谢,

LuTHieR
Hi,
I''m reading a string of numbers from a file (using Borland C++ Builder
6), and I''m doing it like this: first I use FileRead to store all the
data in the file to a char* variable (appropriately called ''data'').
Then, I read every number using

char *ptr;
int value;

[...]

ptr = &data[position];
sscanf (ptr, "%i", &value);
position += IntToStr(value).Length();
numbers[i] = value;

(All of it inside of a loop, of course).
This method seems to be working OK, but Borland CodeGuard tells me that
there''s an access overrun in each sscanf call, so I guess there is a
better way of doing it. Could you please help me?
Big thanks,

LuTHieR



在C ++中你应该尽量避免使用CI / O函数,除非你有一个
使用它们的充分理由。众所周知,sscanf及其变种很容易错误地使用。这是一个如何使用C ++的stringstreams做同样的例子。修改你的需求:

#include< iostream>
#include< sstream>

int main()
{
char data [] =" 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 ;
std :: istringstream ss(data);
int i;
while(ss>> i)
std :: cout<< i<< std :: endl;
}
-
Alan Johnson



In C++ you should try avoid using the C I/O functions unless you have a
good reason to be using them. sscanf and its variants are notoriously
easy to use incorrectly. Here is an example of how to do the same using
C++''s stringstreams. Modify to your needs:

#include <iostream>
#include <sstream>

int main()
{
char data[] = "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47" ;
std::istringstream ss(data) ;
int i ;
while (ss >> i)
std::cout << i << std::endl ;
}

--
Alan Johnson






Don 打扰回答,就像使用>>一样简单操作符为

字符串类型。

再次感谢


LuTHieR


LuTHieR ha escrito:
Don''t bother answering, it was as simple as using the >> operator to a
string type.
Thanks again

LuTHieR

LuTHieR ha escrito:
谢谢:)
还有一个问题......如果数据在实际数据之前包含一个字符串
(类似data [] ="标签1 2 3 4 5 6 7 8),我怎样才能轻松阅读?
再次感谢

LuTHieR

Alan Johnson ha escrito :
Thanks :)
One more question...if data contains a String before the actual data
(something like data[] = "label 1 2 3 4 5 6 7 8"), how can I easily
read it?
Thanks again

LuTHieR

Alan Johnson ha escrito:
LuTHieR写道:
LuTHieR wrote:
我正在从文件中读取一串数字(使用Borland C ++ Builder
6),我这样做:首先我使用FileRead将文件中的所有数据存储到char *变量(适当地称为''data'')。
然后,我读了每个数字使用

char * ptr;
int value;

[...]

ptr =& data [position];
sscanf(ptr,"%i",& value);
position + = IntToStr(value).Length();
number s [i] = value;

(当然所有这些都在循环中)。
这种方法似乎工作正常,但Borland CodeGuard告诉我
每个sscanf调用都有一个访问超限,所以我猜有更好的方法。你能帮帮我吗?
非常感谢,

LuTHieR
Hi,
I''m reading a string of numbers from a file (using Borland C++ Builder
6), and I''m doing it like this: first I use FileRead to store all the
data in the file to a char* variable (appropriately called ''data'').
Then, I read every number using

char *ptr;
int value;

[...]

ptr = &data[position];
sscanf (ptr, "%i", &value);
position += IntToStr(value).Length();
numbers[i] = value;

(All of it inside of a loop, of course).
This method seems to be working OK, but Borland CodeGuard tells me that
there''s an access overrun in each sscanf call, so I guess there is a
better way of doing it. Could you please help me?
Big thanks,

LuTHieR



在C ++中你应该尽量避免使用CI / O函数,除非你有一个
使用它们的充分理由。众所周知,sscanf及其变种很容易错误地使用。这是一个如何使用C ++的stringstreams做同样的例子。修改你的需求:

#include< iostream>
#include< sstream>

int main()
{
char data [] =" 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 ;
std :: istringstream ss(data);
int i;
while(ss>> i)
std :: cout<< i<< std :: endl;
}
-
Alan Johnson



In C++ you should try avoid using the C I/O functions unless you have a
good reason to be using them. sscanf and its variants are notoriously
easy to use incorrectly. Here is an example of how to do the same using
C++''s stringstreams. Modify to your needs:

#include <iostream>
#include <sstream>

int main()
{
char data[] = "2 3 5 7 11 13 17 19 23 29 31 37 41 43 47" ;
std::istringstream ss(data) ;
int i ;
while (ss >> i)
std::cout << i << std::endl ;
}

--
Alan Johnson






这篇关于从文件中读取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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