chunk

Discussion in 'C Programming' started by Bill Cunningham, Feb 4, 2008.

  1. My compiler gives me a syntax error on line 13 and I don't see. Here's
    the code.

    #include <stdio.h>


    int chunk (char *n1, char *n2, int bs, int nb);
    main(){
    int chunk (char *n1, char *n2, int bs, int nb)
    {FILE *fp;
    fp=fopen(n1,"rb");
    fread(n1,bs,sizeof(nb),fp);
    fclose(fp);
    fp=fopen(n2,"wb");
    fwrite(n2,bs,sizeof(nb),fp);
    fclose(fp);}

    I would think this fuction should be declared on a header and compile into
    an object file and maybe even a lib file and linked to something using main.
    But this still should work shouldn't it?

    Bill
     
    Bill Cunningham, Feb 4, 2008
    #1
    1. Advertising

  2. In article <Exupj.6292$qI.4067@trnddc03>,
    Bill Cunningham <> wrote:
    > My compiler gives me a syntax error on line 13 and I don't see. Here's
    >the code.


    >#include <stdio.h>
    >
    >
    >int chunk (char *n1, char *n2, int bs, int nb);
    >main(){
    > int chunk (char *n1, char *n2, int bs, int nb)
    > {FILE *fp;
    > fp=fopen(n1,"rb");
    > fread(n1,bs,sizeof(nb),fp);
    > fclose(fp);
    > fp=fopen(n2,"wb");
    > fwrite(n2,bs,sizeof(nb),fp);
    > fclose(fp);}


    You are missing a }. You have an open { from the main() line,
    and you have an open { from the int chunk line that is inside main.

    Unless you are using gcc or another compiler with a similar extension,
    it is not allowed to define nested functions, so it would not be valid
    to have int chunk() { inside of the definition of main.

    You must be using a C90 compiler, as otherwise you would not be able
    to use the implicit int return result for your definition of main(),
    and yet you do not return any value within the body of main.
    --
    This is a Usenet signature block. Please do not quote it when replying
    to one of my postings.
    http://en.wikipedia.org/wiki/Signature_block
     
    Walter Roberson, Feb 4, 2008
    #2
    1. Advertising


  3. > You are missing a }. You have an open { from the main() line,
    > and you have an open { from the int chunk line that is inside main.
    >
    > Unless you are using gcc or another compiler with a similar extension,
    > it is not allowed to define nested functions, so it would not be valid
    > to have int chunk() { inside of the definition of main.
    >
    > You must be using a C90 compiler, as otherwise you would not be able
    > to use the implicit int return result for your definition of main(),
    > and yet you do not return any value within the body of main.


    Oh I see now. What I think I would want to do is declare the function in
    a header like what is above main() then define it in its own c file and run
    gcc -c on it to make an object file. I have gcc-3.4.6. What about the
    sizeofs will they work? I am using int on my machine because size_t and int
    are both 32 bit. Short is 16 bit on my machine while a char of couse is 8
    bit. I just want to use this on my machine otherwise I would have used
    size_t bs and size_t nb. But here-

    fwrite(n2,bs,sizeof(nb),fp); /* DO I need sizeof for fwrite? and the same
    question for fread. */

    Bill
     
    Bill Cunningham, Feb 4, 2008
    #3
  4. [snip]

    > You must be using a C90 compiler, as otherwise you would not be able
    > to use the implicit int return result for your definition of main(),
    > and yet you do not return any value within the body of main.


    gcc has switches for ansi and iso c. I would just like to stick with
    ansi C. Would return chunk() work while passing the parameters I would want
    to return chunk's parameters at return?

    Bill
     
    Bill Cunningham, Feb 4, 2008
    #4
  5. Bill Cunningham

    Ian Collins Guest

    Bill Cunningham wrote:
    > [snip]
    >
    >> You must be using a C90 compiler, as otherwise you would not be able
    >> to use the implicit int return result for your definition of main(),
    >> and yet you do not return any value within the body of main.

    >
    > gcc has switches for ansi and iso c. I would just like to stick with
    > ansi C. Would return chunk() work while passing the parameters I would want
    > to return chunk's parameters at return?
    >

    Try it yourself, as a learner, always invoke gcc with

    gcc -Wall -ansi -pedantic

    Then you will get enough errors and warnings to keep you on the straight
    and narrow.

    --
    Ian Collins.
     
    Ian Collins, Feb 4, 2008
    #5
  6. In article <wRupj.6293$qI.2440@trnddc03>,
    Bill Cunningham <> wrote:

    >What about the
    >sizeofs will they work? I am using int on my machine because size_t and int
    >are both 32 bit. Short is 16 bit on my machine while a char of couse is 8
    >bit. I just want to use this on my machine otherwise I would have used
    >size_t bs and size_t nb. But here-
    >
    >fwrite(n2,bs,sizeof(nb),fp); /* DO I need sizeof for fwrite? and the same
    >question for fread. */


    Your code was,

    >>> int chunk (char *n1, char *n2, int bs, int nb)
    >>> {FILE *fp;
    >>> fp=fopen(n1,"rb");
    >>> fread(n1,bs,sizeof(nb),fp);
    >>> fclose(fp);
    >>> fp=fopen(n2,"wb");
    >>> fwrite(n2,bs,sizeof(nb),fp);
    >>> fclose(fp);}


    You ask whether the sizeof() would work. The answer is "We don't know".
    You have not defined the meaning of the various parameters for us.
    It might work... it just seems unlikely that anyone would want
    to bother to pass a parameter by value (bs) only to take the
    size of the -received- parameter rather than the value of the parameter.

    Note that sizeof(nb) is going to be sizeof(int), not the size of
    whatever value you happen to pass in that spot -- e.g., if your
    calling routine happens to supply a long there, then because of the
    routine prototype specifying int in that position, the calling
    routine is going to convert the passed value from the (hypothetical)
    long into an int.

    You might as well just have coded sizeof(int) and saved confusion
    compared to sizeof(nb).

    I'm not saying tha sizeof(int) is the correct thing to pass to
    fread or fwrite for this purpose: you did not define what the
    routine is supposed to -do- so we don't know whether sizeof(int)
    is the proper thing for that position or not.
    --
    "I was very young in those days, but I was also rather dim."
    -- Christopher Priest
     
    Walter Roberson, Feb 4, 2008
    #6
  7. "Bill Cunningham" <> writes:
    > My compiler gives me a syntax error on line 13 and I don't see. Here's
    > the code.
    >
    > #include <stdio.h>
    >
    >
    > int chunk (char *n1, char *n2, int bs, int nb);
    > main(){
    > int chunk (char *n1, char *n2, int bs, int nb)
    > {FILE *fp;
    > fp=fopen(n1,"rb");
    > fread(n1,bs,sizeof(nb),fp);
    > fclose(fp);
    > fp=fopen(n2,"wb");
    > fwrite(n2,bs,sizeof(nb),fp);
    > fclose(fp);}

    [...]

    You have mismatched braces, and you're trying to define a function
    inside another function (C doesn't allow nested function definitions).

    The way you arrange your source code makes this kind of error
    difficult to see. You hide your opening and closing braces by putting
    them next to other tokens with no whitespace. Since they define the
    structure of your program, they should stand out.

    Here's a reformatted version of your program; I've changed nothing but
    the layout:

    #include <stdio.h>

    int chunk (char *n1, char *n2, int bs, int nb);

    main()
    {
    int chunk (char *n1, char *n2, int bs, int nb)
    {
    FILE *fp;
    fp = fopen(n1, "rb");
    fread(n1, bs, sizeof(nb), fp);
    fclose(fp);
    fp = fopen(n2, "wb");
    fwrite(n2, bs, sizeof(nb), fp);
    fclose(fp);
    }

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 4, 2008
    #7
  8. Bill Cunningham wrote:
    > My compiler gives me a syntax error on line 13 and I don't see.


    Neither do we, since you didn't bother to show us the diagnostic or
    identify line 13. In any case, your code is dead much earlier, since
    you attempt to use nested functions, which do not exist in C.

    Here's what you need to do. _Outside_ of main, insert this code:
    #include <stdio.h>


    void /* fixed bogus 'int' */ chunk(char *n1, char *n2,
    int bs, int nb)
    {
    FILE *fp;
    fp = fopen(n1, "rb");
    fread(n1, bs, sizeof(nb), fp);
    fclose(fp);
    fp = fopen(n2, "wb");
    fwrite(n2, bs, sizeof(nb), fp);
    fclose(fp);
    }


    > Here's the code.
    >
    > #include <stdio.h>
    >
    >
    > int chunk (char *n1, char *n2, int bs, int nb);
    > main(){
    > int chunk (char *n1, char *n2, int bs, int nb)
    > {FILE *fp;
    > fp=fopen(n1,"rb");
    > fread(n1,bs,sizeof(nb),fp);
    > fclose(fp);
    > fp=fopen(n2,"wb");
    > fwrite(n2,bs,sizeof(nb),fp);
    > fclose(fp);}
    >
    > I would think this fuction should be declared on a header and compile into
    > an object file and maybe even a lib file and linked to something using main.


    What in the world is that supposed to mean?

    > But this still should work shouldn't it?


    No. Grossly illegal code is not guaranteed to work.
     
    Martin Ambuhl, Feb 4, 2008
    #8
  9. Bill Cunningham

    Guest

    On Feb 4, 7:46 am, Martin Ambuhl <> wrote:
    > Bill Cunningham wrote:
    > > My compiler gives me a syntax error on line 13 and I don't see.

    >
    > Neither do we, since you didn't bother to show us the diagnostic or
    > identify line 13. In any case, your code is dead much earlier, since
    > you attempt to use nested functions, which do not exist in C.

    Please try to be more polite.

    > Here's what you need to do. _Outside_ of main, insert this code:

    Why do you suggest code that is not correct?
    > #include <stdio.h>
    >
    > void /* fixed bogus 'int' */ chunk(char *n1, char *n2,
    > int bs, int nb)
    > {
    > FILE *fp;
    > fp = fopen(n1, "rb");

    What if fopen returns NULL?

    > fread(n1, bs, sizeof(nb), fp);

    its sizeof (nb), bs, not the other way around.

    I am curious what happends when bs*sizeof(nb) > what n1 points to.
    You also don't check the return value of fread.

    > fclose(fp);
    > fp = fopen(n2, "wb");

    What if fopen returns NULL?

    > fwrite(n2, bs, sizeof(nb), fp);

    its sizeof (nb), bs, not the other way around.
    I am curious what happends when bs*sizeof(nb) > what n2 points to.
    You also don't check the return value of fwrite.

    > fclose(fp);

    What about the return value of fclose()? fclose could fail here.

    > }
    > > Here's the code.

    >
    > > #include <stdio.h>

    >
    > > int chunk (char *n1, char *n2, int bs, int nb);
    > > main(){
    > > int chunk (char *n1, char *n2, int bs, int nb)
    > > {FILE *fp;
    > > fp=fopen(n1,"rb");
    > > fread(n1,bs,sizeof(nb),fp);
    > > fclose(fp);
    > > fp=fopen(n2,"wb");
    > > fwrite(n2,bs,sizeof(nb),fp);
    > > fclose(fp);}

    >
    > > I would think this fuction should be declared on a header and compile into
    > > an object file and maybe even a lib file and linked to something using main.

    >
    > What in the world is that supposed to mean?

    Please try to be more polite, especially when you critisize someone
    elses code and then you post code that does not work.

    > > But this still should work shouldn't it?

    >
    > No. Grossly illegal code is not guaranteed to work.

    Again, please try to be more polite, your code was not correct either.
     
    , Feb 4, 2008
    #9
  10. Bill Cunningham

    Default User Guest

    Martin Ambuhl wrote:

    > Bill Cunningham wrote:
    > > My compiler gives me a syntax error on line 13 and I don't see.

    >
    > Neither do we, since you didn't bother to show us the diagnostic or
    > identify line 13. In any case, your code is dead much earlier, since
    > you attempt to use nested functions, which do not exist in C.


    I gave Bill a lot of (in my opinion of course) good information on this
    problem last time. He ignored everything except adding the blockcount
    parameter. Unfortunately he didn't USE that parameter, but it is indeed
    there.

    That's pretty much it for me. I'm not going to spend time and effort
    trying to help when he doesn't listen.




    Brian
     
    Default User, Feb 4, 2008
    #10
  11. > Your code was,
    >
    >>>> int chunk (char *n1, char *n2, int bs, int nb)
    >>>> {FILE *fp;
    >>>> fp=fopen(n1,"rb");
    >>>> fread(n1,bs,sizeof(nb),fp);
    >>>> fclose(fp);
    >>>> fp=fopen(n2,"wb");
    >>>> fwrite(n2,bs,sizeof(nb),fp);
    >>>> fclose(fp);}

    >
    > You ask whether the sizeof() would work. The answer is "We don't know".
    > You have not defined the meaning of the various parameters for us.
    > It might work... it just seems unlikely that anyone would want
    > to bother to pass a parameter by value (bs) only to take the
    > size of the -received- parameter rather than the value of the parameter.

    [snip]

    This function is supposed to do part of what dd in unix and linux does.
    dd if=n1 of=n2 bs=2048 count=1

    bs is block size. nb is number of blocks, like count in dd. n1 is the name
    of the file and n2 is the name of the chunk or block I want.

    Bill
     
    Bill Cunningham, Feb 4, 2008
    #11

  12. > I gave Bill a lot of (in my opinion of course) good information on this
    > problem last time. He ignored everything except adding the blockcount
    > parameter. Unfortunately he didn't USE that parameter, but it is indeed
    > there.
    >
    > That's pretty much it for me. I'm not going to spend time and effort
    > trying to help when he doesn't listen.
    >

    I appreciate your help Brian. Maybe some of what you said went over my
    head. You did spend quite a bit of time on this but I think I've encounted
    new problems with trying to code this especially in the fread, fwrite.

    Thanks

    Bill
     
    Bill Cunningham, Feb 4, 2008
    #12
  13. In article <1eJpj.46083$K%.44258@trnddc04>,
    Bill Cunningham <> wrote:
    >> Your code was,


    >>>>> int chunk (char *n1, char *n2, int bs, int nb)
    >>>>> {FILE *fp;
    >>>>> fp=fopen(n1,"rb");
    >>>>> fread(n1,bs,sizeof(nb),fp);
    >>>>> fclose(fp);
    >>>>> fp=fopen(n2,"wb");
    >>>>> fwrite(n2,bs,sizeof(nb),fp);
    >>>>> fclose(fp);}


    >> You ask whether the sizeof() would work. The answer is "We don't know".
    >> You have not defined the meaning of the various parameters for us.


    >This function is supposed to do part of what dd in unix and linux does.
    >dd if=n1 of=n2 bs=2048 count=1


    >bs is block size. nb is number of blocks, like count in dd. n1 is the name
    >of the file and n2 is the name of the chunk or block I want.


    In that case, No, the sizeof() in there will not work, unless the
    block count nb that you pass in -happens- to be the same as sizeof(int)
    on your system. Leave out the sizeof() calls and just pass nb
    in that parameter.
    --
    "I will speculate that [...] applications [...] could actually see a
    performance boost for most users by going dual-core [...] because it
    is running the adware and spyware that [...] are otherwise slowing
    down the single CPU that user has today" -- Herb Sutter
     
    Walter Roberson, Feb 4, 2008
    #13
  14. In article <>,
    <> wrote:
    >On Feb 4, 7:46 am, Martin Ambuhl <> wrote:


    >> fread(n1, bs, sizeof(nb), fp);

    >its sizeof (nb), bs, not the other way around.


    Why do you say that, vippstar?

    C89 4.9.8.1 The fread Function

    Synopsis

    #include <stdio.h>
    size_t fread(void *ptr, size_t size, size_t nmemb,
    FILE *stream);

    Description

    The fread function reads, into the array ppinted to by ptr,
    up to nmemb elements whose size is specified by size, from the
    stream pointed to by stream. [...]

    The fread function returns the number of elements successfully read,
    which may be less than nmemb if a read error or end-of-file is
    encountered. [...]


    What leads you to conclude that sizeof(nb) is the desired blocksize
    and bs is the number of blocks, rather than keeping with the
    original poster's scheme that bs is the desired Block Size (bs)
    and that sizeof(nb) or more likely nb itself is the Number of Blocks (nb) ?
    --
    "Okay, buzzwords only. Two syllables, tops." -- Laurie Anderson
     
    Walter Roberson, Feb 4, 2008
    #14
  15. On 4 Feb 2008 at 17:51, Default User wrote:
    > Martin Ambuhl wrote:
    >
    >> Bill Cunningham wrote:
    >> > My compiler gives me a syntax error on line 13 and I don't see.

    >>
    >> Neither do we, since you didn't bother to show us the diagnostic or
    >> identify line 13. In any case, your code is dead much earlier, since
    >> you attempt to use nested functions, which do not exist in C.

    >
    > I gave Bill a lot of (in my opinion of course) good information on this
    > problem last time.


    Wa ha ha.

    > That's pretty much it for me. I'm not going to spend time and effort
    > trying to help when he doesn't listen.


    A real tragedy for Bill, I'm sure. I wouldn't trust the Loser's advice
    on how to brush my teeth.
     
    Antoninus Twink, Feb 4, 2008
    #15
  16. In article <>,
    Antoninus Twink <> eloquently wrote:
    ....
    >
    >Wa ha ha.
    >
    >> That's pretty much it for me. I'm not going to spend time and effort
    >> trying to help when he doesn't listen.

    >
    >A real tragedy for Bill, I'm sure. I wouldn't trust the Loser's advice
    >on how to brush my teeth.


    Good line!
    Definitely a keeper.
     
    Kenny McCormack, Feb 4, 2008
    #16
  17. Bill Cunningham

    Default User Guest

    Bill Cunningham wrote:

    >
    > > I gave Bill a lot of (in my opinion of course) good information on
    > > this problem last time. He ignored everything except adding the
    > > blockcount parameter. Unfortunately he didn't USE that parameter,
    > > but it is indeed there.
    > >
    > > That's pretty much it for me. I'm not going to spend time and effort
    > > trying to help when he doesn't listen.
    > >

    > I appreciate your help Brian. Maybe some of what you said went over
    > my head. You did spend quite a bit of time on this but I think I've
    > encounted new problems with trying to code this especially in the
    > fread, fwrite.


    The first thing told you to do was the write down what this function is
    supposed to do. That gives you guidance in designing the algorithm. If
    *you* don't know what it's supposed to be doing, how we possibly help?

    Do that first. Then take it from there.




    Brian
     
    Default User, Feb 4, 2008
    #17
  18. Bill Cunningham

    Default User Guest

    Bill Cunningham wrote:

    > This function is supposed to do part of what dd in unix and linux
    > does. dd if=n1 of=n2 bs=2048 count=1


    Write down what you think "dd in unix does" means. That lets us know
    that you understand what that phrase means. I could write out what I
    think you are trying to do, but it's far better if you do it.

    > bs is block size. nb is number of blocks, like count in dd. n1 is the
    > name of the file and n2 is the name of the chunk or block I want.


    See, that last part makes no sense. I believe n2 is the output
    filename. I don't even know what "name of the chunk or block" means. As
    I said before, I strongly suggest that you start using descriptive
    parameter and variable names. "n1" and "n2" are not descriptive.




    Brian
     
    Default User, Feb 4, 2008
    #18
  19. On 4 Feb 2008 at 19:53, Kenny McCormack wrote:
    > In article <>,
    > Antoninus Twink <> eloquently wrote:
    > ...
    >>
    >>Wa ha ha.
    >>
    >>> That's pretty much it for me. I'm not going to spend time and effort
    >>> trying to help when he doesn't listen.

    >>
    >>A real tragedy for Bill, I'm sure. I wouldn't trust the Loser's advice
    >>on how to brush my teeth.

    >
    > Good line!
    > Definitely a keeper.


    Thanks!

    And The Loser proves the sniggers are well-deserved by following up with
    the following piece of dazzlingly insightful advice:

    > The first thing told you to do was the write down what this function
    > is supposed to do. That gives you guidance in designing the algorithm.


    And to think he dispenses this "advice" at no charge! Amazing.
     
    Antoninus Twink, Feb 4, 2008
    #19
  20. In article <8Zupj.6295$qI.4673@trnddc03>,
    Bill Cunningham <> wrote:
    >[snip]


    >> You must be using a C90 compiler, as otherwise you would not be able
    >> to use the implicit int return result for your definition of main(),
    >> and yet you do not return any value within the body of main.


    > gcc has switches for ansi and iso c. I would just like to stick with
    >ansi C.


    ANSI's 1989 C standard ("C89") was adopted with section renumbering
    and very minor changes by ISO in 1990 ("C90").

    ISO's 1999 C standard ("C99") was adopted intact by ANSI.

    Thus, "ANSI C" may refer to either the 1989 or 1999 standards,
    and "ISO C" may refer to either the 1990 or 1999 standards.
    If you are trying to distinguish some year or functionality in C
    based solely upon the name of the standards organization, you cannot.


    >Would return chunk() work while passing the parameters I would want
    >to return chunk's parameters at return?


    No, not from main(). From main() you need to return 0 or EXIT_SUCCESS
    or EXIT_FAILURE; if you return any other value, the result
    is implementation-specific.
    --
    "History is a pile of debris" -- Laurie Anderson
     
    Walter Roberson, Feb 4, 2008
    #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. Jean-Daniel Gamache
    Replies:
    0
    Views:
    428
    Jean-Daniel Gamache
    Jul 14, 2004
  2. Ray Thomas
    Replies:
    1
    Views:
    3,279
    Ray Thomas
    Jul 21, 2004
  3. Lior Knaany

    Gzip each chunk separately

    Lior Knaany, Jan 2, 2006, in forum: Java
    Replies:
    9
    Views:
    1,121
    Lior Knaany
    Jan 16, 2006
  4. Rob Shepherd
    Replies:
    0
    Views:
    597
    Rob Shepherd
    Jun 9, 2005
  5. Sanjeeb
    Replies:
    3
    Views:
    426
    Ryan Kelly
    Aug 3, 2010
Loading...

Share This Page