Thread: Help Reading File Line By Line On Windows?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    Provided that you don't have to load a file with a size in the order of some GB's, I suggest that you try something like this:


    1. check the size of the file
    2. allocate a chunk of dynamic memory as large as needed to contain the whole contents of the file
    3. load the contents of the file in the allocated memory
    4. scan the resulting string in order to count the total number of '\n' characters contained in the allocated memory (which equal to the number of lines)
    5. allocate a chunk of memory containing space for a vector of pointers to char as long as the number of lines
    6. store the pointer to the first character of the loaded string, then scan the loaded string again looking for each '\n' character; whenever you do find one, do two things: a. put a '\0' terminator in place of the '\n' and b. store the index of the next character into the next pointer to char of your vector


    When the process is done, you should have a vector containing pointers to char, each of which pointing to the very start of one line taken from the file and zero terminated. You then should be able to access those strings by using the pointers stored in the vector with simple zero-based indexes.

    Of course, it would be a good thing isolating the mechanism I just described in a dedicated subroutine.

    I could provide sample code, and you surely could grasp the mechanism in a much easier way, but looks like providing complete sample code is forbidden in this forum.

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    6
    Quote Originally Posted by aldo_baldo View Post
    Provided that you don't have to load a file with a size in the order of some GB's, I suggest that you try something like this:
    1. check the size of the file
    2. allocate a chunk of dynamic memory as large as needed to contain the whole contents of the file
    3. load the contents of the file in the allocated memory
    4. scan the resulting string in order to count the total number of '\n' characters contained in the allocated memory (which equal to the number of lines)
    5. allocate a chunk of memory containing space for a vector of pointers to char as long as the number of lines
    6. store the pointer to the first character of the loaded string, then scan the loaded string again looking for each '\n' character; whenever you do find one, do two things: a. put a '\0' terminator in place of the '\n' and b. store the index of the next character into the next pointer to char of your vector

    When the process is done, you should have a vector containing pointers to char, each of which pointing to the very start of one line taken from the file and zero terminated. You then should be able to access those strings by using the pointers stored in the vector with simple zero-based indexes. Of course, it would be a good thing isolating the mechanism I just described in a dedicated subroutine. I could provide sample code, and you surely could grasp the mechanism in a much easier way, but looks like providing complete sample code is forbidden in this forum.
    Any possible way you get me the source somehow?

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by new2c95 View Post
    Any possible way you get me the source somehow?
    Are you looking for someone to solve your specific problem for you and post source code to that solution? The purpose of this forum is to learn and get suggestions, improve your skills. If you post what you've got so far as a complete program maybe someone can suggest the next step or help you fix what is broken.

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    Quote Originally Posted by new2c95 View Post
    Any possible way you get me the source somehow?
    I have the code in the form of a LoadStrings() function, and I really WOULD LIKE to share it with you. I'm just an enthusiastic self-taught, low-end hobbyist, and I know very well that one can learn a lot by looking at someone else's code (I learnt so much that way). I can't understand how giving examples could make any harm, but I don't want to break the rules of the forum.

    Someone here can explain me what's the difference between providing tutorials and providing some sample code? There are tutorials, here, so why can't we share sample code?
    Last edited by aldo_baldo; 10-14-2014 at 09:41 AM. Reason: Typos correction

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems while reading a file line by line
    By tinchi in forum C Programming
    Replies: 9
    Last Post: 08-17-2014, 12:10 PM
  2. Replies: 6
    Last Post: 06-07-2012, 02:50 AM
  3. Replies: 7
    Last Post: 12-13-2010, 02:13 PM
  4. reading words line by line from a file
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 05-04-2008, 12:34 AM
  5. reading a file line by line and char *
    By odysseus.lost in forum C Programming
    Replies: 8
    Last Post: 05-31-2005, 09:47 AM

Tags for this Thread