Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > chunk

Reply
Thread Tools

chunk

 
 
Bill Cunningham
Guest
Posts: n/a
 
      02-04-2008
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


 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-04-2008
In article <Exupj.6292$qI.4067@trnddc03>,
Bill Cunningham <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      02-04-2008

> 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


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      02-04-2008
[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


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-04-2008
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.
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      02-04-2008
In article <wRupj.6293$qI.2440@trnddc03>,
Bill Cunningham <(E-Mail Removed)> 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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-04-2008
"Bill Cunningham" <(E-Mail Removed)> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-04-2008
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.

 
Reply With Quote
 
vippstar@gmail.com
Guest
Posts: n/a
 
      02-04-2008
On Feb 4, 7:46 am, Martin Ambuhl <(E-Mail Removed)> 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.
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      02-04-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sending a file chunk by chunk instead as a whole to a web server Sanjeeb Python 3 08-03-2010 05:52 AM
What is an AVI Chunk Viewer? - AVI Chunk Viewer.jpg (0/1) mazdra76@yahooo.com Computer Support 1 03-17-2006 02:52 AM
Gzip each chunk separately Lior Knaany Java 9 01-16-2006 10:16 AM
ChunkedInputStream bad chunk size from IIS server - Any ideas? Ray Thomas Java 1 07-21-2004 10:36 PM
connection 1: transfer chunk 1 (byte 0 to byte 1024) Jean-Daniel Gamache Java 0 07-14-2004 03:57 AM



Advertisments