Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.sys.mac.programmer.help > #135
| From | "kquirici@yahoo.com" <kquirici@yahoo.com> |
|---|---|
| Newsgroups | comp.sys.mac.programmer.help |
| Subject | Re: fopen problem - straight C program on OS Lion, Xcode 4.3.2 - whazzup? |
| Date | 2012-06-17 08:38 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <ee3750bb-dbf1-411e-b772-34be0c45824f@googlegroups.com> (permalink) |
| References | <a05151a5-ea7c-479c-9f08-9e3c147be254@googlegroups.com> <87oboiq8b0.fsf@araminta.anjou.terraraq.org.uk> |
On Sunday, June 17, 2012 11:14:59 AM UTC-4, Richard Kettlewell wrote:
> "kquirici@yahoo.com" <kquirici@yahoo.com> writes:
>
> > A skeleton program which is supposed to open a text file (created by
> > Wrangler so it's unicode but w/ unix lf breaks) and read a line and print it.
> > he file exists in the location as specified in the code,
> > which is below. The build succeeded w/o error. It's prob. some stupid typo
> > but I can't see it.
> >
> > Regards,
> >
> > ken quirici
> >
> > here's the output returned:
> >
> > fopen failed
> > my_ret = -1
> >
> > Here's the code as copied from the Xcode editor.
> >
> > //
> > // main.c
> > // encode
> > //
> > // Created by ken quirici on 6/16/12.
> > // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
> > //
> >
> > #include <stdio.h>
> >
> > int main(int argc, const char * argv[])
> > {
> > char *my_filename;
> > char *my_mode;
> > char next_line[1000000];
> > int my_ret,max_line;
> >
> > my_filename = "/Users/kenquirici/Desktop/journal/journal.txt.save";
> > my_mode = "r";
> > max_line = 1000000;
> > // next_line = "";
> > // my_ret_line = "";
> >
> > FILE *my_file;
> >
> > if((my_file = fopen(my_filename,my_mode)) == NULL)
> > printf("fopen failed\n");
>
> Use perror() instead of printf, so you can see why the file couldn't be
> opened, e.g.:
>
> perror(my_filename);
>
> > else if(fgets(next_line,max_line,my_file) == NULL)
> > printf("fgets returned null\n");
> > else
> > printf("%s%s","next_line: ",next_line);
>
> That's a really bizarre way to use printf. Why not:
>
> printf("next_line: %s", next_line);
>
> > my_ret = fclose(my_file);
>
> Calling fclose() on a null pointer (as you do in the error case) is a
> bug.
>
> > // insert code here...
> > printf("%s%d%s","my_ret = ",my_ret,"\n");
> > return 0;
> > }
>
> --
> http://www.greenend.org.uk/rjk/
ok I'll try perror. Thx for that & the other suggestions.
Regards
ken quirici
Back to comp.sys.mac.programmer.help | Previous | Next — Previous in thread | Next in thread | Find similar
fopen problem - straight C program on OS Lion, Xcode 4.3.2 - whazzup? "kquirici@yahoo.com" <kquirici@yahoo.com> - 2012-06-17 07:55 -0700
Re: fopen problem - straight C program on OS Lion, Xcode 4.3.2 - whazzup? Richard Kettlewell <rjk@greenend.org.uk> - 2012-06-17 16:14 +0100
Re: fopen problem - straight C program on OS Lion, Xcode 4.3.2 - whazzup? "kquirici@yahoo.com" <kquirici@yahoo.com> - 2012-06-17 08:38 -0700
Re: fopen problem - straight C program on OS Lion, Xcode 4.3.2 - whazzup? "kquirici@yahoo.com" <kquirici@yahoo.com> - 2012-06-17 08:54 -0700
Re: fopen problem - straight C program on OS Lion, Xcode 4.3.2 - whazzup? "kquirici@yahoo.com" <kquirici@yahoo.com> - 2012-06-17 08:53 -0700
Re: fopen problem - straight C program on OS Lion, Xcode 4.3.2 - whazzup? Richard Kettlewell <rjk@greenend.org.uk> - 2012-06-17 18:00 +0100
Re: fopen problem - straight C program on OS Lion, Xcode 4.3.2 - whazzup? "kquirici@yahoo.com" <kquirici@yahoo.com> - 2012-06-17 13:12 -0700
Re: fopen problem - straight C program on OS Lion, Xcode 4.3.2 - whazzup? Bob Harris <nospam.News.Bob@remove.Smith-Harris.us> - 2012-06-18 22:30 -0400
csiph-web