function and fileio questions

E

Erik Paul

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

Eric Sosman

Erik said:
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.
 
B

Ben Pfaff

Erik Paul said:
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.
 
R

Robert Bachmann

Erik said:
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/007904975/basedefs/fcntl.h.html
http://www.opengroup.org/onlinepubs/007904975/functions/open.html

For discussing this functions you should consult as POSIX newsgroup.

-Robert
 
P

Paul Emmons

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top