How to seek in a file "wb"?????

H

hstagni

I tried to use fseek in a file opened for writing:
------ BEGIN -------
int main()
{
char c;
FILE *fp;
fp=fopen("texto", "wb");
putc('a', fp);
putc('b', fp);


fseek(fp, -1, SEEK_CUR);
c=getc(fp); printf("%c", c);

fclose(fp);
return 0;
}
------END--------

However, the result of "printf" was the EOF char and I was expecting a
"b".
I know the problem is in fseek, because it doesn´t work(how i wish it
works) on files opened for writing.

THE QUESTION IS:
WHAT SHOULD I DO TO MAKE THE SAMPLE ABOVE WORK?
 
W

Walter Roberson

I tried to use fseek in a file opened for writing:
------ BEGIN -------
int main()
{
char c;
FILE *fp;
fp=fopen("texto", "wb");
putc('a', fp);
putc('b', fp);


fseek(fp, -1, SEEK_CUR);
c=getc(fp); printf("%c", c);

fclose(fp);
return 0;
}
------END--------
However, the result of "printf" was the EOF char and I was expecting a
"b".
I know the problem is in fseek, because it doesn'tt work(how i wish it
works) on files opened for writing.
THE QUESTION IS:
WHAT SHOULD I DO TO MAKE THE SAMPLE ABOVE WORK?

Change the "wb" to "wb+" so that you are in update mode instead
of write mode.
 
B

Ben Pfaff

hstagni said:
I tried to use fseek in a file opened for writing: [...]
fp=fopen("texto", "wb"); [...]
c=getc(fp); printf("%c", c);

Open the file for reading and writing (mode "w+b"). You can't
expect reading to work on a file opened only for writing.
 
J

JimS

I tried to use fseek in a file opened for writing:
------ BEGIN -------
int main()
{
char c;
FILE *fp;
fp=fopen("texto", "wb");
putc('a', fp);
putc('b', fp);


fseek(fp, -1, SEEK_CUR);
c=getc(fp); printf("%c", c);

fclose(fp);
return 0;
}
------END--------

However, the result of "printf" was the EOF char

Unlikely, because you've declared c as char instead of int.

Jim
 
M

Martin Ambuhl

hstagni said:
I tried to use fseek in a file opened for writing:
------ BEGIN -------
int main()
{
char c;
FILE *fp;
fp=fopen("texto", "wb");
putc('a', fp);
putc('b', fp);


fseek(fp, -1, SEEK_CUR);
c=getc(fp); printf("%c", c);

fclose(fp);
return 0;
}
------END--------

However, the result of "printf" was the EOF char and I was expecting a
"b".
I know the problem is in fseek, because it doesn´t work(how i wish it
works) on files opened for writing.

THE QUESTION IS:
WHAT SHOULD I DO TO MAKE THE SAMPLE ABOVE WORK?

The printf(..., ftell(fp)) calls have been added so you can see what is
happening. The other three changes (including a needed header, changing
the mode in which the file is opened, ending the last line of output
with an '\n') are all needed.

#include <stdio.h> /* mha: added. Needed for all of FILE,
fopen, putc, fseek, getc, and fclose
in OP's code. */


int main()
{
char c;
FILE *fp;
fp = fopen("texto", "wb+"); /* mha: please note the addition of '+'
*/
printf("After open, ftell -> %ld\n", ftell(fp));
putc('a', fp);
printf("After putc('a',fp), ftell -> %ld\n", ftell(fp));
putc('b', fp);
printf("After putc('b',fp), ftell -> %ld\n", ftell(fp));

fseek(fp, -1, SEEK_CUR);
printf("After fseek(fp, -1, SEEK_CUR), ftell -> %ld\n", ftell(fp));
c = getc(fp);
printf("After getc(fp), ftell -> %ld\n", ftell(fp));
printf("%c\n", c); /* mha: added the necessary '\n' */

fclose(fp);
return 0;
}

After open, ftell -> 0
After putc('a',fp), ftell -> 1
After putc('b',fp), ftell -> 2
After fseek(fp, -1, SEEK_CUR), ftell -> 1
After getc(fp), ftell -> 2
b
 
C

Chris Dollin

hstagni said:
THANKS A LOT

One last question:

Should I use ints to read chars from a file?

If you're using `getc`, you should not that it returns an
`int`, not `char`, and that end-of-file is noted by returning
EOF, which is a value that /isn't/ a (n unsigned) char.
Stuffing the result of `getc` into a `char` will break
somethine.
I know EOF is (-1),

The Standard only requires that it's negative. It need not
be -1. (-1 is a very sensible value, but C implementations
are not required to be sensible.)
but since you have "feof" function, why would I use ints?

Because you weren't using `feof`, and you were using `getc`.
 
H

hstagni

THANKS A LOT

One last question:
Unlikely, because you've declared c as char instead of int.

Should I use ints to read chars from a file? I know EOF is (-1), but
since you have "feof" function, why would I use ints?

thnx
 
K

Kenneth Brody

hstagni said:
I tried to use fseek in a file opened for writing:
------ BEGIN ------- [...]
fp=fopen("texto", "wb");

You have opened the file in write-only mode.

[...]
c=getc(fp); printf("%c", c);

Here, you attempt to read from the write-only file.

[...]
THE QUESTION IS:
WHAT SHOULD I DO TO MAKE THE SAMPLE ABOVE WORK?

The problem has nothing to do with your seek. Rather, it has
to do with attempting to read from a write-only file. You need
to open the file in read-write mode:

fp = fopen("texto","wb+");

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top