Why a data will be destroyed when running on device but not on simulator?

Kei Minagawa

When running my program on a iOS simulator I didn't have a problem. But when running on my iPhone5c, I have a problem. The problem is data will be destroyed when the data was loaded. Here is my program source code. Where my code is wrong?

overview of my program:

1.load data from "sample.txt"
2.log the data

AppDelegate.h:

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (assign) unsigned char* bytePtr;
@property (strong, nonatomic) NSData* data;
@end

AppDelegate.m:

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    [self load];

    return YES;
}

- (void) load
{
    NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"txt"];
    self.data = [NSData dataWithContentsOfFile:path];
    self.bytePtr = (unsigned char *)[self.data bytes];
    NSLog(@"%s", self.bytePtr);
}
~snip~

sample.txt:

abcdefghijklmnopqrstuvwxyz

output:

abcdefghijklmnopqrstuvwxyz
roj

expected output:

abcdefghijklmnopqrstuvwxyz
Rob Napier
NSString* path = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"txt"];
self.data = [NSData dataWithContentsOfFile:path];
self.bytePtr = (unsigned char *)[self.data bytes];
NSLog(@"%s", self.bytePtr);

You're accessing data as though it is a NULL-terminated cstring (using %s). Unless your file ends in a \0 (which is doesn't appear to), your NSLog will just keep reading data until it finds one.

If you want to read a string from a file, using stringWithContentsOfURL:encoding:error:.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get the Device model name when running on Simulator?

From Dev

XCode 5.1.1 freezes when running app on device, works on simulator

From Dev

Why is iOS Simulator not running?

From Dev

Error: couldn't get default input device... when running on simulator

From Dev

Setting the frames per second to 30 when running a sprite-kit project on a device instead of the simulator

From Dev

Setting the frames per second to 30 when running a sprite-kit project on a device instead of the simulator

From Dev

Error: couldn't get default input device... when running on simulator

From Dev

Running code on iOS device instead of simulator

From Dev

Alignment issue when running simulator

From Dev

(Phonegap + iOS) Why when I get the full path of the filesystem in a device or simulator I get only "/"?

From Dev

(Phonegap + iOS) Why when I get the full path of the filesystem in a device or simulator I get only "/"?

From Dev

Why is activity A started when activity B is destroyed

From Dev

Why Does it Matter if An Object is Nil When Destroyed?

From Dev

Why is activity A started when activity B is destroyed

From Dev

Why Does it Matter if An Object is Nil When Destroyed?

From Dev

Interpreting a backtrace when crashing on the device but not the simulator

From Dev

iPad Device Orientation Problems when Testing on Simulator

From Dev

Font is not recognized when executed in SIMULATOR and in DEVICE

From Dev

Font is not recognized when executed in SIMULATOR and in DEVICE

From Dev

Swift App crashes when closed in device/Simulator?

From Dev

Core data not working on device but fine on simulator

From Dev

UITableView data displays in simulator, but not on actual device

From Dev

Error when call API when running simulator

From Dev

Changing storyboard segue `kind` between "Show" and "ShowDetail": why does the change not take effect when running the simulator?

From Dev

Python dice simulator, why it is running without stopping?

From Dev

Unable to determine if the Xamarin Forms application is running in the Simulator or on the Device

From Dev

How to enter a value while running UIAutomation Test on iOS Simulator or Device

From Dev

What architecture should it be my .a for running on iOS Device and iOS Simulator

From Dev

Why does an empty cell in UITableView has grey background on device but not on simulator?

Related Related

  1. 1

    Get the Device model name when running on Simulator?

  2. 2

    XCode 5.1.1 freezes when running app on device, works on simulator

  3. 3

    Why is iOS Simulator not running?

  4. 4

    Error: couldn't get default input device... when running on simulator

  5. 5

    Setting the frames per second to 30 when running a sprite-kit project on a device instead of the simulator

  6. 6

    Setting the frames per second to 30 when running a sprite-kit project on a device instead of the simulator

  7. 7

    Error: couldn't get default input device... when running on simulator

  8. 8

    Running code on iOS device instead of simulator

  9. 9

    Alignment issue when running simulator

  10. 10

    (Phonegap + iOS) Why when I get the full path of the filesystem in a device or simulator I get only "/"?

  11. 11

    (Phonegap + iOS) Why when I get the full path of the filesystem in a device or simulator I get only "/"?

  12. 12

    Why is activity A started when activity B is destroyed

  13. 13

    Why Does it Matter if An Object is Nil When Destroyed?

  14. 14

    Why is activity A started when activity B is destroyed

  15. 15

    Why Does it Matter if An Object is Nil When Destroyed?

  16. 16

    Interpreting a backtrace when crashing on the device but not the simulator

  17. 17

    iPad Device Orientation Problems when Testing on Simulator

  18. 18

    Font is not recognized when executed in SIMULATOR and in DEVICE

  19. 19

    Font is not recognized when executed in SIMULATOR and in DEVICE

  20. 20

    Swift App crashes when closed in device/Simulator?

  21. 21

    Core data not working on device but fine on simulator

  22. 22

    UITableView data displays in simulator, but not on actual device

  23. 23

    Error when call API when running simulator

  24. 24

    Changing storyboard segue `kind` between "Show" and "ShowDetail": why does the change not take effect when running the simulator?

  25. 25

    Python dice simulator, why it is running without stopping?

  26. 26

    Unable to determine if the Xamarin Forms application is running in the Simulator or on the Device

  27. 27

    How to enter a value while running UIAutomation Test on iOS Simulator or Device

  28. 28

    What architecture should it be my .a for running on iOS Device and iOS Simulator

  29. 29

    Why does an empty cell in UITableView has grey background on device but not on simulator?

HotTag

Archive