【问题标题】:Read multiple files in Vala在 Vala 中读取多个文件
【发布时间】:2016-03-24 16:02:33
【问题描述】:

我正在寻找一种在 Vala 中读取多个文件的方法。 互联网上有一个关于读取文件夹中的所有文件并按类型对它们进行排序的示例,但我无法从中做出任何事情,但仍在尝试。我在说:http://www.valadoc.org/#!api=glib-2.0/GLib.Dirhttps://wiki.gnome.org/Projects/Vala/GIOSamples,主要是。

在我的文件夹中,我有一堆文件和我的 vala 程序。我需要读取该文件夹中具有特定文件扩展名的所有文件。例如读取所有 .txt 文件,直到该文件夹​​中没有更多 .txt 文件为止。

非常感谢!

【问题讨论】:

    标签: file directory vala


    【解决方案1】:

    我会这样做......

    void main( )
    {
      string dir = ".";
      Dir d;
      try
      {
        d = Dir.open( dir );
      }
      catch ( FileError e )
      {
        stderr.printf( "Could not open %s! %s", dir, e.message );
        return;
      }
      unowned string? name;
      while ( ( name = d.read_name( ) ) != null )
      {
        string path = Path.build_filename( dir, name );
        if ( name.down( ).has_suffix( ".txt" ) && FileUtils.test( path, FileTest.IS_REGULAR ) )
        {
          FileStream? f = FileStream.open( path, "r" );
          if ( f == null )
          {
            stderr.printf( "Error opening %s for reading! %d: %s\n", path, GLib.errno, GLib.strerror( GLib.errno ) );
            return;
          }
          /* Read contents from f... */
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      相关资源
      最近更新 更多