【问题标题】:Xcode & passing command line argumentsXcode & 传递命令行参数
【发布时间】:2010-05-25 00:49:01
【问题描述】:

我刚开始使用 C 和 Xcode,遇到了一些困难。

我要做的就是从命令行读取文件并在终端中查看输出。 我认为我的问题在于我要读取的文件的路径。我使用的是 Mac,文件在我的桌面上,所以路径应该是 Users/myName/Desktop/words.txt。这是正确的吗?

这是我的代码:

#import <Foundation/Foundation.h>

int main (int argc, const char* argv[]){

    if(argc == 1){
        NSLog(@" you must pass at least one arguement");
        return 1;
    }
    NSLog(@"russ");
    FILE*  wordFile = fopen(argv[1] , "r");
    char word[100];

    while (fgets(word,100,wordFile)) {

        NSLog(@" %s is %d chars long", word,strlen(word));

    }

    fclose(wordFile);
    return 0;

}//main

【问题讨论】:

    标签: c macos command-line-arguments


    【解决方案1】:

    如果您需要 OS X 中文件的路径,获取它的简单方法是将文件拖到您正在键入命令的 Terminal.app 窗口中。瞧!

    【讨论】:

      【解决方案2】:

      桌面的路径是/Users/[username]/Desktop/

      ~/Desktop/ 是一种与用户无关的表示方式,~ 表示当前用户的主目录。必须使用stringByExpandingTildeInPath之类的方法来扩展它

      不确定是否使用 C#(我从未在 Mac OS X 上使用过),但在 Objective-C/Cocoa 中,你会这样做..

      // Get array with first index being path to desktop
      NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
      
      // Get the first element
      NSString *desktopPath = [paths objectAtIndex:0];
      
      // Append words.txt to path
      NSString *theFilePath = [desktopPath stringByAppendingPathComponent:@"words.txt"];
      
      NSLog(@"%@", theFilePath);
      

      这是获取桌面路径的最可靠方法,因为用户可以从技术上将其桌面文件夹移动到其他位置(尽管这不太可能)。另一个有效的选项是使用 NSString 方法stringByExpandingTildeInPath

      NSString *desktop = [@"~/Desktop" stringByExpandingTildeInPath];
      NSString *theFile = [desktop stringByAppendingPathComponent:@"words.txt"]
      

      正如我所说,这两个都在 Objective-C 中,但如果您可以访问 Cocoa 库,那么转换为 C# 应该不难。


      您发布的代码可以正常工作:

      dbr:.../build/Debug $ ./yourcode ~/Desktop/words.txt 
      yourcode[2106:903] russ
      yourcode[2106:903]  this is words.txt is 17 chars long
      

      您的终端会自动扩展~/ tilda 路径

      【讨论】:

        【解决方案3】:

        关闭...它是

        /{Volume}/Users/myName/Desktop/words.txt

        ... 其中 {Volume} 是您的硬盘驱动器的名称。您也可以尝试使用:

        ~/Desktop/words.txt

        ... 其中~ 被理解为“您的主目录”,但这可能无法正确解析。

        【讨论】:

        • 这并没有解决我的问题(任何一个建议)我使用:/Macintosh HD/Users/russ/Desktop/words.txt & ~/Desktop/words.txt
        【解决方案4】:

        (注意 - 这似乎是 C 问题,而不是 C# 问题)

        其实你可以这样做:

        /Users/myName/Desktop/words.txt

        您不必提供卷的路径。

        但是,要在 C 中获取完整路径,您需要执行以下操作:

        #include <stdlib.h>
        #include <stdio.h>
        #include <string.h>
        char *home, *fullPath;
        
        home = getenv("HOME");
        
        fullPath = strcat(home, "/Desktop/words.txt");
        

        将文件名作为参数传递时遇到的问题是,您需要将当前工作目录设置为文件所在的位置。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-03-25
          • 1970-01-01
          • 2020-08-24
          • 2018-04-07
          • 1970-01-01
          • 2022-10-02
          • 1970-01-01
          • 2010-11-11
          相关资源
          最近更新 更多