Thread: Question about Dynamic Allocation and reading from a file

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    12

    Question about Dynamic Allocation and reading from a file

    Hello,

    I need to make a program that can read a file and store the file's values in an array.

    My first step was to find the size of the file using FSEEK in conjunction with the ftell function.

    Now my question is in regards to sizing the array which will hold the values of the file.

    Will the size of the array needed be exactly the size of the value returned by the ftell function? I'm using fgets to retrieve the data, so would I have to increase the size in order to store a "NULL" value?

    Here is a sample of the code:

    Code:
    ...
    fseek(in,0,SEEK_END);
    size=ftell(in);
    
    array=(char*)malloc(size*sizeof(char));
    
    while ((fgets(data,size,in)) != NULL)
    {
    }
    ...
    Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    You seem to have somewhat solved this problem in your next post. But I don't know what you mean by storing a NULL value.

    You should say exactly what format your file is in and exactly how you want to store the data in your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another question about dynamic allocation
    By AAnderson in forum C Programming
    Replies: 58
    Last Post: 04-03-2013, 07:10 PM
  2. Dynamic Memory Allocation Question
    By somniferium in forum C Programming
    Replies: 6
    Last Post: 10-12-2012, 07:51 PM
  3. Replies: 15
    Last Post: 06-17-2012, 04:27 PM
  4. Help with file reading/dynamic memory allocation
    By Quasar in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2004, 03:36 PM
  5. dynamic allocation question
    By vale in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 04:23 PM

Tags for this Thread