views:

451

answers:

4

I just started working with C & Xcode and I've run into a little difficulty.

All I want to do is read a file from the command line and see the output in the terminal. I think my problem lies with the path to the file that I want to read in. I'm using a Mac and the file is on my desktop, so the path should be Users/myName/Desktop/words.txt. Is this correct?

This is my code:

#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
A: 

Close... it's

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

... where {Volume} is the name of your hard drive. You can also try using:

~/Desktop/words.txt

... where ~ is understood to mean "your home directory", but this might not resolve correctly.

fbrereto
+2  A: 

The path to the desktop is /Users/[username]/Desktop/

~/Desktop/ is a user-agnostic way of denoting this, ~ represents the current users home directory. It must be expanded using a method like stringByExpandingTildeInPath

Not sure about using C# (I've never used it on Mac OS X), but in Objective-C/Cocoa, you would do..

// 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);

This is the most robust way of getting the Desktop path, as a user could technically move their Desktop folder else where (although this is pretty unlikely). Another valid option is to use the NSString method stringByExpandingTildeInPath:

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

As I said, both those are in Objective-C, but it shouldn't be hard to translate to C#, assuming you can get at the Cocoa libraries.


The code you posted works correctly:

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

Your terminal automatically expands the ~/ tilda path

dbr
A: 

(Note - this appears to be a C question, not a C# question)

Actually, you can do:

/Users/myName/Desktop/words.txt

You don't have to give the path to the volume.

However, to get the full path in C you'd do something like:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *home, *fullPath;

home = getenv("HOME");

fullPath = strcat(home, "/Desktop/words.txt");

The issue you're running into with passing the filename as an argument is that you need to set the current working directory to the place where the file exists.

Wade Williams
+1  A: 

If you need the path to a file in OS X, an easy way to get it is to just drag the file into the Terminal.app window where you are typing the command. Voila!

Stephen Canon
Great tip what!!