reading from .txt file into an array or vector

boyfarrell

Registered
Hi,

I have data file that are simple columns of numbers, how can I read these into my cocoa programme? Using code similar to this,
Code:
[NSFileManager contentsAtPath: @"vector.txt"]
?
 

HateEternal

Mac Metal Head
I have never done any Cocoa programming, but there are a few different ways you could do it in most programming languages.

1. If you know the column sizes (start pos and length) you could manualy read in each column manually, this would be a little more work but it would be the most accurate, given a few exceptions.

2. You could read in a whole line and use a split function to separate all the columns out into an array. This only works if there is a space or some delimiting character in between each column, which isn't allways the case with flat files.

Let me know if you want any more clarification. There are probably some other ways to do it, these are the ways I have seen used most often.
 

boyfarrell

Registered
What I'm actually doing is reading in x-y pairs of data. The data represents the spectra emitted by the sun as seen from the surface of the earth. I have attached a sample spectrum.

The data set can be either together as in the attached file or one column in seperate files it doesn't matter.

The data sets in reality are really very long, we are taking 10,000 different numbers! So entering them manually isn't an option. However, I will always know the length of the file, that is to say I will always know the number of elements in the array. I will also know how the columns are delimited (if in the same file). So I think option two sounds good.

Do you know how to do that in normal C? It doesn't have to be in cocoa. I just though it might be easier in cocoa that's all.

Thanks, Daniel.
 

Attachments

kainjow

Registered
well you'd start by reading the data into a string:
Code:
NSString *textFile = [NSString stringWithContentsOfFile:@"/path/to/Spectra.txt"];
Then (for the simplest route) you would split that up by lines:
Code:
NSArray *lines = [textFiles componentsSeparatedByString:@"\r"]; // or \n
Then you would enumerate through each line like so:
Code:
NSEnumerator *lineEnum = [lines objectEnumerator];
NSString *line;
while (line = [lineEnum nextObject])
{
     // work with line
}
Then in each iteration of the loop you would parse the x and y values, maybe using something like this (untested):
Code:
int x, y;
sscanf([line UTF8String], "%d\t%d", &x, &y);
// do something with x and y now
 

boyfarrell

Registered
Hi,

I have used the above code, at it build just fine. However, as I don't actually understand what the lines actually mean I don't know how to go from what is above to actually having an array of number that I can do something with. I normally like figuring things out for myself but I could do with some spoon feeding here because I really stuck - and getting a bit frustrated, surely this should be really easy! :mad:

Maybe it would be better to do it with fopen and other C stdio.h commands? As I have more understanding here?
 

kainjow

Registered
What are you trying to do? Build an array? If you want an array of X and Y values in Cocoa, you'd want an NSMutableArray to hold them. But you'd want a way of holding the X and Y values together, so possibly through a class (Coordinates) that has private member variables to contain the X and Y values.
 

boyfarrell

Registered
I am trying to build an array. I want an different array for every column I have in my .txt file 'spectra'.

I think what I'll do is write this in go old C. Then I can always convert it into a method for an Objective-C instance.

Yes, I have heard of NSMutableArray. But my problem is more that i don't understand the basic tools in the foundation.h so I can't do this myself in cocoa just yet.

I would have thought this is something quite a lot of the researcher types that frequent this place might have done before. Oh well not to worry, I'll post the code when I have figured it out!

Cheers for your help.
 
Top