470,848 Members | 935 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

fopen

I have a question about fopen. I want to append text to a text file
already written. I think this is what I need to do. If I am correct fopen's
second parameter takes only 2 letters. I am using wt. To append text would I
need another call to fopen if my text file isn't already existing consisting
of wa or a+ ? How can I check if an existing text file exists that has been
created before ?

Bill
Jun 27 '08 #1
24 2720
In article <VgA5k.40538$lE3.23882@trnddc05>,
Bill Cunningham <no****@nspam.comwrote:
I have a question about fopen. I want to append text to a text file
already written. I think this is what I need to do. If I am correct fopen's
second parameter takes only 2 letters.
No, there are well-defined three-character sequences.
>I am using wt.
The standard does not define the meaning of 't', only of 'b' (binary).
Text is assumed if binary is not specified.
To append text would I
need another call to fopen if my text file isn't already existing consisting
of wa or a+ ? How can I check if an existing text file exists that has been
created before ?
fopen() with 'a'. ftell() your current position, fseek() to the
beginning of the file, and ftell() again. If the first ftell()
result is the same as the second ftell() then you started out at
the beginning of the file and so there was no existing text.

(Note: If I recall correctly, in some systems when using text streams,
the initial file position is officially considered to be "one past"
the end of file, rather than right -at- the end of file. The writes
still work the same way, but if my memory is correct on this point,
then ftell() on a newly-created text file might not return the
same value as ftell() from the "beginning" of the same file.)
--
"Product of a myriad various minds and contending tongues, compact of
obscure and minute association, a language has its own abundant and
often recondite laws, in the habitual and summary recognition of
which scholarship consists." -- Walter Pater
Jun 27 '08 #2

"Bill Cunningham" <no****@nspam.comschreef in bericht
news:VgA5k.40538$lE3.23882@trnddc05...
I have a question about fopen. I want to append text to a text file
already written. I think this is what I need to do. If I am correct
fopen's second parameter takes only 2 letters. I am using wt. To append
text would I need another call to fopen if my text file isn't already
existing consisting of wa or a+ ?
read your favorite C book and try it out, its in there no matter if its a
good or bad book

Jun 27 '08 #3
On Jun 16, 11:44 pm, "Bill Cunningham" <nos...@nspam.comwrote:
I have a question about fopen. I want to append text to a text file
already written. I think this is what I need to do. If I am correct fopen's
second parameter takes only 2 letters. I am using wt.
People have already told you numerous times 't' is non-standard.
How many more times do you need to be told 't' is non-standard?
Here, once more: 't' is non-standard.

Just in case: 't' *is* *non*-standard.
Jun 27 '08 #4

"Serve Lau" <ni***@qinqin.comwrote in message
news:e8***************************@cache5.tilbu1.n b.home.nl...
read your favorite C book and try it out, its in there no matter if its a
good or bad book
Already checked it. I will check out the mentioned functions. Always
wondered what fpos did. I'll check ftell and such.

Bill
Jun 27 '08 #5

"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:g3**********@canopus.cc.umanitoba.ca...
The standard does not define the meaning of 't', only of 'b' (binary).
Text is assumed if binary is not specified.
[snip]

I heard something about Microsoft and the 't'. If it isn't standard why use
it. What does M$ have to do with this anyway? I just won't use it anymore. I
will work with the functions you mentioned thanks very much.

Bill
Jun 27 '08 #6

"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:g3**********@canopus.cc.umanitoba.ca...

I've tried this. Every new number overwites the last. Here is the code I can
see I don't know how to use fseek.

#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
if (argc != 2)
{
puts ("usage error");
exit (EXIT_FAILURE);
}
double x;
x = strtod (argv[1], NULL);
FILE *fp;
fp = fopen ("data", "wa");
fprintf(fp,"%.2f\n",x);
ftell(fp);
fseek(fp,0,1);
ftell(fp);
fclose (fp);
return 0;
}

What did I do wrong?

Bill
Jun 27 '08 #7

<vi******@gmail.comwrote in message
news:2a**********************************@8g2000hs e.googlegroups.com...
People have already told you numerous times 't' is non-standard.
How many more times do you need to be told 't' is non-standard?
Here, once more: 't' is non-standard.

Just in case: 't' *is* *non*-standard.
Hum don't remember talking about it. I remember something mentioned
about M$ and t. Well no matter. I can see I can count on you for help that
is not needed. Have I not made to your killfile yet? What can I do to help?
You're in mine.

Bill
Jun 27 '08 #8

"Bill Cunningham" <no****@nspam.comwrote in message
news:66D5k.20948$3j2.17956@trnddc03...
>
"Serve Lau" <ni***@qinqin.comwrote in message
news:e8***************************@cache5.tilbu1.n b.home.nl...
You know my book doesn't give return values for these functions for
error checking. What about ftell and fseek do they return -1 or non zero
when successful ?

Bill
Jun 27 '08 #9
Bill Cunningham said:

<snip>
Have I not made to your killfile yet? What can I do
to help? You're in mine.
If he really were in your killfile, you would not have seen his article.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #10

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:_u******************************@bt.com...
If he really were in your killfile, you would not have seen his article.
I use OE. There's no killfile there. I just don't read posts from certain
persons. I know agent has a killfile. There comes a time to not read some
posts. A "killfile".

Bill
Jun 27 '08 #11
Bill Cunningham said:
>
"Bill Cunningham" <no****@nspam.comwrote in message
news:66D5k.20948$3j2.17956@trnddc03...
>>
"Serve Lau" <ni***@qinqin.comwrote in message
news:e8***************************@cache5.tilbu1. nb.home.nl...

You know my book doesn't give return values for these functions for
error checking.
In message <3d********@corp.newsgroups.com(posted on 20 July 2002) you
wrote:

"This little K&R2 book is giving me the devil of a time."

It is not unreasonable to deduce from that statement that you have had a
copy of K&R2 since at least 2002.

Despite your claim, K&R2 *does* document the return value semantics for
fopen, freopen, fflush, fclose, fprintf, fscanf, fgetc, fgets, fputc,
fputs, fread, fwrite, fseek, ftell, fgetpos, and fsetpos - on pp242-248.
What about ftell and fseek do they return -1 or non zero
when successful ?
Read the book.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #12
Bill Cunningham said:
>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:_u******************************@bt.com...
>If he really were in your killfile, you would not have seen his article.
I use OE. There's no killfile there.
Then he can't be in one, can he?
I just don't read posts from certain persons.
If that were true, you would not have read his article, so there would have
been no cause to respond to it.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #13
"Bill Cunningham" <no****@nspam.comwrites:
I have a question about fopen. I want to append text to a text file
already written. I think this is what I need to do. If I am correct fopen's
second parameter takes only 2 letters. I am using wt. To append text would I
need another call to fopen if my text file isn't already existing consisting
of wa or a+ ? How can I check if an existing text file exists that has been
created before ?

Bill
Try reading the man page Bill. I hear it helps.
Jun 27 '08 #14
"Bill Cunningham" <no****@nspam.comwrites:
"Walter Roberson" <ro******@ibd.nrc-cnrc.gc.cawrote in message
news:g3**********@canopus.cc.umanitoba.ca...
>The standard does not define the meaning of 't', only of 'b' (binary).
Text is assumed if binary is not specified.
[snip]

I heard something about Microsoft and the 't'. If it isn't standard why use
it. What does M$ have to do with this anyway? I just won't use it anymore. I
will work with the functions you mentioned thanks very much.

Bill
Alright, give it up Kenny. It's not funny anymore :-)
Jun 27 '08 #15
In article <%GE5k.40579$lE3.1104@trnddc05>,
Bill Cunningham <no****@nspam.comwrote:
fp = fopen ("data", "wa");
I said to use "a", not "wa".
--
"MAMA: Oh--So now it's life. Money is life. Once upon a time freedom
used to be life--now it's money. I guess the world really do change.
WALTER: No--it was always money, Mama. We just didn't know about it."
-- Lorraine Hansberry
Jun 27 '08 #16

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:g8******************************@bt.com...
Bill Cunningham said:
>>
"Bill Cunningham" <no****@nspam.comwrote in message
news:66D5k.20948$3j2.17956@trnddc03...
>>>
"Serve Lau" <ni***@qinqin.comwrote in message
news:e8***************************@cache5.tilbu1 .nb.home.nl...

You know my book doesn't give return values for these functions for
error checking.

In message <3d********@corp.newsgroups.com(posted on 20 July 2002) you
wrote:

"This little K&R2 book is giving me the devil of a time."

It is not unreasonable to deduce from that statement that you have had a
copy of K&R2 since at least 2002.

Despite your claim, K&R2 *does* document the return value semantics for
fopen, freopen, fflush, fclose, fprintf, fscanf, fgetc, fgets, fputc,
fputs, fread, fwrite, fseek, ftell, fgetpos, and fsetpos - on pp242-248.
>What about ftell and fseek do they return -1 or non zero
when successful ?

Read the book.
I may have to break it out. I have a little pocket reference I've leafed
though. Couldn't find return values. Well I'll get them.

Bill
Jun 27 '08 #17
On Jun 17, 8:35 am, "Bill Cunningham" <nos...@nspam.comwrote:
"Richard Heathfield" <r...@see.sig.invalidwrote in message

news:g8******************************@bt.com...
Bill Cunningham said:
"Bill Cunningham" <nos...@nspam.comwrote in message
news:66D5k.20948$3j2.17956@trnddc03...
>"Serve Lau" <ni...@qinqin.comwrote in message
news:e8***************************@cache5.tilbu1 .nb.home.nl...
You know my book doesn't give return values for these functions for
error checking.
In message <3d38f60...@corp.newsgroups.com(posted on 20 July 2002) you
wrote:
"This little K&R2 book is giving me the devil of a time."
It is not unreasonable to deduce from that statement that you have had a
copy of K&R2 since at least 2002.
Despite your claim, K&R2 *does* document the return value semantics for
fopen, freopen, fflush, fclose, fprintf, fscanf, fgetc, fgets, fputc,
fputs, fread, fwrite, fseek, ftell, fgetpos, and fsetpos - on pp242-248.
What about ftell and fseek do they return -1 or non zero
when successful ?
Read the book.

I may have to break it out. I have a little pocket reference I've leafed
though. Couldn't find return values. Well I'll get them.

Bill
/* Code not compiled */
#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[]) {
double x;
FILE *fp;
x = strtod(argv[1], NULL);
fp = fopen("foo", "a+");
fprintf(fp, "%.2f", x);
fclose(fp);
return 0;
}

Probably this is what you want to do. The mode "a+" means open for
appending and reading as well. Its not required if you just wish to
append to the file.
Return type for ftell is long. fseek returns int ( -1 on error, 0
otherwise ). If you are on linux/unix box, use the man pages. If you
are on a windows machine and using Visual Studio, you can use MSDN.
Its better to have the documentation handy.

Jun 27 '08 #18
"Bill Cunningham" <no****@nspam.comwrote in message
news:5iG5k.66404$bs3.8292@trnddc07...
>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:g8******************************@bt.com...
>Bill Cunningham said:
>>>
"Bill Cunningham" <no****@nspam.comwrote in message
news:66D5k.20948$3j2.17956@trnddc03...

"Serve Lau" <ni***@qinqin.comwrote in message
news:e8***************************@cache5.tilbu 1.nb.home.nl...

You know my book doesn't give return values for these functions for
error checking.

In message <3d********@corp.newsgroups.com(posted on 20 July 2002) you
wrote:

"This little K&R2 book is giving me the devil of a time."

It is not unreasonable to deduce from that statement that you have had a
copy of K&R2 since at least 2002.

Despite your claim, K&R2 *does* document the return value semantics for
fopen, freopen, fflush, fclose, fprintf, fscanf, fgetc, fgets, fputc,
fputs, fread, fwrite, fseek, ftell, fgetpos, and fsetpos - on pp242-248.
>>What about ftell and fseek do they return -1 or non zero
when successful ?

Read the book.
I may have to break it out. I have a little pocket reference I've
leafed though. Couldn't find return values. Well I'll get them.
http://www.opengroup.org/onlinepubs/...xsh/fopen.html

http://www.opengroup.org/onlinepubs/...sh/fclose.html

http://www.opengroup.org/onlinepubs/...xsh/fread.html

http://www.opengroup.org/onlinepubs/...sh/fwrite.html

http://www.opengroup.org/onlinepubs/...sh/fflush.html

http://www.opengroup.org/pubs/online...h/fgetpos.html

http://www.opengroup.org/pubs/online...h/fsetpos.html

http://www.opengroup.org/onlinepubs/...sh/fscanf.html

http://www.opengroup.org/onlinepubs/...ons/fputs.html

http://www.opengroup.org/onlinepubs/...ons/fputc.html

http://www.opengroup.org/onlinepubs/...ons/fgets.html

http://www.opengroup.org/onlinepubs/...ons/fgetc.html

http://www.opengroup.org/onlinepubs/...ns/ferror.html

http://www.opengroup.org/onlinepubs/...ions/feof.html

http://www.opengroup.org/onlinepubs/...ons/ftell.html

http://www.opengroup.org/onlinepubs/...ons/fseek.html

http://www.opengroup.org/onlinepubs/...ns/fgetwc.html

http://www.opengroup.org/onlinepubs/...ns/fgetws.html

http://www.opengroup.org/onlinepubs/...s/freopen.html

ect...

http://www.opengroup.org/

is you friend!

Jun 27 '08 #19
Bill Cunningham wrote:
"Richard Heathfield" <rj*@see.sig.invalidwrote:
><3d********@corp.newsgroups.com(posted on 20 July 2002) you
wrote:

"This little K&R2 book is giving me the devil of a time."

It is not unreasonable to deduce from that statement that you
have had a copy of K&R2 since at least 2002.

Despite your claim, K&R2 *does* document the return value
semantics for fopen, freopen, fflush, fclose, fprintf, fscanf,
fgetc, fgets, fputc, fputs, fread, fwrite, fseek, ftell,
fgetpos, and fsetpos - on pp242-248.
>>What about ftell and fseek do they return -1 or non zero
when successful ?

Read the book.

I may have to break it out. I have a little pocket reference
I've leafed though. Couldn't find return values.
You should never have hidden it. It answers virtually every
question you have asked here.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

** Posted from http://www.teranews.com **
Jun 27 '08 #20
Serve Lau wrote:
"Bill Cunningham" <no****@nspam.comschreef in bericht
>I have a question about fopen. I want to append text to a text
file already written. I think this is what I need to do. If I am
correct fopen's second parameter takes only 2 letters. I am using
wt. To append text would I need another call to fopen if my text
file isn't already existing consisting of wa or a+ ?

read your favorite C book and try it out, its in there no matter
if its a good or bad book
You appear to devote your time to telling people not to ask
questions, using incorrect punctuation.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #21

"CBFalconer" <cb********@yahoo.comschreef in bericht
news:48***************@yahoo.com...
Serve Lau wrote:
>"Bill Cunningham" <no****@nspam.comschreef in bericht
>>I have a question about fopen. I want to append text to a text
file already written. I think this is what I need to do. If I am
correct fopen's second parameter takes only 2 letters. I am using
wt. To append text would I need another call to fopen if my text
file isn't already existing consisting of wa or a+ ?

read your favorite C book and try it out, its in there no matter
if its a good or bad book

You appear to devote your time to telling people not to ask
questions, using incorrect punctuation.
Please show me my posts that justifies this general statement. Also please
dont criticize my puntuation, it makes me feel VERY bad.

Jun 27 '08 #22
Serve Lau wrote:
"CBFalconer" <cb********@yahoo.comschreef:
>Serve Lau wrote:
>>"Bill Cunningham" <no****@nspam.comschreef in bericht

I have a question about fopen. I want to append text to a text
file already written. I think this is what I need to do. If I am
correct fopen's second parameter takes only 2 letters. I am using
wt. To append text would I need another call to fopen if my text
file isn't already existing consisting of wa or a+ ?

read your favorite C book and try it out, its in there no matter
if its a good or bad book ^^
^^
You appear to devote your time to telling people not to ask
questions, using incorrect punctuation.

Please show me my posts that justifies this general statement. Also
please dont criticize my puntuation, it makes me feel VERY bad.
^^
Alright, I'll ignore punctuation. :-) As for general statements,
the quotation above will do.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #23

"CBFalconer" <cb********@yahoo.comschreef in bericht
news:48***************@yahoo.com...
>Please show me my posts that justifies this general statement. Also
please dont criticize my puntuation, it makes me feel VERY bad.
^^
Alright, I'll ignore punctuation. :-) As for general statements,
the quotation above will do.
get out more

Jun 27 '08 #24
"Serve Lau" <ni***@qinqin.comwrote:
"CBFalconer" <cb********@yahoo.comschreef in bericht
news:48***************@yahoo.com...
Please show me my posts that justifies this general statement. Also
please dont criticize my puntuation, it makes me feel VERY bad.
^^
Alright, I'll ignore punctuation. :-) As for general statements,
the quotation above will do.

get out more
Leer je talen correct schrijven, dwaze Belg. Je bent een belediging voor
het Nederlandse taalgebied.

Richard
Jun 27 '08 #25

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ming | last post: by
11 posts views Thread by typingcat | last post: by
10 posts views Thread by Grocery Clerk | last post: by
28 posts views Thread by Sathyaish | last post: by
13 posts views Thread by Blue | last post: by
10 posts views Thread by pjlsr | last post: by
5 posts views Thread by Jae | last post: by
10 posts views Thread by Julia | last post: by
25 posts views Thread by subramanian100in | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.