fopen

B

Bill Cunningham

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
 
W

Walter Roberson

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.)
 
S

Serve Lau

Bill Cunningham said:
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
 
V

vippstar

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.
 
B

Bill Cunningham

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
 
B

Bill Cunningham

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
 
B

Bill Cunningham

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
 
B

Bill Cunningham

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
 
B

Bill Cunningham

Richard Heathfield said:
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
 
R

Richard

Bill Cunningham said:
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.
 
R

Richard

Bill Cunningham said:
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 :)
 
B

Bill Cunningham

Richard Heathfield said:
Bill Cunningham said:


In message <[email protected]> (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.


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
 
R

rahul

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.
 
S

Serve Lau

CBFalconer said:
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.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top