-1

How to find out the lenght of an array of chars that is not null terminated/zero terminated or anything like that?

Because I wrote a writeFile function and I wanna get rid of that 'len' parameter.

int writeFile(FILE * handle, char * data, int len)
{
    fseek(handle, 0, SEEK_SET);
    for(int i=0; i <= len; i++)
        fputc(data[i], handle);
}
user1091856
  • 3,032
  • 6
  • 31
  • 42
  • 4
    You can't, that's what `len` is there for, unless you just want to combine the two in a `struct` or something, or use `std::string` or a `vector` or something else because your question is tagged C++. – Seth Carnegie Jan 08 '12 at 18:00
  • 4
    For C++, see http://stackoverflow.com/a/2404697/103167 By the way, you're actually writing `len+1` characters. – Ben Voigt Jan 08 '12 at 18:00

5 Answers5

3

You cannot get rid of the len parameter. The computer is not an oracle to guess your intentions. But you can use the fwrite() function which will write your data much more efficiently than fputc().

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
1

there is no portable way*, that is why sentinel values like null terminators are used.

In fact, its better to specify a length parameter, as it allows partial writes from data buffers (though in your case I would use a size_t/std::size_t for the length).

*you could try using _msize on windows, but it will lead to tears.

Necrolis
  • 25,836
  • 3
  • 63
  • 101
0
#define writeFile(handle, data) writeFileImpl(handle, data, sizeof data)
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 3
    what happens if an array thats decayed to a pointer has been passed, or a text string from `malloc`? :) in this case I think a macro would be way too evil, as sooner or later, someone will assume that it works on everything... – Necrolis Jan 08 '12 at 18:03
  • @Necrolis: The C++ solution I linked in my comment is DEFINITELY better, since it protects against reporting the size of a pointer. But if you gotta have something that works in C, this is it. – Ben Voigt Jan 08 '12 at 18:18
0

As Seith Carnegie commented and others answered, you cannot do that (getting the length of any array of char).

Some C libraries provide you with an extension giving an (over-sized) estimate of the length of heap-allocated memory (e.g. pointers obtained by malloc).

If you uses Boehm's garbage collector (which is very useful!), it gives you in <gc/gc.h> the GC_size function.

But when the array of char is inside a structure, or on the call stack, there is no way to get its size at runtime. Only your program knows it.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

You can't get rid of the len parameter unless you have another way of determining the length of your data (usually by using a null terminator). This is because C and C++ don't store the length of the data. Furthermore, programmers might appreciate the len parameter. You don't always want to write out all the bytes in your array.

Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77