本文介绍了从文件中读取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在从一个文件中读取一串数字(使用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调用都有一个访问超限,所以我猜有一个 更好的方式。你能帮帮我吗? 非常感谢, LuTHieRHi,I''m reading a string of numbers from a file (using Borland C++ Builder6), and I''m doing it like this: first I use FileRead to store all thedata in the file to a char* variable (appropriately called ''data'').Then, I read every number usingchar *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 thatthere''s an access overrun in each sscanf call, so I guess there is abetter 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 JohnsonIn C++ you should try avoid using the C I/O functions unless you have agood reason to be using them. sscanf and its variants are notoriouslyeasy to use incorrectly. Here is an example of how to do the same usingC++''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 在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 在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 这篇关于从文件中读取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!