problem writing to file 
Author Message
 problem writing to file

hello,

I have the following code:

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

/* print decimal to outfile in binary format */
void *printBinary(int val, char *filename)
{
 FILE *outfile;
 unsigned int usc = (unsigned int)val;
 unsigned int mask = UINT_MAX - (UINT_MAX >> 1);
 static char result [sizeof(int) * CHAR_BIT + 1];
 char *store = result;

 while (mask)
 {
        *store++ = (char)('0' + ((usc & mask) != 0));
        mask >>= 1;
    }
    *store = '\0';

 if((outfile = fopen(filename, "w")) == NULL)
 {
  perror(filename);
  exit(1);
 }
 fprintf(outfile, "%s ", result);
 fclose(outfile);

Quote:
}

int main(int argc, char *argv[])
{
    FILE *fp;
    int c;
    if((fp = fopen(argv[1], "r")) == NULL)
    {
        perror(argv[1]);
        exit(1);
    }
    while((c = fgetc(fp)) != EOF)
    {
         printBinary(c, argv[2]);
    }

Quote:
}

Say that argv[1] is test.txt and argv[2] is test.out.
test.txt contains one string: "test"
this code is supposed to read in test and then print out the binary
equivelent to test in test.out.
However, the only thing in test.out is the binary code for the last
letter of "test".  If I replace  fprintf(outfile, "%s ", result); in
printBinary() with  fprintf(stdout, "%s ", result);, it prints "test" in
binary... why does it print it correctly to stdout and not test.out?

thanks very much for the help,
Aaron



Fri, 28 Dec 2001 03:00:00 GMT  
 problem writing to file

Quote:

> hello,
> I have the following code:
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <limits.h>
> /* print decimal to outfile in binary format */
> void *printBinary(int val, char *filename)
> {
>  FILE *outfile;
>  unsigned int usc = (unsigned int)val;
>  unsigned int mask = UINT_MAX - (UINT_MAX >> 1);
>  static char result [sizeof(int) * CHAR_BIT + 1];
>  char *store = result;
>  while (mask)
>  {
>         *store++ = (char)('0' + ((usc & mask) != 0));
>         mask >>= 1;
>     }
>     *store = '\0';
>  if((outfile = fopen(filename, "w")) == NULL)

Since you're using the "w" mode, any data already in the file is discarded
when you call fopen().  If you use "a" instead, it will work better.
Considering that opening and/or creating a file can be an expensive
operation, it would make even more sense to open the file only once by
doing something like this:

static FILE *outfile = NULL;
...
if (outfile == NULL && (outfile = fopen(filename, "w")) == NULL)
/* handle file opening error */

Others will undoubtedly suggest different approaches, such as opening the
file in the main program and letting the function handle just the writing
of data.

--
Eric Amick
Columbia, MD



Fri, 28 Dec 2001 03:00:00 GMT  
 problem writing to file
Thanks very much for your help.  I originally had it set to append, but
changed it.  I didn't think that I was calling printBinary() for every letter,
there for it was rewriting itself over and over.

thanks again,
Aaron

Quote:


> > hello,

> > I have the following code:

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

> > /* print decimal to outfile in binary format */
> > void *printBinary(int val, char *filename)
> > {
> >  FILE *outfile;
> >  unsigned int usc = (unsigned int)val;
> >  unsigned int mask = UINT_MAX - (UINT_MAX >> 1);
> >  static char result [sizeof(int) * CHAR_BIT + 1];
> >  char *store = result;

> >  while (mask)
> >  {
> >         *store++ = (char)('0' + ((usc & mask) != 0));
> >         mask >>= 1;
> >     }
> >     *store = '\0';

> >  if((outfile = fopen(filename, "w")) == NULL)

> Since you're using the "w" mode, any data already in the file is discarded
> when you call fopen().  If you use "a" instead, it will work better.
> Considering that opening and/or creating a file can be an expensive
> operation, it would make even more sense to open the file only once by
> doing something like this:

> static FILE *outfile = NULL;
> ...
> if (outfile == NULL && (outfile = fopen(filename, "w")) == NULL)
> /* handle file opening error */

> Others will undoubtedly suggest different approaches, such as opening the
> file in the main program and letting the function handle just the writing
> of data.

> --
> Eric Amick
> Columbia, MD




Fri, 28 Dec 2001 03:00:00 GMT  
 problem writing to file
<big snip>

Quote:
> if((outfile = fopen(filename, "w")) == NULL)

This is your problem. From my fopen man page:
       w      Truncate  file  to  zero length or create text file
              for writing.   The  stream  is  positioned  at  the
              beginning of the file.

You'll need to either use append mode or (better) open the file once and
write it all at once. And next time you post a question, please post the
shortest possible program that misbehaves; it might help you find the
bug yourself, and everybody else will have less code to read through.

<snip>

Quote:
>However, the only thing in test.out is the binary code for the last
>letter of "test".  If I replace  fprintf(outfile, "%s ", result); in
>printBinary() with  fprintf(stdout, "%s ", result);, it prints "test" in
>binary... why does it print it correctly to stdout and not test.out?

Gergo

--
Rule of Creative Research:
        (1) Never draw what you can copy.
        (2) Never copy what you can trace.
        (3) Never trace what you can cut out and paste down.

GU d- s:+ a--- C++>$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+



Fri, 28 Dec 2001 03:00:00 GMT  
 problem writing to file
this was the shortest working code.  You should see the actual code... it's
several hundred lines of code.  I narrowed it down to that certain function
and the main() function.

thanks for your help,
Aaron

Quote:


> <big snip>
> > if((outfile = fopen(filename, "w")) == NULL)

> This is your problem. From my fopen man page:
>        w      Truncate  file  to  zero length or create text file
>               for writing.   The  stream  is  positioned  at  the
>               beginning of the file.

> You'll need to either use append mode or (better) open the file once and
> write it all at once. And next time you post a question, please post the
> shortest possible program that misbehaves; it might help you find the
> bug yourself, and everybody else will have less code to read through.

> <snip>

> >However, the only thing in test.out is the binary code for the last
> >letter of "test".  If I replace  fprintf(outfile, "%s ", result); in
> >printBinary() with  fprintf(stdout, "%s ", result);, it prints "test" in
> >binary... why does it print it correctly to stdout and not test.out?

> Gergo

> --
> Rule of Creative Research:
>         (1) Never draw what you can copy.
>         (2) Never copy what you can trace.
>         (3) Never trace what you can cut out and paste down.

> GU d- s:+ a--- C++>$ UL+++ P>++ L+++ E>++ W+ N++ o? K- w--- !O !M !V
> PS+ PE+ Y+ PGP+ t* 5+ X- R>+ tv++ b+>+++ DI+ D+ G>++ e* h! !r !y+



Fri, 28 Dec 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Problem opening a file for read while it is open for write

2. File Read/Write Problem

3. problems writing to binary files

4. Problems with writing to a file

5. help - write to file in interrupt problems

6. Q: problem: CGI-programming with C (writing files and sending picyures)

7. PROBLEM READING/WRITING FILE __SIMULTANEOUSLY__!

8. File reading/writing problem

9. Problem with NT and writing large files

10. Problem writing to a writable file

11. file reading/writing speed problem

12. Problems with MFC-Internet Functions, writing a File

 

 
Powered by phpBB® Forum Software