Reading in file of unknown length

This is a discussion on Reading in file of unknown length within the C Programming forums, part of the General Programming Boards category; Hello I am trying to read in a file using fgets. I purposely malloc a char pointer that will not ...

  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!
    Name:  code.PNG
Views: 133
Size:  19.4 KB

    Name:  result.PNG
Views: 88
Size:  17.6 KB

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    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,788
    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  
    "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, 11:17 AM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21