|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
"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 > |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
"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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 ** |
|
![]() |
| Outils de la discussion | |
|
|