moerjielovecookie

Sawen_Blog

一个普通工科牲的博客网站
x
github
follow
email

C Standard Library File I/O

1 Function Introduction#

1.1 Library Variables#

VariableDescription
size_tAn unsigned integer type, the result of the sizeof keyword, representing the size of an object
FILEA file stream type, suitable for storing file stream information

1.2 Library Macros#

MacroDescription
NULLNull pointer constant
EOFA negative integer indicating the end of a file
stderr, stdin, stdoutPointers to FILE type, corresponding to standard error, standard input, and standard output streams

1.3 Library Functions#

1.3.1 fopen#

FILE *fopen(const char *filename, const char *mode) 
// Opens the file pointed to by filename with the given mode mode
modeDescription
"r"Opens a file for reading; the file must exist
"w"Creates an empty file for writing. If a file with the same name exists, its contents are deleted
"a"Appends to a file; write operations append data to the end of the file, creating the file if it does not exist
"r+"Opens a file for update; can read and write. The file must exist
"w+"Creates an empty file for reading and writing
"a+"Opens a file for reading and appending

The function returns a FILE pointer, or NULL on failure

1.3.2 fread#

Declaration:

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
// ptr: Pointer to a memory block with a minimum size of size*nmemb bytes
// size: Size of each element to be read, in bytes
// nmemb: Number of elements
// stream: Pointer to a FILE object that specifies an input stream

The number of successfully read elements is returned as a size_t object

1.3.3 fwrite#

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
// ptr: Pointer to the array of elements to be written
// size: Size of each element to be written, in bytes
// nmemb: Number of elements
// stream: Pointer to a FILE object that specifies an output stream

1.3.4 fseek#

int fseek(FILE *stream, long int offset, int whence)
// stream: Pointer to a FILE object
// offset: Offset in bytes relative to whence
// whence: Indicates the position from which to add the offset

whence is generally one of the following three constants

ConstantDescription
SEEK_SETBeginning of the file
SEEK_CURCurrent position of the file pointer
SEEK_ENDEnd of the file

Returns the number of bytes from the whence position

1.3.5 ftell#

long int ftell(FILE *stream)

Returns the current value of the position identifier

2 Using the C Standard Library to Read TS Files and Write to TXT#

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

int main() {
    FILE *fileInput, *fileOutput;
    unsigned char buffer[188];  // The standard packet size for TS streams is 188 bytes
    size_t bytesRead;

    // Open TS file
    fileInput = fopen("/home/sawen/my_test_pcie/test.ts", "rb");
    if (!fileInput) {
        perror("Failed to open input file");
        return EXIT_FAILURE;
    } else {
        printf("Input file opened successfully.\n");
    }

    // Check file size
    fseek(fileInput, 0, SEEK_END);
    long fileSize = ftell(fileInput);
    rewind(fileInput);
    if (fileSize == 0) {
        printf("Input file is empty.\n");
        fclose(fileInput);
        return EXIT_FAILURE;
    } else {
        printf("Input file size: %ld bytes.\n", fileSize);
    }

    // Open TXT file
    fileOutput = fopen("output.txt", "w");
    if (!fileOutput) {
        perror("Failed to open output file");
        fclose(fileInput);
        return EXIT_FAILURE;
    } else {
        printf("Output file opened successfully.\n");
    }

    // Read data and write to TXT file
    int packetCount = 0;
    while ((bytesRead = fread(buffer, 1, sizeof(buffer), fileInput)) > 0) {
        for (size_t i = 0; i < bytesRead; ++i) {
            fprintf(fileOutput, "%02x ", buffer[i]); // Write bytes in hexadecimal format
        }
        fprintf(fileOutput, "\n");  // New line after each TS packet
        packetCount++;
    }

    // Check if any data was read
    if (packetCount == 0) {
        printf("No data read from file.\n");
    } else {
        printf("Processed %d TS packets.\n", packetCount);
    }

    // Close files
    fclose(fileInput);
    fclose(fileOutput);

    printf("Data transfer complete.\n");
    return EXIT_SUCCESS;
}

The output is as follows

1718891286912.png

You can see that it writes to the txt file in lines of 188 bytes.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.