error

Discussion in 'C Programming' started by Bill Cunningham, Aug 30, 2012.

  1. I'm hoping that someone can point out the error I'm getting here and seg
    fault. I find it very hard to read code posted by these newsreaders. I'm
    using OE. I hope someone can read this code.

    Bill
    #include <stdio.h>
    #include <errno.h>
    #include <string.h>

    int main()
    {
    errno=0;
    size_t nread, nwrite, nbytes;
    nread = nwrite = nbytes = 0;
    char buf[9000] = { 0 };
    FILE *fp, *fp2;
    if ((fp = fopen("6", "rb")) == NULL)
    perror("fopen 1");
    if ((fp2 = fopen("a", "wb")) == NULL)
    perror("fopen 2");
    do {
    nread = fread(buf, sizeof(buf), 1, fp);
    nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    } while (!feof(fp));
    fclose(fp);
    fclose(fp2);
    printf("%d %d\n", nread, nwrite);
    printf("%s\n",strerror(errno));
    return 0;
    }
     
    Bill Cunningham, Aug 30, 2012
    #1
    1. Advertising

  2. Bill Cunningham

    Lanarcam Guest

    Le 30/08/2012 21:47, Bill Cunningham a écrit :
    > I'm hoping that someone can point out the error I'm getting here and seg
    > fault. I find it very hard to read code posted by these newsreaders. I'm
    > using OE. I hope someone can read this code.
    >
    > Bill
    > #include <stdio.h>
    > #include <errno.h>
    > #include <string.h>
    >
    > int main()
    > {
    > errno=0;
    > size_t nread, nwrite, nbytes;
    > nread = nwrite = nbytes = 0;
    > char buf[9000] = { 0 };
    > FILE *fp, *fp2;
    > if ((fp = fopen("6", "rb")) == NULL)
    > perror("fopen 1");
    > if ((fp2 = fopen("a", "wb")) == NULL)
    > perror("fopen 2");
    > do {
    > nread = fread(buf, sizeof(buf), 1, fp);
    > nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    > } while (!feof(fp));
    > fclose(fp);
    > fclose(fp2);
    > printf("%d %d\n", nread, nwrite);
    > printf("%s\n",strerror(errno));
    > return 0;
    > }
    >
    >

    Not sure, but you use fclose(fp) outside of the
    block where fopen() succeeds.

    if ((fp = fopen("6", "rb")) == NULL)
    {
    perror("fopen 1");
    }
    else
    {
    if ((fp2 = fopen("a", "wb")) == NULL)
    perror("fopen 2");
    else
    {
    do
    {
    nread = fread(buf, sizeof(buf), 1, fp);
    nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    } while (!feof(fp));

    fclose(fp2);
    }
    fclose(fp);
    }
     
    Lanarcam, Aug 30, 2012
    #2
    1. Advertising

  3. Lanarcam wrote:

    > Not sure, but you use fclose(fp) outside of the
    > block where fopen() succeeds.
    >
    > if ((fp = fopen("6", "rb")) == NULL)
    > {
    > perror("fopen 1");
    > }
    > else
    > {
    > if ((fp2 = fopen("a", "wb")) == NULL)
    > perror("fopen 2");
    > else
    > {
    > do
    > {
    > nread = fread(buf, sizeof(buf), 1, fp);
    > nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    > } while (!feof(fp));
    >
    > fclose(fp2);
    > }
    > fclose(fp);
    > }


    Ok I think I see what your getting at. The ifs should have bigger blocks
    instead of the one statement perrors.

    Bill
     
    Bill Cunningham, Aug 30, 2012
    #3
  4. Bill Cunningham

    Lanarcam Guest

    Le 30/08/2012 22:05, Bill Cunningham a écrit :
    > Lanarcam wrote:
    >
    >> Not sure, but you use fclose(fp) outside of the
    >> block where fopen() succeeds.
    >>
    >> if ((fp = fopen("6", "rb")) == NULL)
    >> {
    >> perror("fopen 1");
    >> }
    >> else
    >> {
    >> if ((fp2 = fopen("a", "wb")) == NULL)
    >> perror("fopen 2");
    >> else
    >> {
    >> do
    >> {
    >> nread = fread(buf, sizeof(buf), 1, fp);
    >> nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    >> } while (!feof(fp));
    >>
    >> fclose(fp2);
    >> }
    >> fclose(fp);
    >> }

    >
    > Ok I think I see what your getting at. The ifs should have bigger blocks
    > instead of the one statement perrors.
    >

    Yes, in short:

    if (fp = fopen() != NULL)
    {
    // do something

    fclose(fp);
    }
     
    Lanarcam, Aug 30, 2012
    #4
  5. In article <k1ofvu$g2o$>,
    "Bill Cunningham" <> wrote:

    > I'm hoping that someone can point out the error I'm getting here and seg


    Why don't you point out the error you're getting here, then someone can
    help to explain what is causing the error. Or are we to just guess what
    error you are getting?

    > fault. I find it very hard to read code posted by these newsreaders. I'm
    > using OE. I hope someone can read this code.
    >
    > Bill
    > #include <stdio.h>
    > #include <errno.h>
    > #include <string.h>
    >
    > int main()
    > {
    > errno=0;
    > size_t nread, nwrite, nbytes;
    > nread = nwrite = nbytes = 0;
    > char buf[9000] = { 0 };


    Your problem could be here ^

    > FILE *fp, *fp2;
    > if ((fp = fopen("6", "rb")) == NULL)
    > perror("fopen 1");
    > if ((fp2 = fopen("a", "wb")) == NULL)
    > perror("fopen 2");
    > do {
    > nread = fread(buf, sizeof(buf), 1, fp);
    > nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    > } while (!feof(fp));
    > fclose(fp);
    > fclose(fp2);
    > printf("%d %d\n", nread, nwrite);


    Or it could be here ^

    > printf("%s\n",strerror(errno));
    > return 0;
    > }
     
    Mark Storkamp, Aug 30, 2012
    #5
  6. Bill Cunningham

    Casey Carter Guest

    On 2012-08-30 14:47, Bill Cunningham wrote:
    > int main()
    > {

    [snip]
    > nread = fread(buf, sizeof(buf), 1, fp);


    nread = fread(buf, 1, sizeof(buf), fp);

    > nwrite = fwrite(buf, sizeof(buf), nread, fp2);


    nwrite = fwrite(buf, 1, nread, fp2);
     
    Casey Carter, Aug 30, 2012
    #6
  7. Mark Storkamp wrote:
    > In article <k1ofvu$g2o$>,
    > "Bill Cunningham" <> wrote:
    >
    >> I'm hoping that someone can point out the error I'm getting here
    >> and seg

    >
    > Why don't you point out the error you're getting here, then someone
    > can help to explain what is causing the error. Or are we to just
    > guess what error you are getting?


    Well in the code I compile
    fopen 2:text file busy

    >> fault. I find it very hard to read code posted by these newsreaders.
    >> I'm using OE. I hope someone can read this code.
    >>
    >> Bill
    >> #include <stdio.h>
    >> #include <errno.h>
    >> #include <string.h>
    >>
    >> int main()
    >> {
    >> errno=0;
    >> size_t nread, nwrite, nbytes;
    >> nread = nwrite = nbytes = 0;
    >> char buf[9000] = { 0 };

    >
    > Your problem could be here ^
    >
    >> FILE *fp, *fp2;
    >> if ((fp = fopen("6", "rb")) == NULL)
    >> perror("fopen 1");
    >> if ((fp2 = fopen("a", "wb")) == NULL)
    >> perror("fopen 2");
    >> do {
    >> nread = fread(buf, sizeof(buf), 1, fp);
    >> nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    >> } while (!feof(fp));
    >> fclose(fp);
    >> fclose(fp2);
    >> printf("%d %d\n", nread, nwrite);

    >
    > Or it could be here ^
    >
    >> printf("%s\n",strerror(errno));
    >> return 0;
    >> }
     
    Bill Cunningham, Aug 30, 2012
    #7
  8. Bill Cunningham wrote:
    > Mark Storkamp wrote:
    >> In article <k1ofvu$g2o$>,
    >> "Bill Cunningham" <> wrote:
    >>
    >>> I'm hoping that someone can point out the error I'm getting here
    >>> and seg

    >>
    >> Why don't you point out the error you're getting here, then someone
    >> can help to explain what is causing the error. Or are we to just
    >> guess what error you are getting?

    >
    > Well in the code I compile
    > fopen 2:text file busy
    >
    >>> fault. I find it very hard to read code posted by these newsreaders.
    >>> I'm using OE. I hope someone can read this code.
    >>>
    >>> Bill
    >>> #include <stdio.h>
    >>> #include <errno.h>
    >>> #include <string.h>
    >>>
    >>> int main()
    >>> {
    >>> errno=0;
    >>> size_t nread, nwrite, nbytes;
    >>> nread = nwrite = nbytes = 0;
    >>> char buf[9000] = { 0 };

    >>
    >> Your problem could be here ^
    >>
    >>> FILE *fp, *fp2;
    >>> if ((fp = fopen("6", "rb")) == NULL)
    >>> perror("fopen 1");
    >>> if ((fp2 = fopen("a", "wb")) == NULL)
    >>> perror("fopen 2");
    >>> do {
    >>> nread = fread(buf, sizeof(buf), 1, fp);
    >>> nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    >>> } while (!feof(fp));
    >>> fclose(fp);
    >>> fclose(fp2);
    >>> printf("%d %d\n", nread, nwrite);

    >>
    >> Or it could be here ^
    >>
    >>> printf("%s\n",strerror(errno));
    >>> return 0;
    >>> }


    Wait a minute ! wait! Silly me. I have a file areadly called "a" and
    need to set the w to w+. Let me try that.

    Bill
     
    Bill Cunningham, Aug 30, 2012
    #8
  9. Lanarcam <> writes:
    [...]
    > Yes, in short:
    >
    > if (fp = fopen() != NULL)
    > {
    > // do something
    >
    > fclose(fp);
    > }


    You need parentheses:

    if ((fp = fopen()) != NULL)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 30, 2012
    #9
  10. Casey Carter wrote:
    > On 2012-08-30 14:47, Bill Cunningham wrote:
    >> int main()
    >> {

    > [snip]
    >> nread = fread(buf, sizeof(buf), 1, fp);

    >
    > nread = fread(buf, 1, sizeof(buf), fp);
    >
    >> nwrite = fwrite(buf, sizeof(buf), nread, fp2);

    >
    > nwrite = fwrite(buf, 1, nread, fp2);


    You're right. That was part of the problem. Have I been reading this
    functions parameters backward. Why number of items 1 in fread?

    Bill
     
    Bill Cunningham, Aug 30, 2012
    #10
  11. Mark Storkamp wrote:
    > In article <k1ofvu$g2o$>,
    > "Bill Cunningham" <> wrote:
    >
    >> I'm hoping that someone can point out the error I'm getting here
    >> and seg

    >
    > Why don't you point out the error you're getting here, then someone
    > can help to explain what is causing the error. Or are we to just
    > guess what error you are getting?
    >
    >> fault. I find it very hard to read code posted by these newsreaders.
    >> I'm using OE. I hope someone can read this code.
    >>
    >> Bill
    >> #include <stdio.h>
    >> #include <errno.h>
    >> #include <string.h>
    >>
    >> int main()
    >> {
    >> errno=0;
    >> size_t nread, nwrite, nbytes;
    >> nread = nwrite = nbytes = 0;
    >> char buf[9000] = { 0 };

    >
    > Your problem could be here ^


    Yep! Yep! These cannnot be initialized. The compiler will complain that
    nread and nwrite aren't declared for some reason.

    >> FILE *fp, *fp2;
    >> if ((fp = fopen("6", "rb")) == NULL)
    >> perror("fopen 1");
    >> if ((fp2 = fopen("a", "wb")) == NULL)
    >> perror("fopen 2");
    >> do {
    >> nread = fread(buf, sizeof(buf), 1, fp);
    >> nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    >> } while (!feof(fp));
    >> fclose(fp);
    >> fclose(fp2);
    >> printf("%d %d\n", nread, nwrite);

    >
    > Or it could be here ^
    >
    >> printf("%s\n",strerror(errno));
    >> return 0;
    >> }
     
    Bill Cunningham, Aug 30, 2012
    #11
  12. "Bill Cunningham" <> writes:
    > I'm hoping that someone can point out the error I'm getting here and seg
    > fault. I find it very hard to read code posted by these newsreaders. I'm
    > using OE. I hope someone can read this code.
    >
    > Bill
    > #include <stdio.h>
    > #include <errno.h>
    > #include <string.h>
    >
    > int main()
    > {
    > errno=0;
    > size_t nread, nwrite, nbytes;


    You never use nbytes.

    > nread = nwrite = nbytes = 0;
    > char buf[9000] = { 0 };
    > FILE *fp, *fp2;
    > if ((fp = fopen("6", "rb")) == NULL)
    > perror("fopen 1");
    > if ((fp2 = fopen("a", "wb")) == NULL)
    > perror("fopen 2");


    You detect fopen() errors, but you don't handle them. perror() merely
    prints an error message; it doesn't terminate the program. If fopen()
    fails, you print an error message and continue executing.

    > do {
    > nread = fread(buf, sizeof(buf), 1, fp);
    > nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    > } while (!feof(fp));


    Don't use feof() to decide when to terminate an input loop. Use the
    result returned by the function you're using to read the data. feof()
    is useful mainly to distinguish between an end-of-file condition and an
    error condition. (If there's an error, you'll have an infinite loop,
    because feof() will never return a true result; instead, ferror() will
    do so. No, I'm not telling you to call ferror() instead of feof().)

    > fclose(fp);
    > fclose(fp2);
    > printf("%d %d\n", nread, nwrite);


    nread and nwrite are of type size_t. Use "%zu" rather than "%d" (or one
    of the usual workarounds if your implementation doesn't support "%zu").

    > printf("%s\n",strerror(errno));
    > return 0;
    > }


    There may be other errors; I haven't spent much time on this.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 30, 2012
    #12
  13. Casey Carter <> writes:
    > On 2012-08-30 14:47, Bill Cunningham wrote:
    >> int main()
    >> {

    > [snip]
    >> nread = fread(buf, sizeof(buf), 1, fp);

    >
    > nread = fread(buf, 1, sizeof(buf), fp);
    >
    >> nwrite = fwrite(buf, sizeof(buf), nread, fp2);

    >
    > nwrite = fwrite(buf, 1, nread, fp2);


    The ordering of the second and third arguments to fread() and
    fwrite() affects the value returned (because it's counting different
    things), but not how much they attempt to read or write.

    Since the program doesn't do anything with the results (other than
    printing them), it probably doesn't matter much. But you should
    definitely think about what you want to count when calling fread()
    and fwrite().

    Here's something I wrote about the difference:

    http://stackoverflow.com/a/8589688/827263

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Will write code for food.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 30, 2012
    #13
  14. On Thu, 30 Aug 2012 17:03:48 -0400, "Bill Cunningham"
    <> wrote:

    >Bill Cunningham wrote:
    >> Mark Storkamp wrote:
    >>> In article <k1ofvu$g2o$>,
    >>> "Bill Cunningham" <> wrote:
    >>>
    >>>> I'm hoping that someone can point out the error I'm getting here
    >>>> and seg
    >>>
    >>> Why don't you point out the error you're getting here, then someone
    >>> can help to explain what is causing the error. Or are we to just
    >>> guess what error you are getting?

    >>
    >> Well in the code I compile
    >> fopen 2:text file busy
    >>
    >>>> fault. I find it very hard to read code posted by these newsreaders.
    >>>> I'm using OE. I hope someone can read this code.
    >>>>
    >>>> Bill
    >>>> #include <stdio.h>
    >>>> #include <errno.h>
    >>>> #include <string.h>
    >>>>
    >>>> int main()
    >>>> {
    >>>> errno=0;
    >>>> size_t nread, nwrite, nbytes;
    >>>> nread = nwrite = nbytes = 0;
    >>>> char buf[9000] = { 0 };
    >>>
    >>> Your problem could be here ^
    >>>
    >>>> FILE *fp, *fp2;
    >>>> if ((fp = fopen("6", "rb")) == NULL)
    >>>> perror("fopen 1");
    >>>> if ((fp2 = fopen("a", "wb")) == NULL)
    >>>> perror("fopen 2");
    >>>> do {
    >>>> nread = fread(buf, sizeof(buf), 1, fp);
    >>>> nwrite = fwrite(buf, sizeof(buf), nread, fp2);
    >>>> } while (!feof(fp));
    >>>> fclose(fp);
    >>>> fclose(fp2);
    >>>> printf("%d %d\n", nread, nwrite);
    >>>
    >>> Or it could be here ^
    >>>
    >>>> printf("%s\n",strerror(errno));
    >>>> return 0;
    >>>> }

    >
    > Wait a minute ! wait! Silly me. I have a file areadly called "a" and
    >need to set the w to w+. Let me try that.


    I see you have reverted back to your standard of making random guesses
    at both the problem and the solution. By now you should know it is
    not effective.

    --
    Remove del for email
     
    Barry Schwarz, Aug 30, 2012
    #14
  15. On Thu, 30 Aug 2012 17:16:40 -0400, "Bill Cunningham"
    <> wrote:

    >Mark Storkamp wrote:
    >> In article <k1ofvu$g2o$>,
    >> "Bill Cunningham" <> wrote:
    >>
    >>> I'm hoping that someone can point out the error I'm getting here
    >>> and seg

    >>
    >> Why don't you point out the error you're getting here, then someone
    >> can help to explain what is causing the error. Or are we to just
    >> guess what error you are getting?
    >>
    >>> fault. I find it very hard to read code posted by these newsreaders.
    >>> I'm using OE. I hope someone can read this code.
    >>>
    >>> Bill
    >>> #include <stdio.h>
    >>> #include <errno.h>
    >>> #include <string.h>
    >>>
    >>> int main()
    >>> {
    >>> errno=0;
    >>> size_t nread, nwrite, nbytes;
    >>> nread = nwrite = nbytes = 0;
    >>> char buf[9000] = { 0 };

    >>
    >> Your problem could be here ^

    >
    > Yep! Yep! These cannnot be initialized. The compiler will complain that
    >nread and nwrite aren't declared for some reason.


    What on earth are you talking about? Don't guess what the compiler
    will do. Show an actual compilation and the accompanying error
    messages.

    --
    Remove del for email
     
    Barry Schwarz, Aug 30, 2012
    #15
  16. Barry Schwarz wrote:

    > What on earth are you talking about? Don't guess what the compiler
    > will do. Show an actual compilation and the accompanying error
    > messages.


    Well that's what it said. Unless I'm imagining it. It said my objects
    where undeclared.

    B
     
    Bill Cunningham, Aug 30, 2012
    #16
  17. Barry Schwarz wrote:

    > I see you have reverted back to your standard of making random guesses
    > at both the problem and the solution. By now you should know it is
    > not effective.


    Guessed about what? "Let me try that." Wasn't a guess but a statement of
    fact.

    B
     
    Bill Cunningham, Aug 30, 2012
    #17
  18. Barry Schwarz wrote:

    > I see you have reverted back to your standard of making random guesses
    > at both the problem and the solution. By now you should know it is
    > not effective.


    When you rarely get good advice, atleast not without complaints on clc,
    guessing sometimes is all that's left.

    B
     
    Bill Cunningham, Aug 31, 2012
    #18
  19. In article <>,
    Kenneth Brody <> wrote:
    >
    >Well, it's telling you that the fopen() for the file "a" failed.
    >
    >Then, since you fwrite() to that file anyway, there's your segv.
    >
    >OT hint: The error "text file busy" means that you are attempting to open an
    >executing program for write.


    Good call.

    --
    -Ed Falk,
    http://thespamdiaries.blogspot.com/
     
    Edward A. Falk, Aug 31, 2012
    #19
  20. Kenneth Brody wrote:
    > On 8/30/2012 6:55 PM, Bill Cunningham wrote:
    >> Barry Schwarz wrote:
    >>
    >>> What on earth are you talking about? Don't guess what the compiler
    >>> will do. Show an actual compilation and the accompanying error
    >>> messages.

    >>
    >> Well that's what it said. Unless I'm imagining it. It said my
    >> objects where undeclared.

    >
    > What was the exact wording of the error? And to which line does the
    > error message refer?


    I can't remember exactly. I fixed it and got it working though. :)

    B
     
    Bill Cunningham, Aug 31, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hfk0
    Replies:
    2
    Views:
    21,688
  2. JavaQueries
    Replies:
    1
    Views:
    3,695
    John C. Bollinger
    Mar 1, 2005
  3. Balaji
    Replies:
    3
    Views:
    10,130
  4. Bishop
    Replies:
    1
    Views:
    801
    Bishop
    Feb 24, 2007
  5. juvi
    Replies:
    3
    Views:
    1,057
    Alexey Smirnov
    Jan 22, 2009
Loading...

Share This Page