5 Replies Latest reply: Mar 14, 2006 9:22 AM by Daniel Stein
Pierrebr Level 1 (0 points)
I am new to Xcode. I want to create an application that will read a serie of values x and y and plot it for analysis into a custom view.
The data are taken during a scan and store into an ASCII file. I can transfer the file to my Mac but the problem is.
How do I read my data from a text file using Xcode?
I have try using NSData but I am not going anywhere. The data are into the buffer! How to extract value from the buffer?

PowerBookG4, G5   Mac OS X (10.4.5)  
  • Paul Janssens Level 1 (90 points)
    You can use fopen, fread and fscanf.
  • Daniel Stein Level 4 (1,940 points)
    Even if you use the C library functions referred to above, you may eventually have to encapsulate the data in Cocoa objects in order to plot them in a GUI. If you go that way, some of what follows may help you with that as well.

    If you are using Cocoa, as indicated by your reference to NSData, let me advise you to try NSString methods.

    First of all, since you have an ASCII file, you can import the entire contents with the NSString class method stringWithContentsOfFile:

    The argument for this method is another NSString representing the file system path to the file. NSString has lots of good methods for dealing with file system paths.

    Once you have the data represented as an NSString object, you can use other NSString methods to parse the file contents into an array of individual NSString objects using such methods as componentsSeparatedByString which allow you to specify a separator character such as <tab> or some other character as the separator.

    I hope this helps you get started.

    Certainly NSData is a way to go, but NSString methods are very powerful. Investigate the properties of this important Cocoa Foundation framework class.
  • Pierrebr Level 1 (0 points)
    It work with fopen and fscanf.
    Thank, Pierre
  • Pierrebr Level 1 (0 points)
    This is a good idea, now I can see at least where to start with my object.
    The other method works but I was not able to figure out how to transform the data into an object for the display.
    Thank you,
  • Daniel Stein Level 4 (1,940 points)
    Here's the interface for a very simple graphical object:

    @interface PlotPoint: NSObject
    NSRect rect;
    NSColor *color;

    - ( id ) initWithRect: ( NSRect ) aRect;
    - ( void ) setColor: ( NSColor * ) aColor;
    - ( NSColor * ) color;
    - ( NSRect ) rect;
    - ( void ) plot;


    The class has an initializer, some accessors, and a plot method for actually drawing the object on-screen. You could start with something as simple as:

    - ( void ) plot
    [ color set ];
    NSRectFill( rect );
    //NSBezierPath *path = [ NSBezierPath bezierPathWithOvalInRect: rect ];
    //[ path stroke ];

    The NSBezierPath is just a suggestion in case you want to make a more complicated symbol. Just assemble the pieces of a path and draw it. Or if you make a closed-path, you could [ path fill ] it. If you go that way, you will probably want to make one of the instance variables "origin" instead of "rect".

    In the above example, the rect ivar contains the location on screen and the size for the plot point object. That is the information you get from your ASCII file, which you would use when initializing the PlotPoint object.

    The basic idea is to make a list of objects you want to draw, say, in a mutable array, and then in the drawRect method for your custom view, enumerate the list and have each object draw itself

    while ( obj = [ enumerator nextObject ] ) [ obj plot ];

    If the object is a PlotPoint object (or even a subclass of it!), this should go off without a hitch. Good luck with your plotting program!