Thread: trying to read numbers from a file - only getting zeros

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'd suggest reading in a single pass instead. Do this by reading line by line into a sufficiently long string. Check if the line begins with a '#', and if so, you can skip it. Otherwise, parse it for the numeric values.

    For saving the numeric values, you can start with a dynamic array of a small capacity (number of elements allocated) with zero size (number of elements in use). As you read the numeric values into struct objects and populate the dynamic array with these struct objects, the size will eventually reach the capacity. At that point, to add more numeric value struct objects, you expand the capacity of the dynamic array by a factor (typically 1.5 or twice). So, by keeping track of both size and capacity, you can read in a single pass rather than having a first pass to find the size and then a second pass to do the actual reading and extraction.
    Last edited by laserlight; 07-28-2020 at 05:53 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-10-2013, 09:47 PM
  2. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  3. How to read numbers out of a file in c
    By muskateer1 in forum C Programming
    Replies: 3
    Last Post: 04-05-2012, 02:29 PM
  4. read file? zeros
    By Question in forum C++ Programming
    Replies: 8
    Last Post: 03-12-2003, 08:19 PM
  5. data read zeros?
    By Question in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2003, 01:15 PM

Tags for this Thread