fwrite don't work with me

Discussion in 'C Programming' started by Medvedev, Jul 1, 2008.

  1. Medvedev

    Medvedev Guest

    FILE *a,*b;
    int z;
    char str1[2];
    char str2[2];

    a= fopen(argv[0],"rb"); //Both files are EXE's
    b= fopen(argv[1],"rb+");

    z= fread(str1,1,1,a);
    if (z !=1)
    printf("-101");

    z= fread(str2,1,1,b);
    if (z !=1)
    printf("-102");

    if(str1 == str2)
    {
    fwrite("a",1,1,b); // here it don't change the first
    byte of the entered file "b" why!
    printf("OK");
    }else{
    fwrite(str1,1,1,b);
    printf("NO");
    }
    fclose(a);
    fclose(b);
    Medvedev, Jul 1, 2008
    #1
    1. Advertising

  2. Medvedev

    Guest

    On Jul 1, 10:48 pm, Medvedev <> wrote:
    > FILE *a,*b;
    > int z;
    > char str1[2];
    > char str2[2];
    >
    > a= fopen(argv[0],"rb"); //Both files are EXE's
    > b= fopen(argv[1],"rb+");
    >
    > z= fread(str1,1,1,a);
    > if (z !=1)
    > printf("-101");
    >
    > z= fread(str2,1,1,b);
    > if (z !=1)
    > printf("-102");
    >
    > if(str1 == str2)
    > {
    > fwrite("a",1,1,b); // here it don't change the first
    > byte of the entered file "b" why!

    Because the file pointer is moved from the previous fread() call.

    That code is poor - if you want more details on your code post it all
    without main() etc snipped.
    , Jul 1, 2008
    #2
    1. Advertising

  3. Medvedev

    Medvedev Guest

    On Jul 1, 12:53 pm, wrote:
    > On Jul 1, 10:48 pm, Medvedev <> wrote:
    >
    > > FILE *a,*b;
    > > int z;
    > > char str1[2];
    > > char str2[2];

    >
    > > a= fopen(argv[0],"rb"); //Both files are EXE's
    > > b= fopen(argv[1],"rb+");

    >
    > > z= fread(str1,1,1,a);
    > > if (z !=1)
    > > printf("-101");

    >
    > > z= fread(str2,1,1,b);
    > > if (z !=1)
    > > printf("-102");

    >
    > > if(str1 == str2)
    > > {
    > > fwrite("a",1,1,b); // here it don't change the first
    > > byte of the entered file "b" why!

    >
    > Because the file pointer is moved from the previous fread() call.
    >
    > That code is poor - if you want more details on your code post it all
    > without main() etc snipped.


    here is the full code "i fix some bugs and it works well but why i am
    supposed to reopen the file to write to it"
    as i can't just call b= fopen(argv[1],"rb+");
    to read and write to the file , why

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>

    using namespace std;

    int main(int argc , char *argv[])
    {
    FILE *a,*b;
    char str1[1];
    char str2[1];

    a= fopen(argv[0],"rb");
    b= fopen(argv[1],"rb");

    fread(str1,1,1,a);
    fread(str2,1,1,b);
    b= freopen(argv[1],"rb+",b); // why i must do that
    // i can't just call b=
    fopen(argv[1],"rb+");
    // in the first because i didn't work
    if(str1[0] == str2[0])
    {
    fputc(11,b);//("t",1,1,b);
    printf("OK");
    }else{
    fwrite(str1,1,1,b);
    printf("NO");
    }

    fclose(a);
    fclose(b);
    system("PAUSE");
    return 0;
    }

    and what do u mean by saying "Because the file pointer is moved from
    the previous fread() call. "
    Medvedev, Jul 1, 2008
    #3
  4. Medvedev

    Guest

    Medvedev wrote:
    > char str1[2];
    > char str2[2];


    [...]

    > if(str1 == str2)


    str1 is a char[];
    str2 is another char[].
    In the expression above, their addresses are being compared
    and they will *never* be equal.

    (str1 == str2)
    is *always* false.

    What can be equal is their contents:
    (str1[0] == str2[0])

    Peruse the C-FAQ ( http://c-faq.com/ ),
    especially the Arrays and Pointers section ( http://c-faq.com/aryptr/index.html
    ).
    , Jul 1, 2008
    #4
  5. Medvedev

    Guest

    Medvedev wrote:
    > #include <cstdio>
    > #include <cstring>
    > #include <cstdlib>
    >
    > using namespace std;


    Oops ... C++ is discussed in the room next door to your left.
    news:comp.lang.c++
    , Jul 1, 2008
    #5
  6. Medvedev

    Medvedev Guest

    On Jul 1, 1:14 pm, wrote:
    > Medvedev wrote:
    > > #include <cstdio>
    > > #include <cstring>
    > > #include <cstdlib>

    >
    > > using namespace std;

    >
    > Oops ... C++ is discussed in the room next door to your left.
    > news:comp.lang.c++


    i fixed that , i need to know why i am supposed to reopen the file
    instead of opening it with the mode "rb+" to read and write
    Medvedev, Jul 1, 2008
    #6
  7. Medvedev

    Medvedev Guest

    On Jul 1, 1:31 pm, Eric Sosman <> wrote:
    > Medvedev wrote:
    > > FILE *a,*b;
    > > int z;
    > > char str1[2];
    > > char str2[2];

    >
    > > a= fopen(argv[0],"rb"); //Both files are EXE's
    > > b= fopen(argv[1],"rb+");

    >
    > > z= fread(str1,1,1,a);
    > > if (z !=1)
    > > printf("-101");

    >
    > > z= fread(str2,1,1,b);
    > > if (z !=1)
    > > printf("-102");

    >
    > > if(str1 == str2)

    >
    > Note that this will never be true. See Question 8.2 in
    > the comp.lang.c Frequently Asked Questions (FAQ) list at
    > <http://www.c-faq.com/>.
    >
    > > {
    > > fwrite("a",1,1,b); // here it don't change the first
    > > byte of the entered file "b" why!

    >
    > Because this line is never executed.
    >
    > --
    >


    watch in the fixed code , u will get "OK" but the file will never
    changed unless you reopen it as in the fixed code
    WHY i must reopen it to write to it instead of calling
    b= fopen(argv[1],"rb+");
    to read and write to the file
    Medvedev, Jul 1, 2008
    #7
  8. Medvedev

    Medvedev Guest

    On Jul 1, 1:37 pm, Medvedev <> wrote:
    > On Jul 1, 1:31 pm, Eric Sosman <> wrote:
    >
    >
    >
    > > Medvedev wrote:
    > > > FILE *a,*b;
    > > > int z;
    > > > char str1[2];
    > > > char str2[2];

    >
    > > > a= fopen(argv[0],"rb"); //Both files are EXE's
    > > > b= fopen(argv[1],"rb+");

    >
    > > > z= fread(str1,1,1,a);
    > > > if (z !=1)
    > > > printf("-101");

    >
    > > > z= fread(str2,1,1,b);
    > > > if (z !=1)
    > > > printf("-102");

    >
    > > > if(str1 == str2)

    >
    > > Note that this will never be true. See Question 8.2 in
    > > the comp.lang.c Frequently Asked Questions (FAQ) list at
    > > <http://www.c-faq.com/>.

    >
    > > > {
    > > > fwrite("a",1,1,b); // here it don't change the first
    > > > byte of the entered file "b" why!

    >
    > > Because this line is never executed.

    >
    > > --
    > >

    >
    > watch in the fixed code , u will get "OK" but the file will never
    > changed unless you reopen it as in the fixed code
    > WHY i must reopen it to write to it instead of calling
    > b= fopen(argv[1],"rb+");
    > to read and write to the file


    i get it , i must call fflush() between fread and fwrite
    thanks allllllllllllll
    Medvedev, Jul 1, 2008
    #8
  9. Medvedev <> wrote:
    > On Jul 1, 12:53 pm, wrote:
    > > On Jul 1, 10:48 pm, Medvedev <> wrote:
    > >
    > > > FILE *a,*b;
    > > > int z;
    > > > char str1[2];
    > > > char str2[2];

    > >
    > > > a= fopen(argv[0],"rb"); //Both files are EXE's
    > > > b= fopen(argv[1],"rb+");

    > >
    > > > z= fread(str1,1,1,a);
    > > > if (z !=1)
    > > > printf("-101");

    > >
    > > > z= fread(str2,1,1,b);
    > > > if (z !=1)
    > > > printf("-102");

    > >
    > > > if(str1 == str2)


    Luckily, you already corrected this mistake.

    > > > {
    > > > fwrite("a",1,1,b); // here it don't change the first
    > > > byte of the entered file "b" why!

    > >
    > > Because the file pointer is moved from the previous fread() call.


    > here is the full code "i fix some bugs and it works well but why i am
    > supposed to reopen the file to write to it"
    > as i can't just call b= fopen(argv[1],"rb+");
    > to read and write to the file , why


    If you read a byte then you're at the second byte in the file. And
    if you then write you write to this position, i.e. you overwrite the
    second byte in the file, not the first. One way to get around that
    is to close the file and then open it anew. You can do that by cal-
    ling first fclose() and then fopen(). Or you can do it in a single
    step by calling freopen(). But you can also move back in the file by
    using the fseek() function. There are several ways to skin the cat;-)

    > if(str1[0] == str2[0])
    > {
    > fputc(11,b);//("t",1,1,b);


    This comment is misleading. What has the string "t" got to do with
    the number 11? You're rather likely putting a '\v' character into
    the file here (assuming that your system uses ASCII).

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Jul 1, 2008
    #9
  10. Medvedev wrote:

    > watch in the fixed code , u will get "OK" but the file will never
    > changed unless you reopen it as in the fixed code
    > WHY i must reopen it to write to it instead of calling
    > b= fopen(argv[1],"rb+");
    > to read and write to the file


    i won't comment on u's observation (you did not introduce him yet),
    but here is what the standard says.

    | 7.9.13
    | [...] If a file can support positioning requests (such as a disk file,
    | as opposed to a terminal), then a file position indicator associated
    | with the stream is positioned at the start (character number zero) of
    | the file, unless [...]. The file position indicator is maintained by
    | subsequent reads, writes, and positioning requests, to facilitate an
    | orderly progression through the file.

    I think this should suffice for a quick overview, though it is of
    course also explicated in the fread/fgetc and fwrite/fputc
    paragraphs. For navigation in streams there are the rewind(), the
    fgetpos()/fsetpos() and the ftell()/fseek() functions. For your
    use on binary streams you only need the fseek() function.

    Ralf
    Ralf Damaschke, Jul 1, 2008
    #10
  11. Medvedev

    rahul Guest

    On Jul 2, 1:55 am, Medvedev <> wrote:
    > On Jul 1, 1:37 pm, Medvedev <> wrote:
    >
    >
    >
    > > On Jul 1, 1:31 pm, Eric Sosman <> wrote:

    >
    > > > Medvedev wrote:
    > > > > FILE *a,*b;
    > > > > int z;
    > > > > char str1[2];
    > > > > char str2[2];

    >
    > > > > a= fopen(argv[0],"rb"); //Both files are EXE's
    > > > > b= fopen(argv[1],"rb+");

    >
    > > > > z= fread(str1,1,1,a);
    > > > > if (z !=1)
    > > > > printf("-101");

    >
    > > > > z= fread(str2,1,1,b);
    > > > > if (z !=1)
    > > > > printf("-102");

    >
    > > > > if(str1 == str2)

    >
    > > > Note that this will never be true. See Question 8.2 in
    > > > the comp.lang.c Frequently Asked Questions (FAQ) list at
    > > > <http://www.c-faq.com/>.

    >
    > > > > {
    > > > > fwrite("a",1,1,b); // here it don't change the first
    > > > > byte of the entered file "b" why!

    >
    > > > Because this line is never executed.

    >
    > > > --
    > > >

    >
    > > watch in the fixed code , u will get "OK" but the file will never
    > > changed unless you reopen it as in the fixed code
    > > WHY i must reopen it to write to it instead of calling
    > > b= fopen(argv[1],"rb+");
    > > to read and write to the file

    >
    > i get it , i must call fflush() between fread and fwrite
    > thanks allllllllllllll


    When you open a file in read/write mode, read and write calls can not
    be consecutive without any intervening fflush() or fseek() call. Also,
    remember that the there is a single file pointer associated with an
    open file. So, both read/write calls are working on the same pointer.
    Don't expect different pointers for read and write calls.
    rahul, Jul 2, 2008
    #11
  12. On 1 Jul, 21:02, Medvedev <> wrote:
    > On Jul 1, 12:53 pm, wrote:
    > > On Jul 1, 10:48 pm, Medvedev <> wrote:


    <snip>

    > here is the full code "i fix some bugs and it works well but why i am
    > supposed to reopen the file to write to it"


    why is that in quotes? (")

    > as i can't just call b= fopen(argv[1],"rb+");
    > to read and write to the file , why
    >
    > #include <cstdio>
    > #include <cstring>
    > #include <cstdlib>
    >
    > using namespace std;


    that is C++
    I'll assume you meant to write C


    > int main(int argc , char *argv[])
    > {
    >         FILE *a,*b;
    >     char str1[1];
    >     char str2[1];


    what is the point of an array of length 1?


    >     a= fopen(argv[0],"rb");
    >     b= fopen(argv[1],"rb");


    no check that argv[0] and argv[1] are not NULL.
    no check that the files opened successfully.
    argv[0] is usually the current program. Did you mean that?


    >     fread(str1,1,1,a);
    >     fread(str2,1,1,b);


    no check on how many characters were read

    >     b= freopen(argv[1],"rb+",b); // why i must do that
    >                                  // i can't just call b=


    no check if the fopen succeeded

    the fread() advances the file pointer. Also you opened
    the file to be read only then tried to write to it.

    C90 (the most widly supported version of C) does not support //
    comments.



    > fopen(argv[1],"rb+");


    can you open a file twice? Where did you store the return value?
    Ah, this is another reason to use /* */ comments...


    > // in the first because i didn't work


    I don't understand what you mean


    >     if(str1[0] == str2[0])
    >     {
    >         fputc(11,b);//("t",1,1,b);


    what?


    >         printf("OK");
    >     }else{
    >         fwrite(str1,1,1,b);
    >         printf("NO");
    >     }
    >
    >     fclose(a);
    >     fclose(b);
    >     system("PAUSE");
    >     return 0;
    >
    > }
    >
    > and what do u mean by saying "Because the file pointer is moved from
    > the previous fread() call.


    fread() advances the file pointer. What are you trying to do?

    Oh my god its slavic bill...


    --
    Nick Keighley
    Nick Keighley, Jul 2, 2008
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nescio
    Replies:
    0
    Views:
    444
    nescio
    Dec 21, 2005
  2. nescio

    fwrite() question

    nescio, Dec 21, 2005, in forum: HTML
    Replies:
    0
    Views:
    356
    nescio
    Dec 21, 2005
  3. seia0106
    Replies:
    3
    Views:
    3,111
    Jack Klein
    Jul 18, 2003
  4. Zalek Bloom
    Replies:
    2
    Views:
    734
    llewelly
    Sep 9, 2003
  5. michelle

    transfer file using recv() and fwrite()

    michelle, Jun 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    3,575
    michelle
    Jun 26, 2003
Loading...

Share This Page