473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

fwrite don't work with me

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);
Jul 1 '08 #1
12 3140
On Jul 1, 10:48 pm, Medvedev <3D.v.Wo...@gmail.comwrote:
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 '08 #2
On Jul 1, 12:53 pm, vipps...@gmail.com wrote:
On Jul 1, 10:48 pm, Medvedev <3D.v.Wo...@gmail.comwrote:
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. "
Jul 1 '08 #3
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 '08 #4
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 '08 #5
On Jul 1, 1:14 pm, badc0...@gmail.com 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
Jul 1 '08 #6
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.

--
Er*********@sun.com
Jul 1 '08 #7
On Jul 1, 1:31 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
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.

--
Eric.Sos...@sun.com
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
Jul 1 '08 #8
On Jul 1, 1:37 pm, Medvedev <3D.v.Wo...@gmail.comwrote:
On Jul 1, 1:31 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
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.
--
Eric.Sos...@sun.com

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
Jul 1 '08 #9
Medvedev <3D********@gmail.comwrote:
On Jul 1, 12:53 pm, vipps...@gmail.com wrote:
On Jul 1, 10:48 pm, Medvedev <3D.v.Wo...@gmail.comwrote:
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 ___ jt@toerring.de
\__________________________ http://toerring.de
Jul 1 '08 #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
Jul 1 '08 #11
On Jul 2, 1:55 am, Medvedev <3D.v.Wo...@gmail.comwrote:
On Jul 1, 1:37 pm, Medvedev <3D.v.Wo...@gmail.comwrote:
On Jul 1, 1:31 pm, Eric Sosman <Eric.Sos...@sun.comwrote:
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.
--
Eric.Sos...@sun.com
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.
Jul 2 '08 #12
On 1 Jul, 21:02, Medvedev <3D.v.Wo...@gmail.comwrote:
On Jul 1, 12:53 pm, vipps...@gmail.com wrote:
On Jul 1, 10:48 pm, Medvedev <3D.v.Wo...@gmail.comwrote:
<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

Jul 2 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tuuska | last post by:
Hello! I have this problem, when fwrite() writes to the beginning of xyz.txt file, it overwrites the first line. Any ideas how to prevent this? I'm running PHP 4.3.3RC3 on Linux. <?php $fp =...
3
by: Antoine Bloncourt | last post by:
Hello everybody Sorry to bother you but I have a problem writing datas into a file ... I want to make a backup of my MySQL database and put the result into a ..sql file. To do this, I use...
4
by: Andrew Kibler | last post by:
Two sections of code, in the first one fwrite works, in the second one it doesn't (ms VC++) both are identical except in the working one fseek is used twice to set the file pointer, once just...
23
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName);...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
2
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
30
by: empriser | last post by:
How to use fread/fwrite copy a file. When reach file's end, fread return 0, I don't konw how many bytes in buf.
19
by: pal | last post by:
Hi all, Could you help me how to use the fwrite( ) in C ++
25
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.