chunk

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

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
 
W

Walter Roberson

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.
 
B

Bill Cunningham

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
 
B

Bill Cunningham

[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
 
I

Ian Collins

Bill said:
[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.
 
W

Walter Roberson

Bill Cunningham said:
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,

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.
 
K

Keith Thompson

Bill Cunningham said:
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);
}
 
M

Martin Ambuhl

Bill said:
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.
 
V

vippstar

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.
}




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.
No. Grossly illegal code is not guaranteed to work.
Again, please try to be more polite, your code was not correct either.
 
D

Default User

Martin said:
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
 
B

Bill Cunningham

Your code was,
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
 
B

Bill Cunningham

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
 
W

Walter Roberson

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.
 
W

Walter Roberson

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) ?
 
A

Antoninus Twink

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.
 
K

Kenny McCormack

Antoninus Twink said:
Wa ha ha.


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.
 
D

Default User

Bill said:
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
 
D

Default User

Bill said:
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
 
A

Antoninus Twink

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.
 
W

Walter Roberson

[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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

URGENT 1
error 28
Help with EXT3 Filesystem work 1
file bug 28
question 33
Writing a structure into a file 3
comparison error 12
Problem in compiling a C code with MSVC++6.00 23

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top