fwrite don't work with me

M

Medvedev

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);
 
V

vippstar

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.
 
M

Medvedev

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. "
 
B

badc0de4

Medvedev said:
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
).
 
B

badc0de4

Medvedev said:
#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++
 
M

Medvedev

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
 
M

Medvedev

Medvedev said:
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
{
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
 
M

Medvedev

Medvedev said:
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/>.
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
 
J

Jens Thoms Toerring

Medvedev said:
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.
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
 
R

Ralf Damaschke

Medvedev said:
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
 
R

rahul

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.
 
N

Nick Keighley

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...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fread/fwrite 2 18
I can't read the first byte of an EXE file!!!!! 4
URGENT 1
fwrite 10
fwrite() does not write data to file 8
error 28
fwrite() fails when called after fread() 2
Filter sober in c++ don't pass test 0

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top