Thread: Reading text file twice, can it be done once?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    9

    Reading text file twice, can it be done once?

    I am reading a text file of two-column numeric data. I do not know the number of rows that the file will contain. Currently, I read the file twice: Once to ascertain the length and the second time to fill the arrays with the data. Is there a way to create and populate the arrays while only having to read the text file once?

    Here is my current code:
    Code:
    FILE* fid;
    double tempx, tempy;
    
    fid = fopen("c:/file.txt","rt");
    int i=0;
    while( fscanf(fid,"%le%le", &tempx, &tempy )!= EOF )
    	i++;
    rewind( fid );
    
    x = new double[i];
    y = new double[i];
    
    i=0;
    while (fscanf(fid,"%le%le", x+i, y+i)!= EOF)
    	i++;
    fclose(fid);
    Thanks for your time.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    std::vector<int> x;
    std::vector<int> y;
    
    while(...)
    {
      x.push_back(...);
      y.push_back(...);
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    Without thinking about it too much it would seem like there are three options:

    1) Create arrays big enough to hold all the data.

    Drawback: Wasted memory space if text file contains small amount of data compared to array size created.

    2) Allocate memory as you go along. I'm sure you could ask for more resources to enlarge your array by one INT entry every new row that is encountered using MALLOC or one of its close cousins.

    3) Use a linked list type structure where each node holds an integer value and just enlarge as you go along. There are libraries that already provide this if you don't want to write your own, such as Vector.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    CaeZaR: What Magos said, with the additional recommendation that you drop C-style file input for C++ iostreams in C++ code. First off, IMHO it's much easier, and second, it's better integrated with the other STL structures (like vectors...).

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You have the worst luck . . . post it in the C forum and get "don't use new", post it here and get "don't use C file functions".

    What you want is realloc(). Look it up and/or examine this C program (I wrote it in one sitting, it probably isn't perfect):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        char **s = 0, **p;
        char line[BUFSIZ];
        int x, lines = 0;
    
        while(fgets(line, sizeof(line), stdin)) {
            p = realloc(s, sizeof(char *) * (lines+1));
            if(!p) {
                perror("Out of memory");
                exit(1);
            }
            s = p;
    
            s[lines] = malloc(strlen(line)+1);
            strcpy(s[lines++], line);
        }
    
        for(x = 0; x < lines; x ++) {
            printf("%i: %s", x, s[x]);
            free(s[x]);
        }
        free(s);
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM