Home > database >  Accessing Binary form/data/array of a text file in C
Accessing Binary form/data/array of a text file in C

Time:07-14

My understanding is, if we run the function for the binary form of that string, it could be faster. For example, let, a string "AAABVAVBBA" is written in a text file test.txt in the hard drive.

Now I want to run KMP string matching algorithm in C and use the binary string of string "AAABVAVBBA" as input (pattern is already given in binary array).

When we save test.txt in the hard drive, it is already in the binary form, so how can I access that binary form of "AAABVAVBBA" in the memory (hard drive) using C?

Note I am not asking how to convert "AAABVAVBBA" string to a binary string (like this post). I want to get the binary form already saved in the hard drive (memory), which is used by assembler.

My query is close to this post.

CodePudding user response:

I don't know if this will answer your question, but please run and study this program:

#include <stdio.h>

int main()
{
    FILE *fp = fopen("test.text", "r");
    if(fp == NULL) return 1;

    char buf[100];
    int r = fread(buf, 1, sizeof(buf), fp);
    if(r <= 0) return 1;

    printf("as string: %.*s", r, buf);

    int i;
    printf("as characters:");
    for(i = 0; i < r; i  ) printf(" %c", buf[i]);
    printf("\n");

    printf("as integers:");
    for(i = 0; i < r; i  ) printf(" %d", buf[i]);
    printf("\n");

    return 0;
}

CodePudding user response:

Here is a bit modified version of @steves program.

https://godbolt.org/z/9dEK6f11h

It writes (and reads of course) in binary and in text mode. Spot the difference if you can. I can't. So you try to solve non-existing problem.

All credit to Steve Summit (upvote his answer)

  • Related