Hi, i would like to know if there is a function in c that allows me to search for a specific character in a file, and how to use it.
Thanks in advance

Dani AI

Generated

As noted, the C standard library does not provide a single function that searches a FILE* directly — the file must be read and the bytes scanned. 's buffer approach is the right idea, but be careful: strchr expects a NUL-terminated C string and is not suitable for arbitrary bytes returned by fread. For raw byte searches use memchr (or a simple loop). A good tradeoff is reading chunks with fread and scanning each chunk with memchr, keeping a running byte offset so the absolute position can be returned.

#include <stdio.h>
#include <string.h>

long find_char_in_file(FILE *f, unsigned char target) {
    unsigned char buf[4096];
    long base = 0;
    size_t n;
    while ((n = fread(buf, 1, sizeof buf, f)) > 0) {
        unsigned char *p = memchr(buf, target, n);
        if (p)
            return base + (p - buf); /* byte offset from start */
        base += n;
    }
    return -1; /* not found (or check ferror(f) for I/O error) */
}

Notes and cautions: open the file in binary mode for byte-accurate results (Windows translates newlines in text mode). Always cast character values to unsigned char when using memchr/strchr. fgetc is simpler for tiny files but slower; ftell gives the current stream position (after a read, subtract 1 if needed) but behavior in text mode is implementation-defined. For very large files on POSIX systems, mmap can be faster; for multi-byte patterns handle chunk overlap between reads.

Recommended Answers

All 2 Replies

No.
You have to read the file and search in the buffer you read.

When you open a file, you receive a file position pointer (part of the FILE *, but not the whole thing).

The file pointer has the address of the first byte of the file. If you increment that address, you can access any byte in the file, in sequential mode.

There are a number of different ways to do that - pick the version of fgetc(char) that you want to use, or use your own char pointer, or ...

Walt is right, that usually, you want to load the file into a buffer, and then scan through it, but you can do it directly as well. Even more options exist, since you can do it using high level stream file handling (easier), or low level non-stream file handling (more complex and definitely not recommended).

The only time that I wouldn't use a buffer, would be if the file was so large that I couldn't fit it all in the char buffer you create. Naturally, a really large char buffer, will need to be allocated dynamically with malloc/calloc, because it will exceed the size of the static memory that C has allocated for such things. (the stack).

For starters, why don't you statically create a char buffer[BUFSIZ] and try using strch() to find the char you want. It will return a pointer to the first instance of the char, or NULL if no such char (which is case sensitive), is found. So you need to check the address that strch() returns.

if( strch() ) { 
  //the char was found //the return was not NULL
else
  //the char was not found //NULL was returned

Now, tell me what goes into the call to strch(). ;)

P.S. You will need to include string.h to use strch().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.