0
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [[NSFileManager defaultManager] createFileAtPath:@"E:/NumsToIntFile" contents:nil attributes:nil];

    int num = 0;
    int x = 1;

    printf("\nEnter a number: ");
    scanf("%d", &num);

    while(num >= x)
    {
       NSString *str = [NSString stringWithFormat:@"%d ", x];
       [str writeToFile:"E:/NumsToIntFile" atomically:YES encoding:NSUTF8StringEncoding error:nil];
       x++;
    }

    NSString *contents = [NSString stringWithContentsOfFile:@"E:/NumsToIntFile"];
    NSLog(@"%@",contents);
    [pool release];
    return 0;
} 

What exactly is wrong with this code? Something in line 17 ([str writeToFile:"E:/NumsToIntFile" atomically:YES encoding:NSUTF8StringEncoding error:nil];) creates an error and I don't know how to fix it.

NOTE: This is via Notepad++ on Windows 7

Arnaud
  • 7,259
  • 10
  • 50
  • 71
C.Johns
  • 27
  • 5
  • main.m: In function 'main': main.m:17:5: warning passing arguement 1 of 'writeToFile:atomatically:encoding:error:' from incompatible pointer type [enabled by default] – C.Johns Sep 09 '15 at 02:43

1 Answers1

0

Add an at symbol @ in front of the "E:/NumsToIntFile" argument on line 17.

Danny Daglas
  • 1,501
  • 1
  • 9
  • 9
  • It works but there's one more error: main.m:17:5: warning: passing arguement 4 of 'writeToFile:atomatically:encoding:error:' from incompatible pointer type [enabled by default] main.m:17:5: note: expected 'struct NSError **' but arguement is of type 'id' – C.Johns Sep 09 '15 at 02:52
  • Declare an NSError pointer like this: `NSError *error;` and pass its address as the 4th argument: `[str writeToFile:@"E:/NumsToIntFile" atomically:YES encoding:NSUTF8StringEncoding error:&error];` – Danny Daglas Sep 09 '15 at 02:54
  • `writeToFile:...` does not append to a file. To append, use `NSFileHandle`. See [Appending to the end of a file with nsmutablestring](http://stackoverflow.com/questions/11106584/appending-to-the-end-of-a-file-with-nsmutablestring). – Danny Daglas Sep 09 '15 at 03:17