From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/io%2Fftell.html | 77 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 devdocs/c/io%2Fftell.html (limited to 'devdocs/c/io%2Fftell.html') diff --git a/devdocs/c/io%2Fftell.html b/devdocs/c/io%2Fftell.html new file mode 100644 index 00000000..c2696fde --- /dev/null +++ b/devdocs/c/io%2Fftell.html @@ -0,0 +1,77 @@ +

ftell

Defined in header <stdio.h>
long ftell( FILE *stream );
+

Returns the file position indicator for the file stream stream.

+

If the stream is open in binary mode, the value obtained by this function is the number of bytes from the beginning of the file.

+

If the stream is open in text mode, the value returned by this function is unspecified and is only meaningful as the input to fseek().

+

Parameters

+ +
stream - file stream to examine

Return value

File position indicator on success or -1L if failure occurs.

+

On error, the errno variable is set to implementation-defined positive value.

+

Example

+

Demonstrates ftell() with error checking. Writes then reads a few floating-point (FP) values to/from a file.

+
#include <stdio.h>
+#include <stdlib.h>
+ 
+/* If the condition is not met then exit the program with error message. */
+void check(_Bool condition, const char* func, int line)
+{
+    if (condition)
+        return;
+    perror(func);
+    fprintf(stderr, "%s failed in file %s at line # %d\n", func, __FILE__, line - 1);
+    exit(EXIT_FAILURE);
+}
+ 
+int main(void)
+{
+    /* Prepare an array of FP values. */
+    #define SIZE 5
+    double A[SIZE] = {1.1,2.,3.,4.,5.};
+ 
+    /* Write array to a file. */
+    const char* fname = "/tmp/test.bin";
+    FILE* file = fopen(fname, "wb");
+    check(file != NULL, "fopen()", __LINE__);
+ 
+    const int write_count = fwrite(A, sizeof(double), SIZE, file);
+    check(write_count == SIZE, "fwrite()", __LINE__);
+ 
+    fclose(file);
+ 
+    /* Read the FP values into array B. */
+    double B[SIZE];
+    file = fopen(fname, "rb");
+    check(file != NULL, "fopen()", __LINE__);
+ 
+    long int pos = ftell(file); /* position indicator at start of file */
+    check(pos != -1L, "ftell()", __LINE__);
+    printf("pos: %ld\n", pos);
+ 
+    const int read_count = fread(B, sizeof(double), 1, file); /* read one FP value */
+    check(read_count == 1, "fread()", __LINE__);
+ 
+    pos = ftell(file); /* position indicator after reading one FP value */
+    check(pos != -1L, "ftell()", __LINE__);
+    printf("pos: %ld\n", pos);
+    printf("B[0]: %.1f\n", B[0]); /* print one FP value */
+ 
+    return EXIT_SUCCESS;
+}

Possible output:

+
pos: 0
+pos: 8
+B[0]: 1.1

References

See also

+ + + +
gets the file position indicator
(function)
moves the file position indicator to a specific location in a file
(function)
moves the file position indicator to a specific location in a file
(function)
C++ documentation for ftell
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/io/ftell +

+
-- cgit v1.2.3