PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > function
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
function

Réponse
 
LinkBack Outils de la discussion
Vieux 31/05/2008, 15h47   #1
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut function

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


  Réponse avec citation
Vieux 31/05/2008, 16h04   #2
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function

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 file> count=<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


  Réponse avec citation
Vieux 31/05/2008, 16h10   #3
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function


"Joachim Schmitz" <nospam.jojo@schmitz-digital.de> wrote in message
news:g1rpes$mp7$1@online.de...
> [OT]
> dd if=/dev/zero of=<your file> count=<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
>



  Réponse avec citation
Vieux 31/05/2008, 16h14   #4
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function


"Joachim Schmitz" <nospam.jojo@schmitz-digital.de> wrote in message
news:g1rpes$mp7$1@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


  Réponse avec citation
Vieux 31/05/2008, 16h14   #5
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function

Bill Cunningham wrote:
> "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> wrote in message
> news:g1rpes$mp7$1@online.de...
>> [OT]
>> dd if=/dev/zero of=<your file> count=<your size>
>> [/OT]

>
> Ah. The easy way out

Jep 8-) 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 8-)

>>> Bill

>> Bye, Jojo



  Réponse avec citation
Vieux 31/05/2008, 16h17   #6
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function

Bill Cunningham wrote:
> "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> wrote in message
> news:g1rpes$mp7$1@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


  Réponse avec citation
Vieux 31/05/2008, 17h16   #7
Bill Cunningham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function


"Joachim Schmitz" <nospam.jojo@schmitz-digital.de> wrote in message
news:g1rq2v$nf9$1@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 8-)


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


  Réponse avec citation
Vieux 31/05/2008, 17h34   #8
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: function

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 **
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h26.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16499 seconds with 16 queries