Reading file into char array in C: Or how to dynamically allocate an array?

Using Linux system calls open and read. The read call accepts 3 parameters

read(File descriptor, Buffer array, Size of the buffer array);

My question is related to the argument #3. Suppose I have multiple text files that I want to read into my buffer array. But as far as I know C doesn't have something like Vector or ArrayList or any other structure with a dynamic size.

Is there an easy way to calculate the size of the file so I know how much space should I allocate for my char array?

Answers


You could also stat() the file to determine its size and then call mmap() on it. Your file will appear as an "array" without the need to malloc() memory.


Need Your Help

Final Keyword in Constant utility class

java performance class constants final

Is the any difference in performance and/or any other benefits we can get when using final keyword with constant utility class. [ This class contains only static final fields and private constructo...

Overriding a method returning a generic class

c# generics inheritance override

in my Silverlight 4 application I started creating and using some generics and now I stumbled upon the following problem:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.