Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fwrite don't work with me

Reply
Thread Tools

fwrite don't work with me

 
 
Medvedev
Guest
Posts: n/a
 
      07-01-2008
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);
 
Reply With Quote
 
 
 
 
vippstar@gmail.com
Guest
Posts: n/a
 
      07-01-2008
On Jul 1, 10:48 pm, Medvedev <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
Medvedev
Guest
Posts: n/a
 
      07-01-2008
On Jul 1, 12:53 pm, (E-Mail Removed) wrote:
> On Jul 1, 10:48 pm, Medvedev <(E-Mail Removed)> 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. "
 
Reply With Quote
 
badc0de4@gmail.com
Guest
Posts: n/a
 
      07-01-2008
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
).
 
Reply With Quote
 
badc0de4@gmail.com
Guest
Posts: n/a
 
      07-01-2008
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++
 
Reply With Quote
 
Medvedev
Guest
Posts: n/a
 
      07-01-2008
On Jul 1, 1:14 pm, (E-Mail Removed) 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
 
Reply With Quote
 
Medvedev
Guest
Posts: n/a
 
      07-01-2008
On Jul 1, 1:31 pm, Eric Sosman <(E-Mail Removed)> 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.
>
> --
> (E-Mail Removed)


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
 
Reply With Quote
 
Medvedev
Guest
Posts: n/a
 
      07-01-2008
On Jul 1, 1:37 pm, Medvedev <(E-Mail Removed)> wrote:
> On Jul 1, 1:31 pm, Eric Sosman <(E-Mail Removed)> 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.

>
> > --
> > (E-Mail Removed)

>
> 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
 
Reply With Quote
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      07-01-2008
Medvedev <(E-Mail Removed)> wrote:
> On Jul 1, 12:53 pm, (E-Mail Removed) wrote:
> > On Jul 1, 10:48 pm, Medvedev <(E-Mail Removed)> 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://www.velocityreviews.com/forums/(E-Mail Removed)
\__________________________ http://toerring.de
 
Reply With Quote
 
Ralf Damaschke
Guest
Posts: n/a
 
      07-01-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fwrite() question nescio HTML 0 12-21-2005 10:02 AM
fwrite() and length of string problem nescio HTML 0 12-21-2005 09:50 AM
Newbie: can I rewrite data with fwrite? Zalek Bloom C++ 2 09-09-2003 03:30 AM
Question about fwrite() function seia0106 C++ 3 07-18-2003 07:05 AM
transfer file using recv() and fwrite() michelle C Programming 2 06-26-2003 05:17 PM



Advertisments