473,503 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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?

May 22 '07 #1
9 3150
In article <11**********************@p77g2000hsh.googlegroups .com>,
hstagni <st****@gmail.comwrote:
>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.

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
May 22 '07 #2
hstagni <st****@gmail.comwrites:
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.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup
May 22 '07 #3
On 22 May 2007 14:20:39 -0700, hstagni <st****@gmail.comwrote:
>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
May 23 '07 #4
hstagni wrote:
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
May 23 '07 #5
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

May 23 '07 #6
hstagni wrote:
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?
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`.

--
"He's dead, Jim, but not as we know it." Unsaid /Trek/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

May 23 '07 #7
hstagni wrote:
>
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:Th*************@gmail.com>
May 23 '07 #8
hstagni <st****@gmail.comwrites:
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?
Have you read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 23 '07 #9
hstagni wrote:
>
.... snip ...
>
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?
You should use int to receive from fgetc, getc, getchar, etc. all
of which return an int. This is to allow them to return EOF (which
may not be -1, all you know is that it will be negative).

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 24 '07 #10

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

Similar topics

9
10876
by: Scott Beavers | last post by:
I'm trying to create a form in Excel to sort from the form and take the data to another worksheet. I am very new to this and any help would be appreciated. I have a value in a cell that will...
7
5066
by: google12 | last post by:
hi, May anyone help me? I need a lib which support .jpeg file with RGBA mode. PIL seems to support RGB and CMYK mode only... Thanks for your help.
40
2987
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
145
6148
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
8
161610
by: John Brock | last post by:
I am creating an Excel workbook using VB.NET, and have run into a problem. Excel at times insists on reformatting data that I enter into cells, e.g., converting "01234" to "1234", and this screws...
20
1882
by: Frank Millman | last post by:
Hi all This is probably old hat to most of you, but for me it was a revelation, so I thought I would share it in case someone has a similar requirement. I had to convert an old program that...
5
3522
eragon
by: eragon | last post by:
I wrote this function to create a new file when the user posts in my forums, and its not creating a new file, can you help me? this script is not copyrighted as the last one. function...
8
5594
by: VijaKhara | last post by:
Hi all, Please tell me how to create a file (using fopen) in a sub-folder (such as sub-folder "Mygoal")? I tried if ( ( fp = fopen ( "mygoal\myfile.tif", "wb" ) ) == NULL ) { fprintf (...
7
9385
by: dieter | last post by:
Hi, Overview ======= I'm doing some simple file manipulation work and the process gets "Killed" everytime I run it. No traceback, no segfault... just the word "Killed" in the bash shell and...
0
7194
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
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.