function – C / C++

  c

Q(Question):

I am trying to write a utility that takes as argument 1 a file name to
write to and as argument 2 takes num as a number of zeros to write a
argument 1. A file of zeros. This is how far I have got. Why would I want a
file of zeros? To mount to a loopback device.

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
if (argc != 3)
{
puts ("usage error");
exit (EXIT_FAILURE);
}
char a = ‘0’;
int b, num;
FILE *fp;
num = atoi (argv[2]);
fp = fopen (argv[1], "wb");

I am thinking that I would need a for loop involved here somewhere. I am
not sure what to put in the body.

Bill

A(Answer):

Bill Cunningham wrote:

I am trying to write a utility that takes as argument 1 a file
name to write to and as argument 2 takes num as a number of zeros to
write a argument 1. A file of zeros. This is how far I have got. Why
would I want a file of zeros? To mount to a loopback device.

[OT]
dd if=/dev/zero of=<your filecount=<your size>
[/OT]

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
if (argc != 3)
{
puts ("usage error");
exit (EXIT_FAILURE);
}
char a = ‘0’;

char a = ‘\0’;

int b, num;
FILE *fp;
num = atoi (argv[2]);
fp = fopen (argv[1], "wb");

I am thinking that I would need a for loop involved here
somewhere. I am not sure what to put in the body.

while (num–)
fputc(a, fp);
fclose(fp);
return 0;
}

Still needs some error checking (num>=0, fp != NULL, fclose and fput worked)

Bill

Bye, Jojo

A(Answer):

"Joachim Schmitz" <no*********@schmitz-digital.dewrote in message
news:g1**********@online.de…

[OT]
dd if=/dev/zero of=<your filecount=<your size>
[/OT]

Ah. The easy way out 🙂

>#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
if (argc != 3)
{
puts ("usage error");
exit (EXIT_FAILURE);
}
char a = ‘0’;

char a = ‘\0’;

Why the termination string character? Do you mean to replace my line or
are you adding one?

> int b, num;
FILE *fp;
num = atoi (argv[2]);
fp = fopen (argv[1], "wb");

I am thinking that I would need a for loop involved here
somewhere. I am not sure what to put in the body.

while (num–)
fputc(a, fp);
fclose(fp);
return 0;
}

Still needs some error checking (num>=0, fp != NULL, fclose and fput
worked)

I can get that part. I think.

>Bill

Bye, Jojo

A(Answer):

"Joachim Schmitz" <no*********@schmitz-digital.dewrote in message
news:g1**********@online.de…

[snip]

while (num–) fputc(a, fp);
fclose(fp);
return 0;
}

Still needs some error checking (num>=0, fp != NULL, fclose and fput
worked)

>Bill

Bye, Jojo

While(num–) What’s that? Is that the same as what I was thinking here.

int c;
for(c=1;c<num;c++)

Bill

A(Answer):

Bill Cunningham wrote:

"Joachim Schmitz" <no*********@schmitz-digital.dewrote in message
news:g1**********@online.de…

>[OT]
dd if=/dev/zero of=<your filecount=<your size>
[/OT]

Ah. The easy way out 🙂

Jep 😎 Why reinventing the wheel…

>>#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
if (argc != 3)
{
puts ("usage error");
exit (EXIT_FAILURE);
}
char a = ‘0’;

char a = ‘\0’;

Why the termination string character?

It’s not just that, it also is the nul character, a byte with all bits
unset.

Do you mean to replace my
line or are you adding one?

Replacing. Do you want zeros in the file or the ascii representation of the
charcter 0? I believe the earlier.

>> int b, num;
FILE *fp;
num = atoi (argv[2]);
fp = fopen (argv[1], "wb");

I am thinking that I would need a for loop involved here
somewhere. I am not sure what to put in the body.

while (num–)
fputc(a, fp);
fclose(fp);
return 0;
}

Still needs some error checking (num>=0, fp != NULL, fclose and fput
worked)

I can get that part. I think.

Yep, left as an excercise to the reader 😎

>>Bill

Bye, Jojo

A(Answer):

Bill Cunningham wrote:

"Joachim Schmitz" <no*********@schmitz-digital.dewrote in message
news:g1**********@online.de…

[snip]

>while (num–) fputc(a, fp);
fclose(fp);
return 0;
}

Still needs some error checking (num>=0, fp != NULL, fclose and fput
worked)

>>Bill

Bye, Jojo

While(num–) What’s that? Is that the same as what I was thinking
here.
int c;
for(c=1;c<num;c++)

for(c=1;c<=num;c++) /* if you want to loop num times */
It’s equivalent, just counting backwards and not needing yet another
variable

Bye, Jojo

A(Answer):

"Joachim Schmitz" <no*********@schmitz-digital.dewrote in message
news:g1**********@online.de…

[snip]

>>Still needs some error checking (num>=0, fp != NULL, fclose and fput
worked)

I can get that part. I think.

Yep, left as an excercise to the reader 😎

Now here’s what I tried in error checking that gave me a warning. I
must’ve messed up.

if((fputc(a,fp))!=NULL)
if((fclose(fp))!=NULL)

comparing argument with a without a cast or something like that the compiler
said. Without cast and comparing was mentioned by the compiler but it
compiled and didn’t work. So I tried this.

if((fputc(a,fp))==NULL)
puts("fputc error");

When I ran the binary fputc error was mentioned 10 times. My 2nd arg was 10.

Bill

A(Answer):

Bill Cunningham wrote:

>

…. snip …

>
While(num–) What’s that? Is that the same as what I was thinking here.

That means while the expression num– is non-zero, do the
following. The expression value is num before decrementing it.


[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **

A(Answer):

Bill Cunningham wrote:

>

…. snip …

>
comparing argument with a without a cast or something like that
the compiler said. Without cast and comparing was mentioned by
the compiler but it compiled and didn’t work. So I tried this.

if((fputc(a,fp))==NULL)
puts("fputc error");

When I ran the binary fputc error was mentioned 10 times. My
2nd arg was 10.

Look up what a function does and returns before you use it. In
this case:

7.19.7.3 The fputc function

Synopsis
[#1]
#include <stdio.h>
int fputc(int c, FILE *stream);

Description

[#2] The fputc function writes the character specified by c
(converted to an unsigned char) to the output stream pointed
to by stream, at the position indicated by the associated
file position indicator for the stream (if defined), and
advances the indicator appropriately. If the file cannot
support positioning requests, or if the stream was opened
with append mode, the character is appended to the output
stream.

Returns

[#3] The fputc function returns the character written. If a
write error occurs, the error indicator for the stream is
set and fputc returns EOF.

Note the returned value.


[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **

A(Answer):

Bill Cunningham wrote:

<snip>

Now here’s what I tried in error checking that gave me a warning.
must’ve messed up.

if((fputc(a,fp))!=NULL)

If you read your system’s documentation for fputc you’ll find that it
returns either the character passed (as an int) or EOF on error. NULL
is the wrong value to be comparing with. The above is better written
as:

if (fputc(ch, stream) == EOF) {
/* error */
}

if((fclose(fp))!=NULL)

Again, fclose will return EOF on failure.

<snip>

A(Answer):

"santosh" <sa*********@gmail.comwrote in message
news:g1**********@registered.motzarella.org…

Bill Cunningham wrote:

<snip>

> Now here’s what I tried in error checking that gave me a warning.
must’ve messed up.

if((fputc(a,fp))!=NULL)

If you read your system’s documentation for fputc you’ll find that it
returns either the character passed (as an int) or EOF on error. NULL
is the wrong value to be comparing with. The above is better written
as:

if (fputc(ch, stream) == EOF) {
/* error */
}

>if((fclose(fp))!=NULL)

Again, fclose will return EOF on failure.

<snip>

I usually don’t miss these things in error checking. I was suprised when
it didn’t work but I guess I’ve had NULL on my mind. Earlier in this post I
wanted to write zeros to a file and found out the proper use was with the
string terminating character ‘\0’ which is a byte with all the bits turned
off. Now a questions about the *putc functions. Do they take and write a
char or 8 bits, or an unsigned int? Fputc’s first parameter is an int and I
declared an int and gave it the value of ‘\0’. If the char is taken from the
first int parameter why an int instead of a char? Is more information needed
than the char itself like padding or the like?

Bill

A(Answer):

"CBFalconer" <cb********@yahoo.comwrote in message
news:48***************@yahoo.com…

[snip]

That means while the expression num– is non-zero, do the
following. The expression value is num before decrementing it.

conditionals. One of my weak points now. I can barely use while. I have
for and it down well. Switch is unusable and unreadable to me at this point.

Bill

A(Answer):

"Bill Cunningham" <no****@nspam.comwrites:

"CBFalconer" <cb********@yahoo.comwrote in message
news:48***************@yahoo.com…

[snip]

>That means while the expression num– is non-zero, do the
following. The expression value is num before decrementing it.

conditionals. One of my weak points now. I can barely use while. I have
for and it down well. Switch is unusable and unreadable to me at this point.

Bill

"while" is about 3x easier to understand than "for". See if you can see why.

A(Answer):

Bill Cunningham wrote:

>
"santosh" <sa*********@gmail.comwrote in message
news:g1**********@registered.motzarella.org…

>Bill Cunningham wrote:

<snip>

>> Now here’s what I tried in error checking that gave me a
warning. must’ve messed up.

if((fputc(a,fp))!=NULL)

If you read your system’s documentation for fputc you’ll find that it
returns either the character passed (as an int) or EOF on error. NULL
is the wrong value to be comparing with. The above is better written
as:

if (fputc(ch, stream) == EOF) {
/* error */
}

>>if((fclose(fp))!=NULL)

Again, fclose will return EOF on failure.

<snip>

I usually don’t miss these things in error checking. I was
suprised when
it didn’t work but I guess I’ve had NULL on my mind. Earlier in this
post I wanted to write zeros to a file and found out the proper use
was with the string terminating character ‘\0’ which is a byte with
all the bits turned off.

Not necessarily. It’s just a byte with value zero.

Now a questions about the *putc functions. Do
they take and write a char or 8 bits, or an unsigned int?

They take an int and a FILE * parameter and they attempt to write the
int parameter, cast to an unsigned char, to the stream indicated. A
char is commonly, but not necessarily eight bits. It’s needs to be at
least eight bits, but could be more, as in the Cray or in most DSPs.

Fputc’s
first parameter is an int and I declared an int and gave it the value
of ‘\0’. If the char is taken from the first int parameter why an int
instead of a char? Is more information needed than the char itself
like padding or the like?

This is I think just for compatibility with fgetc.

A(Answer):

santosh <sa*********@gmail.comwrites:

Bill Cunningham wrote:

>>
"santosh" <sa*********@gmail.comwrote in message
news:g1**********@registered.motzarella.org…

>>Bill Cunningham wrote:

<snip>

Now here’s what I tried in error checking that gave me a
warning. must’ve messed up.

if((fputc(a,fp))!=NULL)

If you read your system’s documentation for fputc you’ll find that it
returns either the character passed (as an int) or EOF on error. NULL
is the wrong value to be comparing with. The above is better written
as:

if (fputc(ch, stream) == EOF) {
/* error */
}

if((fclose(fp))!=NULL)

Again, fclose will return EOF on failure.

<snip>

I usually don’t miss these things in error checking. I was
suprised when
it didn’t work but I guess I’ve had NULL on my mind. Earlier in this
post I wanted to write zeros to a file and found out the proper use
was with the string terminating character ‘\0’ which is a byte with
all the bits turned off.

Not necessarily. It’s just a byte with value zero.

Why on earth would you say that? It serves ZERO purpose whatsoever to
someone with such obvious difficulties as Bill.. All you will do is
confuse the guy more.

>

>Now a questions about the *putc functions. Do
they take and write a char or 8 bits, or an unsigned int?

They take an int and a FILE * parameter and they attempt to write the
int parameter, cast to an unsigned char, to the stream indicated. A
char is commonly, but not necessarily eight bits. It’s needs to be at
least eight bits, but could be more, as in the Cray or in most DSPs.

Ditto. Refer the man to the man page or whatever.

A(Answer):

santosh wrote:

Bill Cunningham wrote:

>Earlier in this
post I wanted to write zeros to a file and found out the proper use
was with the string terminating character ‘\0’ which is a byte with
all the bits turned off.

Not necessarily. It’s just a byte with value zero.

Bill is right, you are wrong.

5.2 Environmental considerations
5.2.1 Character sets

A byte with all bits set to 0, called the null character,
shall exist in the basic execution character set;
it is used to terminate a character string literal.


pete

A(Answer):

"Bill Cunningham" <no****@nspam.comwrites:

"Joachim Schmitz" <no*********@schmitz-digital.dewrote in message
news:g1**********@online.de…

[snip]

>>>Still needs some error checking (num>=0, fp != NULL, fclose and fput
worked)

I can get that part. I think.

Yep, left as an excercise to the reader 😎

Now here’s what I tried in error checking that gave me a warning. I
must’ve messed up.

if((fputc(a,fp))!=NULL)
if((fclose(fp))!=NULL)

comparing argument with a without a cast or something like that the compiler
said. Without cast and comparing was mentioned by the compiler but it
compiled and didn’t work.

"something like that"? Is there some reason you couldn’t tell us
*exactly* what the compiler said?

(Incidentally, be wary of any compiler diagnostic that implies a cast
is missing. 99% of the time a cast is exactly the wrong solution.)

So I tried this.

if((fputc(a,fp))==NULL)
puts("fputc error");

When I ran the binary fputc error was mentioned 10 times. My 2nd arg was 10.

RTFM. (Read The Fine Manual.)

Read the documentation for fputc, either your system’s online
documentation, or your C textbook, or some version or draft of the C
standard. What type does it return? What do different returned
values mean? What value or values does it return to indicate that
there was an error? Does fputc return a pointer type? Does it make
any sense to compare its result to NULL, which is a null pointer
constant?

Repeat the above paragraph, replacing "fputc" with "fclose".

(Don’t bother posting the answers here; most of us already know them.)

You’re obviously guessing, yet again. Don’t do that. Don’t even
think about using a function until you’ve read and understood the
documentation for that function. If your compiler warns you about a
function call, it’s very likely that you’re using that function
incorrectly; consult the function’s documentation *before* (or instead
of) posting here.

And add some whitespace to make your code more readable. You wrote:

if((fputc(a,fp))!=NULL)

I’d write:

if (fputc(a, fp) != NULL)

Note that I’ve also dropped an extraneous set of parentheses.

(I just fixed the layout, not the error; I’ll leave that to you.)


Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
— Antony Jay and Jonathan Lynn, "Yes Minister"

A(Answer):

santosh <sa*********@gmail.comwrites:

Bill Cunningham wrote:

>"santosh" <sa*********@gmail.comwrote in message
news:g1**********@registered.motzarella.org…

[…]

>it didn’t work but I guess I’ve had NULL on my mind. Earlier in this
post I wanted to write zeros to a file and found out the proper use
was with the string terminating character ‘\0’ which is a byte with
all the bits turned off.

Not necessarily. It’s just a byte with value zero.

Which, for all practical purposes, *is* a byte with all the bits
turned off.

(If plain char is signed, and if it has more than one representation
of zero, it’s conceivable that storing ‘\0’ in a char object might
give it a representation other all-bits-zero — but even in such an
implementation, I’d be astonished if it didn’t choose the
all-bits-zero representation.)

Incidentally, it’s true that ‘\0’ is used to terminate a string, but I
don’t think that’s relevant in this case. You’re writing zero bytes
to a binary file. The data isn’t being treated as a string or
strings, so the fact that ‘\0’ happens to be a string terminator isn’t
relevant.

[…]


Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
— Antony Jay and Jonathan Lynn, "Yes Minister"

A(Answer):

"santosh" <sa*********@gmail.comwrote in message
news:g1**********@registered.motzarella.org…

Not necessarily. It’s just a byte with value zero.

I wrote this program in my djgpp compile not my gcc one and got the
ascii value of 48.

printf("%i\n",’0′);

Bill

A(Answer):

In comp.lang.c, Bill Cunningham wrote:

>
"santosh" <sa*********@gmail.comwrote in message
news:g1**********@registered.motzarella.org…

>Not necessarily. It’s just a byte with value zero.

I wrote this program in my djgpp compile not my gcc one and got the
ascii value of 48.

printf("%i\n",’0′);

Right. That’s not a "byte with value zero". That’s a character with a value
of ‘0’ (much like a character with a value of ‘Q’).


Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
———- Slackware – Because I know what I’m doing. ——

A(Answer):

"Bill Cunningham" <no****@nspam.comwrites:

"santosh" <sa*********@gmail.comwrote in message
news:g1**********@registered.motzarella.org…

>Not necessarily. It’s just a byte with value zero.

I wrote this program in my djgpp compile not my gcc one and got the
ascii value of 48.

printf("%i\n",’0′);

‘0’ and ‘\0’ are two very different things.


Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
— Antony Jay and Jonathan Lynn, "Yes Minister"

A(Answer):

Bill Cunningham wrote:

>

…. snip …

>
I wrote this program in my djgpp compile not my gcc one and got
the ascii value of 48.

The DJGPP compiler IS gcc.


[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **

A(Answer):

On Sat, 31 May 2008 16:16:25 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:

>
"Joachim Schmitz" <no*********@schmitz-digital.dewrote in message
news:g1**********@online.de…

[snip]

>>>Still needs some error checking (num>=0, fp != NULL, fclose and fput
worked)

I can get that part. I think.

Yep, left as an excercise to the reader 😎

Now here’s what I tried in error checking that gave me a warning. I
must’ve messed up.

if((fputc(a,fp))!=NULL)
if((fclose(fp))!=NULL)

What is the return type of fputc and fclose? What is the type of
NULL? Is there any implicit conversion between the two types? Why do
you think either function returns NULL on error? Do you have a
reference text of any kind? Have you downloaded any of the free
standard drafts (they all provide complete prototypes for every
standard function)? Somewhere along the line you determined that
fputc takes an int and a FILE*. How come you cannot use the same
source to determine its return type?

>
comparing argument with a without a cast or something like that the compiler

What do you mean "something like that"? You are not a novice poster,
regardless of your skill at C. What is so prohibitively difficult
that you cannot cut and paste the text of the diagnostic into your
message when asking for help?

>said. Without cast and comparing was mentioned by the compiler but it
compiled and didn’t work. So I tried this.

If it produced a diagnostic that you don’t understand, it did not
compile cleanly.

>
if((fputc(a,fp))==NULL)
puts("fputc error");

When I ran the binary fputc error was mentioned 10 times. My 2nd arg was 10.

Your 2nd arg of what? Not fputc, the 2nd arg there is fp, not 10. Not
puts, it only has one arg.

>
Bill

Remove del for email

A(Answer):

"CBFalconer" <cb********@yahoo.comwrote in message
news:48***************@yahoo.com…

The DJGPP compiler IS gcc.

Oh yes sorry it is. I mean my windows environment compiler not my linux
gcc which I use mostly.

Bill

A(Answer):

Barry Schwarz wrote:

On Sat, 31 May 2008 16:16:25 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:

<snip>

>>
if((fputc(a,fp))==NULL)
puts("fputc error");

When I ran the binary fputc error was mentioned 10 times. My 2nd arg
was 10.

Your 2nd arg of what? Not fputc, the 2nd arg there is fp, not 10. Not
puts, it only has one arg.

2nd arg to his program, AKA argv[2], obviously (if you look upthread), the
number of null bytes to writen to a file (who’s name is given in argv[1])

Bye, JOjo

A(Answer):

"Bill Cunningham" <no****@nspam.comwrites:

"santosh" <sa*********@gmail.comwrote in message
news:g1**********@registered.motzarella.org…

>Not necessarily. It’s just a byte with value zero.

I wrote this program in my djgpp compile not my gcc one and got the
ascii value of 48.

printf("%i\n",’0′);

Bill

Come on. Please stop trolling.

A(Answer):

On Sun, 1 Jun 2008 12:13:19 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote:

>Barry Schwarz wrote:

>On Sat, 31 May 2008 16:16:25 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:

<snip>

>>>
if((fputc(a,fp))==NULL)
puts("fputc error");

When I ran the binary fputc error was mentioned 10 times. My 2nd arg
was 10.

Your 2nd arg of what? Not fputc, the 2nd arg there is fp, not 10. Not
puts, it only has one arg.

2nd arg to his program, AKA argv[2], obviously (if you look upthread), the
number of null bytes to writen to a file (who’s name is given in argv[1])

If it is relevant to the current message, it should be quoted in the
current message. Do you really keep old messages around to resolve
unspecified references? Does you news server always deliver messages
in the proper order?
Remove del for email

LEAVE A COMMENT