Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.sys.mac.programmer.help > #137
| 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:53 -0700 |
| Organization | http://groups.google.com |
| Message-ID | <16694ec6-4819-4355-a10d-df1e7df3e337@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/
well that's weird. when I replaced the printf(....my_ret...)
with perror(my_filename)
it now works - it returns the first line of the file whose name is
my_filename. That is, fopen is no longer returning null, apparently.
I tried running it again just now exactly as before and again it worked.
I'm going to see if I can read the whole file in.
I am very confused. I can only think that some previous run left the file
open, so when I tried to fopen it it failed because it was already open.
But that run where the fopen failed, the fclose must have worked (because it
was already open). That make sense?
Regards,
Ken Quirici
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/
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