如何在C中逐个字符/逐个数字地读取输入?
我有一个文本文件,可能包含数千个数字(0-9 --> 单个数字)/字符,例如: 13612371029301276312357829031029352131265309182765236728726355263789120938728...(像这样继续)
在C中,如何将它们读入数组,以便每个数字单独存储? 我的意思是储存后, 数组[0]=1 数组[1]=3 array[2]=6...等等[每个数字都是单独读取的,这不是一个大数字,而是一组数字,输入时不带任何空格]
我想您现在明白我的意思了...如何如果数字没有分隔符,我要存储它们吗?
这是一个重写:
我有一个包含大量数字的文件:~10^8 位数字,没有任何分隔符:
该文件如下所示: 1273894723974134173984102748123719723987482637182384213894109234092341093294 13413413241341...然后继续
我会喜欢按顺序读取文件 - 逐位读取。我该如何在C中做到这一点?
I have a text file that might contain thousands and thousands of numbers(0-9 --> single digit)/characters, like:
13612371029301276312357829031029352131265309182765236728726355263789120938728...(goes on like this)
In C, how do I read them into an array such that each number gets stored separately?
I mean after storing,
array[0]=1
array[1]=3
array[2]=6... and so on [each number is read individually, this is not a big number but a collection of numbers, entered without any kind of spaces]
I think you get my point by now... How do I store them, if the numbers have no separators??
Here is a rewording:
I have a file that has a very large number of digits in it: ~10^8 digits which do not have any seperators:
the file would look like this: 127389472397413417398410274812371972398748263718238421389410923409234109329413413413241341... and goes on and on
I would like to read the file sequentially - digit by digit. How do I do that in C??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
编辑:输出数字数组((char)0 到 char(9))。
Edit: output array of digits ((char)0 to char(9)).
要一次获取一个字符,请参阅 fgetc。要将大量数字组合成一个大整数,请参见 GMP。 您想要实现的目标到底是什么?!
To get one character at a time, see fgetc. To put a lot of digits together into a single huge integer, see e.g. GMP. What is it exactly, that you DO want to accomplish?!
我假设您不需要字符而是实际值,在这种情况下我会这样做:
享受您的新数组,其中所有数字都作为数值存储在数组中。
I'm assuming you don't want the characters but the real values in which case I would do it like this:
Enjoy your new array with all numbers stored in an array as numerical values.
编辑:你的意思是,“对于如此大的输入,我使用什么函数?”与您用于任何输入的相同。几个答案给了你一些非常好的功能。
fgetc()
读取字符一 -一次从文件句柄 - 将数字(存储为char
)转换为数值的常见技巧是x - '0'
,其中x
是数字字符。malloc()
可以动态地创建一个- 分配您想要的任何大小的数组,但您必须 <完成后,code>free()它。要获取文件大小,请使用stat()
< /a> 在大多数类 Unix 系统上,或者为了更便携的方法,请使用 <代码>fseek()和ftell ()
来找到它。这些都是标准的、相当常见的函数,如果你懂C并且知道这些函数,我不知道你的麻烦是什么。EDIT: What do you mean, "what functions do I use for such large inputs?" The same ones you use for any inputs. Several answers have given you some very nice functions.
fgetc()
reads characters one-at-a-time from a filehandle - the common trick to convert a digit (stored as achar
) to a numeric value isx - '0'
, wherex
is the digit character.malloc()
can make a dynamically-allocated array of whatever size you want for you, but you'll have tofree()
it when you're done. To get the file size, usestat()
on most Unix-like systems, or for a more portable approach usefseek()
andftell()
to find it. These are all standard and fairly common functions, and I don't know what your trouble is if you know C and know these functions.只需将其读取为字符串即可。 C 中的字符串基本上是字符数组。
Just read it as string. Strings in C are basically arrays of characters.
如果你想获取第一个数字的值,你只需执行
int firstNumber = myString[0] - '0' ;
要获得第五个,请执行
int number5 = myString[4] - '0' ;
If you want to get the value of the the first number, you just do
int firstNumber = myString[0] - '0' ;
To get the 5th one you do
int number5 = myString[4] - '0' ;
根据 这篇 文章,您可以通过 malloc 分配非常大的内存。
但是,如果文件确实很大并且您无法分配这么大的内存,则可以简单地使用文件映射 API(如果操作系统是 Windows)。
通过文件映射,您可以将文件映射到内存。在它之后,您只有一个指向文件数据的指针(例如 char*)。
According to This post, you can allocate really big memories by malloc.
But if the file is really huge and you cannot allocate such a big memory, you can just simply use File Mapping APIs if the OS is Windows.
With File Mapping you can just map a file to memory. After it, you just have a pointer (a char* for example) that points to file data.
要创建大尺寸的字符串,您需要创建链接列表。链接列表是一组结构,其中最后一部分是指向另一个结构的指针。这将允许您存储更大的字符串,这些字符串将驻留在内存和硬盘空间中,因此您不会因内存问题而耗尽空间。
To create a string of a large size you need to create a Link List. A link list is a set of structs where the last section is a pointer to another struct. This will allow you to store larger strings that will reside in both memory and hard drive space, so you won't run out of space due to memory problem.