Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Reading a file with Xcode

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)

Posted on Mar 12, 2006 10:53 PM

Reply
5 replies

Mar 13, 2006 10:40 AM in response to Pierrebr

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.

Mar 14, 2006 9:22 AM in response to Pierrebr

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;
@end


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!

Reading a file with Xcode

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.