Reading float from a custom file - objective-c

I want to read some float value one by one from a custom file I defined "player.geo".
player.geo is a file I created using Xcode 4 ("Empty File" from the File > New menu)
I'm currently trying to do it like this:
- (id) initWithGeometryFile:(NSString *) nameOfFile
{
NSFileHandle *geoFile = NULL;
NSString *geoFilePath = [[NSBundle mainBundle] pathForResource:#"player" ofType:#"geo"];
geoFile = [NSFileHandle fileHandleForReadingAtPath:geoFilePath];
if(geoFile == NULL)
{
NSLog(#"Failed to open file.");
}
else
{
NSLog(#"Opening %# successful", nameOfFile);
NSMutableData *fileData = [[NSMutableData alloc] initWithData:[geoFile readDataOfLength:4]];
float firstValue;
[fileData getBytes:&firstValue length:sizeof(float)];
NSLog(#"First value in file %# is %f", nameOfFile, firstValue);
}
return self;
}
I'm not getting the expected value of -64.0, rather I'm getting 0.0.
Is this the right way to go about it?
Do I really have to read the file as a string and then parse float the string contents to get the float value?

NSData objects deal with raw bytes, not strings. If you are typing in a string into a txt file, this will not work. If you are using NSData objects, then you will need to first write the data using the data object methods such as writeToFile:atomically:.
Alternately, you can use the NSString functions stringWithContentsOfFile and componentsSeperatedByString to generate an NSArray containing each string on it's own line, like so:
NSString *tmp;
NSArray *lines;
lines = [[NSString stringWithContentsOfFile:#"testFileReadLines.txt"]
componentsSeparatedByString:#"\n"];
NSEnumerator *nse = [lines objectEnumerator];
while(tmp = [nse nextObject]) {
NSLog(#"%#", tmp);
}

Related

Text encoding error when opening Notification Center plist using stringWithContentsOfFile:encoding:error:

I want to write a function that finds out whether "Do Not Disturb" is turned on, on a Mac.
So I have written this function:
-(BOOL)dndIsOn{
NSString* path = [[NSString stringWithFormat:#"~/Library/Preferences/ByHost/com.apple.notificationcenterui.%#.plist",[self getSystemUUID]] stringByExpandingTildeInPath];
NSError *error = nil;
NSString *txtFileContents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
NSLog(#"string:%# error:%#",txtFileContents,error);
if ([txtFileContents rangeOfString:#"<fals"].location == NSNotFound) {
return false;
}else{
return true;
}
}
But the txtFileContents string error returns:
.... .plist” couldn’t be opened using text encoding Unicode (UTF-8)."
Why is this happening?
You can read a .plist which top level is a dictionary using dictionaryWithContentsOfFile:. In case the top level is a array, you have to use arrayWithContentsOfURL:. This will automatically parse the file.
In your case, the top level is a dictionary.
The key is doNotDisturb.
The value you want to return:
BOOL doNotDisturbSet = [[NSDictionary dictionaryWithContentsOfFile:path][#"doNotDisturb"]] boolValue];

Objective C - Create text file to read and write line by line in Cocoa

I building a Mac app,I have 2 problem:
I want to create a text file to read and write data on it. I don't know how to crate a text file to read and write data. Is it use
struct?
I want to create a XML file to read and write data on it. Can I create a struct for XML?
Do you have suggestion? Thanks in advance
Well, to create a file, just use
[[NSFileManager defaultManager] createFileAtPath:#"Your/Path" contents:nil attributes:nil];
This creates an empty file, which you can write to or read from. To write text (or XML), just use NSString's writeToFile:atomically:encoding:error: method like this
NSString *str = //Your text or XML
[str writeToFile:"Your/Path" atomically:YES encoding:NSUTF8StringEncoding error:nil];
To read from a file, just make an NSString with the contents of that file
NSString *contents = [NSString stringWithContentsOfFile:#"Your/Path"];
or, if it does not contain a string, get an NSData object from the file
NSData *contents = [NSData dataWithContentsOfFile:#"Your/Path"];
/**************************main.m******************************
NS FILE HANDLE READ & WRITE
reading and writing in same file
Created by abdulsathar on 6/16/14.
***************************************************************/
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool //ARC
{
NSFileHandle *file;
//object for File Handle
NSError *error;
//crearing error object for string with file contents format
NSMutableData *writingdatatofile;
//create mutable object for ns data
NSString *filePath=[NSString stringWithFormat:#"/Users/chandrakumar/Documents/abdul/doc.txt"];
//telling about File Path for Reading for easy of access
file = [NSFileHandle fileHandleForReadingAtPath:#"/Users/chandrakumar/Documents/abdul/doc.txt"];
//assign file path directory
if (file == nil) //check file exist or not
NSLog(#"Failed to open file");
NSString *getfileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
//access file contents with out ns handle method
if (error) //check error flag for file present or not
NSLog(#"Error reading file: %#", error.localizedDescription);
NSLog(#"contents: %#", getfileContents);
//display file contents in main file
NSArray *listArray = [getfileContents componentsSeparatedByString:#"\n"];
//caluculate list of line present in files
NSLog(#"items = %ld", [listArray count]);
const char *writingchar = "how are you";
writingdatatofile = [NSMutableData dataWithBytes:writingchar length:strlen(writingchar)];
//convert string format into ns mutable data format
file = [NSFileHandle fileHandleForUpdatingAtPath: #"/Users/chandrakumar/Documents/abdul/new.txt"];
//set writing path to file
if (file == nil) //check file present or not in file
NSLog(#"Failed to open file");
[file seekToFileOffset: 6];
//object pointer initialy points the offset as 6 position in file
[file writeData: writingdatatofile];
//writing data to new file
[file closeFile];
//close the file
}
return 0;`enter code here`
}
/***********************************OUTPUT********************************************
2014-06-17 14:55:39.695 storage[4075:303] contents: hello how are you my dearservice
*************************************************************************************/

Unable to write NSArray to file

I am using google contact data objective c APIs for fetching contacts. I got contacts array from google server now i want to write contact to file. i am using writeToFile:atomically: method for writing array to file but This method is not working for me since i feel that output array from gdata API not contain property list objects. Please suggest any alternate solution.
-(void)fetchData{
GDataServiceGoogleContact *service=[[GDataServiceGoogleContact alloc] init];
[service setShouldCacheResponseData:YES];
[service setServiceShouldFollowNextLinks:YES];
[service setUserCredentialsWithUsername:[mUsername stringValue] password:[mPassword stringValue]];
// GENERATING THE URL
NSURL *feedURL=[GDataServiceGoogleContact contactFeedURLForUserID:kGDataServiceDefaultUser];
GDataQuery *contQuery=[GDataQueryContact contactQueryWithFeedURL:feedURL];
[contQuery setShouldShowDeleted:YES];
[contQuery setMaxResults:2000];
GDataServiceTicket *ticket=[service fetchFeedWithQuery:contQuery delegate:self didFinishSelector:#selector(hasFetchedContacts:feed:error:)];
}
-(void) hasFetchedContacts:(GDataServiceTicket*) ticket feed:(GDataFeedContact*) contacts error:(NSError*) err
{
NSArray *contactList=[contacts entries];
NSLog(#"%d",[list writeToFile:#"/Users/subhranil/Desktop/contactList" atomically:NO]);
}
Wrap it up to NSData with:
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:contactList];
Then save NSData to file with:
[data writeToFile:#"/Users/subhranil/Desktop/contactList" atomically:NO];
You can later restore the data back to NSArray using:
NSData *data = [NSData dataWithContentsOfFile: #"yourFilePath"];
NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data]
Just make sure that objects inside your NSArray conform to NSCoding.
You can use byte array for this purpose and NSData for writing to file.
For saving:
NSData *data=[[NSData alloc] initWithBytes:[contacts entries] length:total];
[data writeToFile:#"path" atomically:YES];
total= The total size of the array in bytes
For retrieving:
NSData *newdata = [NSData dataWithContentsOfFile:#"path"];
NSUInteger len = [newdata length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [newdata bytes], len);
byteData will now contain an array of GDataEntryContact objects and you can use them accordingly.
You can encode/decode GDataObject using an xml as generator.
Encode:
[entry setNamespaces:[entry completeNamespaces]];
NSString *xml = [[entry XMLElement] XMLString];
if (nil != xml)
{
//Store your xml NSString to a file
}
Decode:
NSString *xml = //Read your XML String from file;
NSXMLElement *xmlElement = [[NSXMLElement alloc] initWithXMLString:xml error: &error];
if (!error) {
return [[GDataEntryDocBase alloc] initWithXMLElement:xmlElement parent: nil];
}

How to Find & Replace text in a file using Objective C?

I am new to Xcode and was wondering if anyone could help me with this.
I need to make an application that is able to open a file and replace its contents.
E.g. (in psuedo code)
Replace("String1", "String2", "~/Desktop/Sample.txt")
Please let me know if I'm not clear enough.
Thanks in advance.
use stringByReplacingOccurrencesOfString:withString: method which will find all occurrences of one NSString and replace them, returning a new autoreleased NSString.
NSString *source = #"The rain in Spain";
NSString *copy = [source stringByReplacingOccurrencesOfString:#"ain"
withString:#"oof"];
NSLog(#"copy = %#", copy);
// prints "copy = The roof in Spoof"
Edit
to set the file content in your string (be careful , this is not conveniant if your file is a bit large) , replace occurences then copy to a new file :
// Instantiate an NSString which describes the filesystem location of
// the file we will be reading.
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Sample.txt"];
NSError *anError;
NSString *aString = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:&anError];
// If the file read was unsuccessful, display the error description.
// Otherwise, copy the string to your file.
if (!aString) {
NSLog(#"%#", [anError localizedDescription]);
} else {
//replace string1 occurences by string2
NSString *replacedString = [aString stringByReplacingOccurrencesOfString:#"String1"
withString:#"String2"];
//copy replacedString to sample.txt
NSString * stringFilepath = #"ReplacedSample.txt";
[replacedString writeToFile:stringFilepath atomically:YES encoding:NSWindowsCP1250StringEncoding error:error];
}
You probably want this
And regarding your question about how to read the text from a file to a NSString:
NSError * error;
NSString * stringFromFile;
NSString * stringFilepath = #"loadfile.txt";
stringFromFile = [[NSString alloc] initWithContentsOfFile:stringFilepath
encoding:NSWindowsCP1250StringEncoding
error:&error];
And for writing to a file:
(using the same NSString from loading: stringFromFile)
NSError * error;
NSString * stringFilepath = #"savefile.txt";
[stringFromFile writeToFile:stringFilepath atomically:YES encoding:NSWindowsCP1250StringEncoding error:error];
Note that in this example i use an encoding for windows (this means it uses charcters \n\r at the end of each line). Check the documentation for other types of encoding.
(See NSString documentation)
For Xcode 4, open the file you want to search, then click Edit > Find > Find and Replace, or the keyboard shortcut Command + Option + f.

Null output troubles

Okay I've been trying at this for about 2-3 hours now and I don't seem to quite get it. Here is the code and a brief explanation:
I'm trying to make two lists of words, pull one word from each of those lists at random, and display both words (along with a third) on the screen when a button is pressed. Here is the code:
#import "Project001ViewController.h"
#implementation Project001ViewController
-(ArrayOfWords *) advs
{
if(!advs){
advs = [[ArrayOfWords alloc] init];
NSString* advpath = #"/WordLists/adverbs.txt";
NSLog(#"1");
[[self advs] populateListOfWords:advpath];
}
return advs;
}
-(ArrayOfWords *) adjs
{
if (!adjs) {
adjs = [[ArrayOfWords alloc] init];
NSString* adjpath = #"/WordLists/adjectives.txt";
[[self adjs] populateListOfWords:adjpath];
NSLog(#"2");
}
return adjs;
}
- (IBAction)generate:(UIButton *)sender;
{
//int randy = arc4random() % 11;
//NSNumber* num= [NSNumber numberWithInteger:randy];
NSString* obj = #"app";
NSString* adverb = [[self advs] randomItem];
NSString* adjective = [[self adjs] randomItem];
NSLog(#"%i %i",[adjs size],[advs size]);
NSLog(#"1 %# %# %#.",adverb, adjective, obj);
//NSLog(#"%#",thePhrase);
[display setText:#"Hi"];
}
#end
I'm having trouble on the last NSLog line:
NSString* obj = #"app";
NSString* adverb = [[self advs] randomItem];
NSString* adjective = [[self adjs] randomItem];
NSLog(#"%i %i",[adjs size],[advs size]);
NSLog(#"1 %# %# %#.",adverb, adjective, obj);
Instead of getting the two randomly selected words (using arc4random() to produce them) the array returns Null. But I know FOR CERTAIN. That the array's are not empty because the NSLog Line where I print [adjs size] and [advs size] I get the correct sizes of the list of words. I just want to know what is causing them to print Null here.
populateListOfWords, randomItem, and size methods:
- (NSArray *) populateListOfWords:(NSString *) path {
//gets the components of the file into an NSString
NSString *wordListString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//returns an array of all the words (uses the next line indicator '\n' to know when it's at the end of the word
NSArray* words = [wordListString componentsSeparatedByString:#"\n"];
length=(NSNumber*)([words count]);
return words;
}
-(NSString*) randomItem{
//returns random object in list
return (NSString*)[list objectAtIndex:(arc4random() % (int)length)] ;
}
-(int) size{
//returns size of list
return (int)length;
}
(If more code is needed let me know and thank you in advanced for any and all help).
I believe there is a problem with the paths. It is impossible to have access to the path /WordLists/adjectives.txt in iOS due to the application sandbox. I suggest you add these files to the application by dragging and dropping them onto the project. You can get the file paths for resources in application bundle using
NSString * path = [[NSBundle mainBundle] pathForResource:#"adjectives" ofType:#"txt"];
Now pass this path to the method populateListOfWords:.
Because of the incorrect path, I believe wordListString is nil and everything else follows to be that.
Another thing is that int and NSNumber are not toll free bridged like NSStrings and other foundation objects. So
length=(NSNumber*)([words count]);
is incorrect. I suggest you define length as int or better NSUInteger to match the type count method returns.
This method is the problem:
- (NSArray *) populateListOfWords:(NSString *) path {
//gets the components of the file into an NSString
NSString *wordListString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//returns an array of all the words (uses the next line indicator '\n' to know when it's at the end of the word
NSArray* words = [wordListString componentsSeparatedByString:#"\n"];
length=(NSNumber*)([words count]);
return words;
}
It wasn't actually putting the words in a list that anyone else could access. I had to just modify it like so:
- (void) populateListOfWords:(NSString *) path {
//gets the components of the file into an NSString
NSString *wordListString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//returns an array of all the words (uses the next line indicator '\n' to know when it's at the end of the word
NSArray* words = [wordListString componentsSeparatedByString:#"\n"];
list = words;
length=(int)([words count]);
}
Now it gives me the correct output. But for some reason when I press the button twice it crashes. Oh well that's a new problem. Thanks again for all the help.
UPDATE
Turns out advs and adjs were being released so the second go around it was trying to access a nil value because when I call [self advs] [self adjs] the pointers exist, but their contents do not. I had to go back and refill them each time basically removing the if (!advs) and if (adjs) parts. It now works as intended.