0

I used NSInputstream to read from a file. After reading, the NSInputstream contents are empty.I used the code(For transferring a .txt file to ftp server)

- (void)startSend

{ AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];

BOOL                    success;
NSURL *                 url;

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSString *testsessionid=[defaults stringForKey:@"testsessionid"];
NSString *writeFileName=[NSString stringWithFormat:@"%@%@.txt",testsessionid,mainDelegate.studentID];
NSLog(@"Write file name %@",writeFileName);
NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolderPath = [searchPaths objectAtIndex: 0];

NSString *filePath= [documentFolderPath stringByAppendingPathComponent: writeFileName];
NSLog(@"Write folder name %@",filePath);


filePath=@"/Users/sree/Desktop/ARATHY/BCLSTestApp/BCLSTest/Question2.txt";

assert(filePath != nil);
assert([[NSFileManager defaultManager] fileExistsAtPath:filePath]);
assert( [filePath.pathExtension isEqual:@"txt"]  );

assert(self.networkStream == nil);      // don't tap send twice in a row!
assert(self.fileStream == nil);
// First get and check the URL.

url = [NSURL URLWithString:@"ftp://edugame@www.edugame.co/bclstest/243"];
success = (url != nil);

if (success) {
    // Add the last part of the file name to the end of the URL to form the final
    // URL that we're going to put to.

    url = CFBridgingRelease(
                            CFURLCreateCopyAppendingPathComponent(NULL, ( CFURLRef) url, (CFStringRef) [filePath lastPathComponent], false)
                            );
    success = (url != nil);
}

// If the URL is bogus, let the user know.  Otherwise kick off the connection
if ( ! success) {
   NSLog(@"Invalid URL");
} else {

    // Open a stream for the file we're going to send.  We do not open this stream;
    // NSURLConnection will do it for us.

    self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];

 //   self.fileStream=[[NSInputStream alloc] initWithFileAtPath:filePath];
    if(self.fileStream==nil)

        NSLog(@"FILE DOESN'T EXIST");

    else
        NSLog(@"FILE EXISTS");

    assert(self.fileStream != nil);
    BOOL hasbyte=[self.fileStream hasBytesAvailable];

    if (hasbyte==YES)

        NSLog(@"Has contents");

    else
        NSLog(@"no contents");

    [self.fileStream open];
  //  NSLog(@"SIZE OF STREAM IS >> %d",fi);
    // Open a CFFTPStream for the URL.

    self.networkStream = CFBridgingRelease(
                                           CFWriteStreamCreateWithFTPURL(NULL, ( CFURLRef) url)
                                           );
    assert(self.networkStream != nil);

        success = [self.networkStream setProperty:@"edugame" forKey:(id)kCFStreamPropertyFTPUserName];
        assert(success);
        success = [self.networkStream setProperty:@"edu1@Game" forKey:(id)kCFStreamPropertyFTPPassword];
        assert(success);


    self.networkStream.delegate = self;
    [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [self.networkStream open];

    // Tell the UI we're sending.

    //[self sendDidStart];
}

}

It prints, "FILE EXISTS" But in the next line "no contents"

The file is not empty.

ARATHY
  • 349
  • 5
  • 13
  • 29

1 Answers1

2

I just had the same issue, and the answer is simple and silly.

After you create your stream:

self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];

you need to call open on it:

[self.fileStream open];

It makes total sense now that I know what is missing, but I didn't see the call to open in the examples I found of using NSInputStream.

Kelly
  • 1,096
  • 12
  • 22