仅加载特定行
Load only a specific line
我有一个数据文件包含超过一百万行,由 16 个整数组成(这并不重要),我需要在 Octave 中处理这些行。显然,加载整个文件是不可能的。我怎样才能只加载特定行?
我想到了两种可能:
- 我在 Simple 的文档中遗漏了一些东西 I/O
- 我应该将文件转换为 CSV 文件并使用一些 csvread 功能
如果要逐行遍历文件,可以打开文件,然后使用 fscanf
解析每一行。
fid = fopen(filename);
while true
% Read the next 16 integers
data = fscanf(fid, '%d', 16);
% Go until we can't read anymore
if isempty(data)
break
end
end
如果您希望每一行都是一个字符串,您可以使用 fgetl
来获取每一行
fid = fopen(filename);
% Get the first line
line = fgetl(fid);
while line
% Do thing
% Get the next line
line = fgetl(fid);
end
我有一个数据文件包含超过一百万行,由 16 个整数组成(这并不重要),我需要在 Octave 中处理这些行。显然,加载整个文件是不可能的。我怎样才能只加载特定行?
我想到了两种可能:
- 我在 Simple 的文档中遗漏了一些东西 I/O
- 我应该将文件转换为 CSV 文件并使用一些 csvread 功能
如果要逐行遍历文件,可以打开文件,然后使用 fscanf
解析每一行。
fid = fopen(filename);
while true
% Read the next 16 integers
data = fscanf(fid, '%d', 16);
% Go until we can't read anymore
if isempty(data)
break
end
end
如果您希望每一行都是一个字符串,您可以使用 fgetl
来获取每一行
fid = fopen(filename);
% Get the first line
line = fgetl(fid);
while line
% Do thing
% Get the next line
line = fgetl(fid);
end