如何将文件中的单个字符读取到数组中
这里是新手。我需要创建一个程序,从 textfile.txt 读取字符串,然后处理其中的一些字符。
textfile.txt的内容如下。
JA100200300
FE111222333
MA101202303
我需要分别引用“JA”、“100”、“200”和“300”。我设法使用下面的代码来完成此操作,其中我将字符串拆分为 11 个字符并将其放入数组中。当然,此代码仅在您键入字符串时才起作用。我需要一种方法来做同样的事情,但是从文件中获取字符串。
while((a[i++]=getchar()) != '\n' && i < 11);
我该怎么做?我可以读取文件并将每一行字符串放入一个数组中,然后从数组中读取并将每个数组拆分为多个数组吗?这可能吗?或者从文件中读取每个字符并将它们放入类似于 fscanf %d 之类的数组中,但分割每个字符?
Newbie here. I need to create a program that reads strings from a textfile.txt then processes a few characters from it.
The contents of textfile.txt is as below.
JA100200300
FE111222333
MA101202303
I need to reference "JA","100", "200" and "300" separately. I managed to do this with the code below, where I split the string into 11 characters and put it into array. Of course this code works only as you type out the string. I need a way to do the same thing but get the string from a file instead.
while((a[i++]=getchar()) != '\n' && i < 11);
How do I do this? Can I read the file and put each line of string into an array, then read from the array and split each array into multiple arrays? Is that possible? Or read each character from the file and put them into arrays something like fscanf %d
but split each character?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可能想要这样的东西:
You might want something like this: