Thread: Reading in file of unknown length

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Reading in file of unknown length

    Hello I am trying to read in a file using fgets. I purposely malloc
    a char pointer that will not be able to fit the entire line of the file and
    will be able to increment it if needed using realloc. But for some reason
    I can't seem to figure out why it seg faults. The code and result are
    attached if anyone can help me figure out what I am doing wrong that would be very appreciated!
    Reading in file of unknown length-code-png

    Reading in file of unknown length-result-png

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are not increasing the size. Check the simple example here
    What you do is to allocate again and again the same size.

    Also mind that is good to wrap your code in code tags rather than post a screenshot
    Example
    [key]
    /* YOU code here */
    [/key]
    Replace word key with code in order this to work

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by std10093
    Also mind that is good to wrap your code in code tags rather than post a screenshot
    Example
    [key]
    /* YOU code here */
    [/key]
    Replace word key with code in order this to work
    FWIW, you can use [noparse][/noparse] tags to surround [code][/code] tags in your post without having them parsed.
    Attached Images Attached Images Reading in file of unknown length-untitled-jpg 
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from a textfile with unknown length?
    By chickenlittle in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2011, 11:59 PM
  2. unknown file type reading
    By xixonga in forum C Programming
    Replies: 20
    Last Post: 11-28-2010, 12:17 PM
  3. Read from file of unknown length, then terminate
    By hello234 in forum C Programming
    Replies: 2
    Last Post: 10-12-2008, 04:33 PM
  4. reading a file of unknown length
    By the bassinvader in forum C Programming
    Replies: 2
    Last Post: 07-12-2006, 03:06 PM