无法使用 fopen 以八度打开"file name.txt"

  • 本文关键字:八度 file txt name fopen octave
  • 更新时间 :
  • 英文 :


如何使用fopen函数在octave中打开文件名为文件名.txt的文件?我正在使用安装在windows上的octave。

谢谢

使用fopen打开文件并获取文件id, "r"表示读取模式

fid = fopen("name.txt", "r");

然后使用fgets逐行检索文件并将其添加到字符串中。一旦文件被完全读取,fgets将返回-1。

file_string = "";
while((line = fgets(fid)) != -1)
  file_string = strcat(file_string, line);
end

读取完成后关闭文件。

fclose(fid);

你有它,你现在应该有一个变量file_string,它包含你的文件的内容作为一个字符串。显然,这些实用程序比我上面描述的要通用得多,所以我还鼓励您阅读以下文档:

https://www.gnu.org/software/octave/doc/interpreter/Opening-and-Closing-Files.html打开和关闭文件

最新更新