Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 2 > Newsgroup comp.lang.objective-c

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-06-2011, 02:55 PM
eddie
Guest
 
Posts: n/a
Default Asking for guidance on converting c strings to objects

Right now I'm feeling pretty stupid... This code should be simple but I seem to have missed a gross lot on my reading... I'm just trying to read a file and convert each line to an object... any help? I am new to this..

#import "CocoaGuessAppDelegate.h"
#include <stdio.h>
@implementation CocoaGuessAppDelegate
@synthesize window;

- (void)applicationDidFinishLaunchingNSNotificatio n *)aNotification {
FILE *f1;
int i=51;
int x=0;
NSMutableArray *words = [[NSMutableArray alloc] initWithCapacity:50];
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:51];
char line[10];
f1=fopen("/myfile.txt","r");
while(fgets(line,50,f1))
{
printf("%s",line);
[words addObject:[NSMutableString string]];
[string getCString:line maxLength:i encoding:NSUTF8StringEncoding];
NSLog(@"string: %@",string);
[words insertObject:string atIndex: x];
printf("%d\n",x);
NSLog(@"array%@\n",words);
x++;
}
NSLog(@"Done");
fclose(f1);
}

@end

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 02-06-2011, 03:46 PM
Frédéric Testuz
Guest
 
Posts: n/a
Default Re: Asking for guidance on converting c strings to objects

eddie <zaphod69@gmail.com> wrote:

> Right now I'm feeling pretty stupid... This code should be simple but I
> seem to have missed a gross lot on my reading... I'm just trying to read a
> file and convert each line to an object... any help? I am new to this..
>
> #import "CocoaGuessAppDelegate.h"
> #include <stdio.h>
> @implementation CocoaGuessAppDelegate
> @synthesize window;
>
> - (void)applicationDidFinishLaunchingNSNotificatio n *)aNotification {
> FILE *f1;
> int i=51;
> int x=0;
> NSMutableArray *words = [[NSMutableArray alloc] initWithCapacity:50];
> NSMutableString *string = [[NSMutableString alloc]

initWithCapacity:51];
> char line[10]; // Here you have 10 char and

below you allow fgets to read up to 50 chars !!!!
> f1=fopen("/myfile.txt","r");
> while(fgets(line,50,f1))
> {
> printf("%s",line);
> [words addObject:[NSMutableString string]]; // Here you

are adding an empty mutable string to words
> [string getCString:line maxLength:i

encoding:NSUTF8StringEncoding]; // Here you are replacing the content of
line with the content of string (which is always empty in your case).
> NSLog(@"string: %@",string);
> [words insertObject:string atIndex: x]; // Here you

are adding string to words, but string is still empty. You never change
the value of string since it's initialization.
> printf("%d\n",x);
> NSLog(@"array%@\n",words);
> x++;
> }
> NSLog(@"Done");
> fclose(f1);
> }
>
> @end
>


I think you want something like:

- (void)applicationDidFinishLaunchingNSNotificatio n *)aNotification {
FILE *f1;
int i=51;
int x=0;
NSMutableArray *words = [[NSMutableArray alloc] init];
char line[50];
f1=fopen("/myfile.txt","r");
while(fgets(line,50,f1))
{
printf("%s",line);
[words addObject:[NSString stringWithBytes:line
length:50 encoding:NSUTF8StringEncoding]]; // You have to adapt
this line for the case when you are at the end of the file (line will
not contain 50 valid char).
printf("%d\n",x);
NSLog(@"array%@\n",words); // ! the words array will be
logged n times.
x++;
}
NSLog(@"Done");
fclose(f1);
}

BUT, you are not checking if f1 is valid. I dont know the format of the
file you are reading, but I doubt that every word will be exactly 50
chars.

--
Frédéric Testuz
Reply With Quote
  #3 (permalink)  
Old 02-06-2011, 09:50 PM
Tom Harrington
Guest
 
Posts: n/a
Default Re: Asking for guidance on converting c strings to objects

In article
<8780a5fa-5a23-4dd9-88bb-36d83a246737@glegroupsg2000goo.googlegroups.com
>,

eddie <zaphod69@gmail.com> wrote:

> Right now I'm feeling pretty stupid... This code should be simple but I seem
> to have missed a gross lot on my reading... I'm just trying to read a file
> and convert each line to an object... any help? I am new to this..
>
> #import "CocoaGuessAppDelegate.h"
> #include <stdio.h>
> @implementation CocoaGuessAppDelegate
> @synthesize window;
>
> - (void)applicationDidFinishLaunchingNSNotificatio n *)aNotification {
> FILE *f1;
> int i=51;
> int x=0;
> NSMutableArray *words = [[NSMutableArray alloc] initWithCapacity:50];
> NSMutableString *string = [[NSMutableString alloc] initWithCapacity:51];


The above line creates an empty mutable string object with the capacity
initially set to 51.

> char line[10];
> f1=fopen("/myfile.txt","r");
> while(fgets(line,50,f1))
> {
> printf("%s",line);
> [words addObject:[NSMutableString string]];


Here you add an empty mutable string to your array.

> [string getCString:line maxLength:i encoding:NSUTF8StringEncoding];


This takes your empty mutable string "string" and tries to read up to 51
characters from it into "line". But since "string" is empty there's
nothing to read. I think you may have gotten the documentation on this
method backwards, because this reads from "string" into "line" and I
think you want the reverse.

> NSLog(@"string: %@",string);


String is still empty-- you've never put anything into it.

> [words insertObject:string atIndex: x];


Now you add "string" to the array. It's the same object at every pass
through the loop. It's also still empty.

Also, FWIW, you're adding two objects to the array at every pass through
the loop.

> printf("%d\n",x);
> NSLog(@"array%@\n",words);
> x++;
> }
> NSLog(@"Done");
> fclose(f1);
> }
>
> @end
>


I'd suggest something more like this (warning, typed into this window
and not tested):

NSString *string;

while (fgets(line, 50, f1)) {
string = [NSString stringWithUTF8String:line];
[words addObject:string];
}

That's based on believing the fgets() man page when it says that the
resulting string will be NULL-terminated. With out the terminator,
stringWithUTF8String will have problems.

If your file isn't enormous, and if your goal is that each line becomes
one entry in the array, you could also do this:

NSError *fileReadError = nil;
NSString *fileContents = [NSString
stringWithContentsOfFile:@"/myfile.txt"
encoding:NSUTF8StringEncoding
error:&fileReadError];
NSArray *words = [fileContents componentsSeparatedByString:@"\n"];

--
Tom "Tom" Harrington
Independent Mac OS X developer since 2002
http://www.atomicbird.com/
Reply With Quote
  #4 (permalink)  
Old 02-07-2011, 12:06 AM
eddie
Guest
 
Posts: n/a
Default Re: Asking for guidance on converting c strings to objects

On Feb 6, 6:50*pm, Tom Harrington <t...@pcisys.no.spam.dammit.net>
wrote:
> In article
> <8780a5fa-5a23-4dd9-88bb-36d83a246...@glegroupsg2000goo.googlegroups.com
>
>
>
>
>
> >,

> *eddie <zapho...@gmail.com> wrote:
> > Right now I'm feeling pretty stupid... This code should be simple but Iseem
> > to have missed a gross lot on my reading... I'm just trying to read a file
> > and convert each line to an object... any help? I am new to this..

>
> > #import "CocoaGuessAppDelegate.h"
> > #include <stdio.h>
> > @implementation CocoaGuessAppDelegate
> > @synthesize window;

>
> > - (void)applicationDidFinishLaunchingNSNotificatio n *)aNotification {
> > * *FILE *f1;
> > * *int i=51;
> > * *int x=0;
> > * *NSMutableArray *words = [[NSMutableArray alloc] initWithCapacity:50];
> > * *NSMutableString *string = [[NSMutableString alloc] initWithCapacity:51];

>
> The above line creates an empty mutable string object with the capacity
> initially set to 51.
>
> > * *char line[10];
> > * *f1=fopen("/myfile.txt","r");
> > * *while(fgets(line,50,f1))
> > * *{
> > * * * * * *printf("%s",line);
> > * * * * * *[words addObject:[NSMutableString string]];

>
> Here you add an empty mutable string to your array.
>
> > * * * * * *[string getCString:line maxLength:i encoding:NSUTF8StringEncoding];

>
> This takes your empty mutable string "string" and tries to read up to 51
> characters from it into "line". But since "string" is empty there's
> nothing to read. *I think you may have gotten the documentation on this
> method backwards, because this reads from "string" into "line" and I
> think you want the reverse.
>
> > * * * * * *NSLog(@"string: %@",string);

>
> String is still empty-- you've never put anything into it.
>
> > * * * * * *[words insertObject:string atIndex: x];

>
> Now you add "string" to the array. It's the same object at every pass
> through the loop. It's also still empty.
>
> Also, FWIW, you're adding two objects to the array at every pass through
> the loop.
>
> > * * * * * *printf("%d\n",x);
> > * * * * * *NSLog(@"array%@\n",words);
> > * * * * * *x++;
> > * *}
> > * *NSLog(@"Done");
> > * *fclose(f1);
> > }

>
> > @end

>
> I'd suggest something more like this (warning, typed into this window
> and not tested):
>
> NSString *string;
>
> while (fgets(line, 50, f1)) {
> * *string = [NSString stringWithUTF8String:line];
> * *[words addObject:string];
>
> }
>
> That's based on believing the fgets() man page when it says that the
> resulting string will be NULL-terminated. With out the terminator,
> stringWithUTF8String will have problems.
>
> If your file isn't enormous, and if your goal is that each line becomes
> one entry in the array, you could also do this:
>
> NSError *fileReadError = nil;
> NSString *fileContents = [NSString
> * *stringWithContentsOfFile:@"/myfile.txt"
> * *encoding:NSUTF8StringEncoding
> * *error:&fileReadError];
> NSArray *words = [fileContents componentsSeparatedByString:@"\n"];
>
> --
> Tom "Tom" Harrington
> Independent Mac OS X developer since 2002http://www.atomicbird.com/


Thanks both to Frédéric and Tom "Tom"...

This is both a learning experience and an experiment. I am using a
big file called corncob.txt (600 mb) of all words in the english
language. The purpose of the program is to suggest words to the user
if he knows part of the word but does not how to type it... as as
example if the user types "prec" the program will suggest "deprecated"
or "appreciation".... Can be done in C, but I want to learn how to do
it with Objective-C. I edited the code I posted for the sake of
simplicity.

Both of you are right. I am new to Objective-C but I am of the
opinion that elegant code always works better than the mess I posted.

Both solutions are great!

Frédéric's solution involves adding the EDFramework, and I did, but
Xcode 3.2.5 seems to have some issues linking external Frameworks. I
am working on that as it looks like a very useful framework.

Tom, you were right! I indeed got the idea backwards(as usual).
Experience beats any book.

Aaron Hillegass book is great but there is no way one could really
learn without guys like you...

Thanks!!!



Reply With Quote
  #5 (permalink)  
Old 02-07-2011, 07:18 PM
eddie
Guest
 
Posts: n/a
Default Re: Asking for guidance on converting c strings to objects

On Feb 6, 9:06*pm, eddie <zapho...@gmail.com> wrote:
> On Feb 6, 6:50*pm, Tom Harrington <t...@pcisys.no.spam.dammit.net>
> wrote:
>
>
>
>
>
> > In article
> > <8780a5fa-5a23-4dd9-88bb-36d83a246...@glegroupsg2000goo.googlegroups.com

>
> > >,

> > *eddie <zapho...@gmail.com> wrote:
> > > Right now I'm feeling pretty stupid... This code should be simple butI seem
> > > to have missed a gross lot on my reading... I'm just trying to read afile
> > > and convert each line to an object... any help? I am new to this..

>
> > > #import "CocoaGuessAppDelegate.h"
> > > #include <stdio.h>
> > > @implementation CocoaGuessAppDelegate
> > > @synthesize window;

>
> > > - (void)applicationDidFinishLaunchingNSNotificatio n *)aNotification{
> > > * *FILE *f1;
> > > * *int i=51;
> > > * *int x=0;
> > > * *NSMutableArray *words = [[NSMutableArray alloc] initWithCapacity:50];
> > > * *NSMutableString *string = [[NSMutableString alloc] initWithCapacity:51];

>
> > The above line creates an empty mutable string object with the capacity
> > initially set to 51.

>
> > > * *char line[10];
> > > * *f1=fopen("/myfile.txt","r");
> > > * *while(fgets(line,50,f1))
> > > * *{
> > > * * * * * *printf("%s",line);
> > > * * * * * *[words addObject:[NSMutableString string]];

>
> > Here you add an empty mutable string to your array.

>
> > > * * * * * *[string getCString:line maxLength:i encoding:NSUTF8StringEncoding];

>
> > This takes your empty mutable string "string" and tries to read up to 51
> > characters from it into "line". But since "string" is empty there's
> > nothing to read. *I think you may have gotten the documentation on this
> > method backwards, because this reads from "string" into "line" and I
> > think you want the reverse.

>
> > > * * * * * *NSLog(@"string: %@",string);

>
> > String is still empty-- you've never put anything into it.

>
> > > * * * * * *[words insertObject:string atIndex: x];

>
> > Now you add "string" to the array. It's the same object at every pass
> > through the loop. It's also still empty.

>
> > Also, FWIW, you're adding two objects to the array at every pass through
> > the loop.

>
> > > * * * * * *printf("%d\n",x);
> > > * * * * * *NSLog(@"array%@\n",words);
> > > * * * * * *x++;
> > > * *}
> > > * *NSLog(@"Done");
> > > * *fclose(f1);
> > > }

>
> > > @end

>
> > I'd suggest something more like this (warning, typed into this window
> > and not tested):

>
> > NSString *string;

>
> > while (fgets(line, 50, f1)) {
> > * *string = [NSString stringWithUTF8String:line];
> > * *[words addObject:string];

>
> > }

>
> > That's based on believing the fgets() man page when it says that the
> > resulting string will be NULL-terminated. With out the terminator,
> > stringWithUTF8String will have problems.

>
> > If your file isn't enormous, and if your goal is that each line becomes
> > one entry in the array, you could also do this:

>
> > NSError *fileReadError = nil;
> > NSString *fileContents = [NSString
> > * *stringWithContentsOfFile:@"/myfile.txt"
> > * *encoding:NSUTF8StringEncoding
> > * *error:&fileReadError];
> > NSArray *words = [fileContents componentsSeparatedByString:@"\n"];

>
> > --
> > Tom "Tom" Harrington
> > Independent Mac OS X developer since 2002http://www.atomicbird.com/

>
> Thanks both to Frédéric and Tom "Tom"...
>
> This is both a learning experience and an experiment. *I am using a
> big file called corncob.txt (600 mb) of all words in the english
> language. The purpose of the program is to suggest words to the user
> if he knows part of the word but does not how to type it... as as
> example if the user types "prec" the program will suggest "deprecated"
> or "appreciation".... Can be done in C, but I want to learn how to do
> it with Objective-C. I edited the code I posted for the sake of
> simplicity.
>
> Both of you are right. *I am new to Objective-C but I am of the
> opinion that elegant code always works better than the mess I posted.
>
> Both solutions are great!
>
> Frédéric's solution involves adding the EDFramework, and I did, but
> Xcode 3.2.5 seems to have some issues linking external Frameworks. I
> am working on that as it looks like a very useful framework.
>
> Tom, you were right! I indeed got the idea backwards(as usual).
> Experience beats any book.
>
> Aaron Hillegass book is great but there is no way one could really
> learn without guys like you...
>
> Thanks!!!


I Think I almost got it (1 bug left) , but still can't use the
EDCommon Frameqork

#import "CocoaGuessAppDelegate.h"
#include <stdio.h>

@implementation CocoaGuessAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunchingNSNotificatio n *)aNotification
{
FILE *f1;

int x=0;
NSMutableArray *words = [[NSMutableArray alloc] init];
NSMutableString *string = [[NSMutableString alloc] init];

char line[50];
f1=fopen("/corncob2.txt","r");
while(fgets(line,50,f1))
{
string = [NSMutableString stringWithUTF8String:line];
[words insertObject:string atIndex:x];
[string release];
string = nil;
x++;
}
NSLog(@"Done");
NSLog(@"Words: %@\n", words);
fclose(f1);
}

@end


Thanks for your Help!!!!
Reply With Quote
  #6 (permalink)  
Old 02-07-2011, 09:07 PM
Tom Harrington
Guest
 
Posts: n/a
Default Re: Asking for guidance on converting c strings to objects

In article
<e4623648-3b42-4db4-85e1-3efa105032f7@x5g2000prf.googlegroups.com>,
eddie <zaphod69@gmail.com> wrote:

> I Think I almost got it (1 bug left) , but still can't use the
> EDCommon Frameqork


There are a still a few problems-- I'll try and explain.

> #import "CocoaGuessAppDelegate.h"
> #include <stdio.h>
>
> @implementation CocoaGuessAppDelegate
>
> @synthesize window;
>
> - (void)applicationDidFinishLaunchingNSNotificatio n *)aNotification
> {
> FILE *f1;
>
> int x=0;
> NSMutableArray *words = [[NSMutableArray alloc] init];
> NSMutableString *string = [[NSMutableString alloc] init];


That initialization above is unnecessary and is a memory leak. You
should just declare string and leave it at that. The reason for this
is...

> char line[50];
> f1=fopen("/corncob2.txt","r");
> while(fgets(line,50,f1))
> {
> string = [NSMutableString stringWithUTF8String:line];


....that this line overwrites anything previously saved in "string", so
the initialized object from above is just lost.

You also don't ever use methods declared in NSMutableString, so "string"
could just be an NSString.

> [words insertObject:string atIndex:x];


Also, "x" is an unnecessary variable. Just do:

[words addObject:string];

Which will just add to the end of the array.

> [string release];


This will crash, because "stringWithUTF8String" above is an
auto-released object. Just delete this line. Or, replace the line above
with

string = [[NSString alloc] initWithUTF8String:line];

Either way will avoid the crash. Using initWithUTF8String/release will
use less memory than using stringWithUTF8String because you won't be
filling up the autorelease pool.

> string = nil;


This line serves no purpose-- it really doesn't matter if you nil out
this variable.

> x++;
> }
> NSLog(@"Done");
> NSLog(@"Words: %@\n", words);
> fclose(f1);
> }
>
> @end


--
Tom "Tom" Harrington
Independent Mac OS X developer since 2002
http://www.atomicbird.com/
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 10:53 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.