在 C 中读取带有整数和字符串的外部文件
我正在尝试读取外部文本文件。该文件包含以下形式的数字和单词:(
hello 1239 4943 melissa
每个元素独占一行)实际文本文件有超过 1200 个单词。我想读取每一行并将它们存储为字符串,但 fscanf 会跳过数字。如何将数字读入程序并将它们存储为字符串?
char word[1263][13];
FILE * fh;
fh=fopen("wordlist.txt","r");
for (a=0;a<1263;a++)
{
fscanf(fh,"%s",word[a]);
}
fclose(fh);
I am trying to read an external text file. The file contains both numbers and words in the form:
hello 1239 4943 melissa
(with each element on its own line) The actual text file has over 1200 words. I want to read each line and store them as strings, but fscanf skips over the numbers. How can I read the numbers into my program and store them as strings?
char word[1263][13];
FILE * fh;
fh=fopen("wordlist.txt","r");
for (a=0;a<1263;a++)
{
fscanf(fh,"%s",word[a]);
}
fclose(fh);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该能够通过 fscanf 实现这一点
You should be able to achieve this via fscanf
您如何使用
fscanf
?以下代码将起作用:How are you using
fscanf
? The following code will work: