By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

function and fileio questions

P: n/a
I got some maybe stupid questions about file output and functions.
At first I have to say I'm programming on Linux.

The first thing is I couldn't find a function which can tell me
microseconds and the second is that I couldn't find a characterreading
unbuffered function.

Also I couldn't compare the cursorpositions in files.

And the last thing is that I couldn't insert text in a file(or delete
it) with the function fopen() and then using fprintf(). Either I
overwrote the hole file or I just attached the text to the end.

I hope you can help me.
Thanks in advance.

Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Erik Paul wrote:

I got some maybe stupid questions about file output and functions.
At first I have to say I'm programming on Linux.
When it is necessary to mention the system you are using,
it is usually the case that the questions are about that system
and not about the C programming language. You will probably
get better answers from a newsgroup devoted to Linux, or to
Unix programming in general.
The first thing is I couldn't find a function which can tell me
microseconds [...]
Standard C provides time() to measure "real-world" time and
clock() to measure processor time. Standard C does not specify
the accuracy of these measurements, nor the precision of their
expression. For some non-Standard possibilities that might be
available on your system, see Question 19.37 of the comp.lang.c
Frequently Asked Questions (FAQ) list at

http://www.eskimo.com/~scs/C-faq/top.html
and the second is that I couldn't find a characterreading
unbuffered function.
This is Question 19.1.
Also I couldn't compare the cursorpositions in files.
I am not sure what you mean by "cursorposition." If you
mean the position at which the next read would start, ftell()
can provide this as a number -- but the number might not be
meaningful for a text stream.
And the last thing is that I couldn't insert text in a file(or delete
it) with the function fopen() and then using fprintf(). Either I
overwrote the hole file or I just attached the text to the end.


This seems to be Question 19.14, or possibly 19.13.

--
Er*********@sun.com
Nov 14 '05 #2

P: n/a
Erik Paul <In**************@freenet.de> writes:
I got some maybe stupid questions about file output and functions.
At first I have to say I'm programming on Linux.
That's a bad sign--if you have to say what platform you're using,
then the question is probably off-topic in comp.lang.c, because
we only discuss the standard C programming language here.
Extensions and system-specific features are off-topic.
The first thing is I couldn't find a function which can tell me
microseconds
If you mean "the current time in microseconds", there's nothing
in standard C that can portably tell you that. Try
comp.unix.programmer for system-specific information.
and the second is that I couldn't find a characterreading
unbuffered function.
You can use setvbuf() to turn off buffering for a file, then
getc() to read a character from it unbuffered. However, if you
mean "read a character from the user without waiting", see C FAQ
19.1. (Use Google to find the FAQ.)
Also I couldn't compare the cursorpositions in files.
In binary files, you can call ftell() to find out the current
number of characters from the beginning of the file. If ftell()
fails, as it might if you're at a position in the file that can't
be represented as a long int, it returns -1.
And the last thing is that I couldn't insert text in a file(or delete
it) with the function fopen() and then using fprintf(). Either I
overwrote the hole file or I just attached the text to the end.


See C FAQ 19.14.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Nov 14 '05 #3

P: n/a
Erik Paul wrote:
The first thing is I couldn't find a function which can tell me
microseconds What do you mean? The seconds/microseconds which have passed?
and the second is that I couldn't find a characterreading
unbuffered function.

I don't know if ANSI C has functions for unbuffered I/O,
but since you are working on Linux you could use the POSIX functions
declared in fcntl.h.

http://www.opengroup.org/onlinepubs/...s/fcntl.h.html
http://www.opengroup.org/onlinepubs/...ions/open.html

For discussing this functions you should consult as POSIX newsgroup.

-Robert

--
Robert Bachmann _ _
Ro*************@rbdev.net |_) |_)
| \.|_).
Nov 14 '05 #4

P: n/a
On Mon, 15 Dec 2003 18:22:37 +0100, Erik Paul
<In**************@freenet.de> wrote:
The first thing is I couldn't find a function which can tell me
microseconds


When I want to time small functions or sections of code, I use an
assembler function that gets the Pentium or AMD processor's "time
stamp counter" into EDX;:EAX. The function, therefore, returns a
"long long" value. It's just "rdtst" followed by "ret". The function
itself requires 53-115 clocks to run, including the cost of assigning
the returned value to a variable. A microsecond counter isn't much
use except for pieces of code that take at least several microseconds
to run.

Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.