如何使用sscanf或fscanf从文件中读取字符串? [英] How to read strings from file with sscanf or fscanf?

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

问题描述

因此,当然,我现在遇到了一些麻烦.我正在尝试读取一个按列顺序排列的文本文件.我想做的是将数字,字符和字符串列分别存储在数组中.

So, of course, I'm having a little trouble right now. I'm trying to read a text file that goes something like this in a columnar order. What I would like to do is store the number, character and string columns seperately in arrays.

[Numbers] [Characters] [Strings] 

现在,尽管我已经弄清楚了如何将数字和字符列读入它们自己的数组中,但是我似乎无法使用字符串列来读入.至少不是我要使用的命令fscanfsscanf.

Now, while I have figured out how to read the number and character columns into their own arrays, I cannot seem to do so with the string column. At least, not with fscanf or sscanf, which are the commands I want to use.

如何读取使用fscanfsscanf进行组织的文件? (我了解textscan,我想知道是否可以使用fscanf或sscanf).

How can you read a file organized as such using fscanf or sscanf? (I know about textscan, I want to know if this is possible with fscanf or sscanf).

好的,让我在这里添加一些代码.因此,当我进行研究时,我尝试了以下方法:

Okay let me add a little code here. So while I was researching this I tried out the following:

fid = fopen('Data.txt', 'w+'); 
i = 1;
while ~feof(fid)
     line = fgets(fid);
     M(i) = sscanf(line, '%d, %c, %s', [3,inf];
     i = i+1; 
end

这会运行,但是M最终只会以行向量的形式出现,该行向量由数据文件中的第一列数字组成.它只是完全忽略了字符和字符串的存在.

This runs, but M ends up coming out only as a row vector consisting of the first column of numbers in the data file. It just completely ignores the existence of chars and strings.

现在,为了更好地理解sscanf函数,我尝试了以下

Now, to get a better understanding of the sscanf function I tried the following

fid = fopen('Data.txt', 'w+'); 
    i = 1;
    while ~feof(fid)
         line = fgets(fid);
         M(i) = sscanf(line, '%d, %d, %d', [3,inf];
         i = i+1; 
    end

对于仅由数字列组成的数据样本集.顺便说一句,这与以前完全一样.它只是读取数据的第一个数字列并退出.因此,基本上,我什至不知道如何正确使用sscanf,feof或fgets.所以我也可以在这里使用一些帮助.

For a sample set of data consisting of just columns of numbers. This, incidentally, does exactly the same thing as previously; it just reads the first number column of the data and quits. So, I don't even know how to use sscanf, feof, or fgets properly, basically. So I could also use some help here as well.

我知道尝试仅读取数字列对于fscanf来说是微不足道的,但是我试图在这里理解sscanf和fgets.

And I know trying to read just columns of numbers is trivial with fscanf, but I'm trying to understand sscanf and fgets here.

推荐答案

您正在寻找的功能应该是

The function you are looking for would rather be textscan

它将允许您一次读取整个文件,并指定要读取的每一列的格式.

It will allow you to read the whole file at once and to specify the format of each column you are reading.

您的情况应该是这样的:

In your case, it should be something like :

fileID = fopen('myfile.txt');
C = textscan(fileID, '%f %c %s');
fclose(fileID);
celldisp(C)

由于我们不确定您的文本文件的格式,因此您可能需要调整Format字符串.

As we don't know exactly the format of your text file, you'll probably have to tweak the Format string.

您将获得一个单元格数组,其中包含根据格式规范分析的文件内容.

You will get a cell array with the content of the file parsed according to the format specification.

Matlab的文档详细说明了可能性,格式规范,并提供了很好的示例.

Matlab's documentation explain in details the possibilities, the format specification, and comes with good examples.

这篇关于如何使用sscanf或fscanf从文件中读取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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