Thread: making a text file into a char array

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    7

    Smile making a text file into a char array

    Hi

    Im writing a program to create a textbuffer. I have finished writing the functions that the interface has specified and I wanted to test the functions.

    I'm however unable to read in the text file in the form of a char array coz my newTB function is suuposed to take in a char array.

    this is my main function where im calling newTB and releaseTB.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "textbuffer.h"
    #include "textbuffer.c"
    
    int main (int argc, char* argv[]) {
       FILE *f;
       f = fopen("testData.txt","r");
       TB newtestTB = newTB (f); 
       releaseTB(newtestTB);
       assert( newtestTB==NULL);
    	
       return 0;
    }
    and these are my newTN and releaseTB prototypes:
    Code:
    /* Allocate a new textbuffer whose contents is initialised with the text given
     * in the array.
     */
    TB newTB (char text[]);
    
    /* Free the memory occupied by the given textbuffer.  It is an error to access
     * the buffer afterwards.
     */
    void releaseTB (TB tb);

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by shredderr View Post
    Hi

    Im writing a program to create a textbuffer. I have finished writing the functions that the interface has specified and I wanted to test the functions.

    I'm however unable to read in the text file in the form of a char array coz my newTB function is suuposed to take in a char array.

    this is my main function where im calling newTB and releaseTB.
    [CODE]#include <stdio.h>
    #include <stdlib.h>
    #include "textbuffer.h"
    #include "textbuffer.c"

    int main (int argc, char* argv[]) {
    FILE *f;
    f = fopen("testData.txt","r");
    TB newtestTB = newTB (f);
    [code]
    1) What you are actually doing there is assigning your file pointer (not it's contents) to the newtb function.
    2) TB NewTB is not a valid function name. Function names cannot contain spaces.
    3) Never #include a C file...

    What you need to do in your main() function is this...
    Open your file...
    Discover it's size ... fseek() to the end, use ftell() to get the size.
    Create a char array ... Use calloc() to make it a few characters bigger than the file.
    Reset the file pointer... fseek() to the beginning
    Read the whole file .. Use a single fread() call to grab the whole thing into your array.
    Close the file.
    Do what you gotta do to the array...
    Release the memory... Use free() to release it.

    You probably won't want to do that with self-written functions because all you'd really be doing is reinventing the wheel...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading and making calculation in Text file using C
    By ntagrafix in forum C Programming
    Replies: 5
    Last Post: 02-21-2010, 12:15 AM
  2. Making a char array uppercase
    By pollypocket4eva in forum C Programming
    Replies: 6
    Last Post: 11-17-2008, 09:34 AM
  3. Making a unicode text file
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2008, 08:21 PM
  4. Write text file to char array
    By mjh in forum C Programming
    Replies: 2
    Last Post: 04-02-2007, 08:11 AM
  5. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 06:18 PM