C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-03-2006, 03:36 PM   #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.
CaeZaR is offline   Reply With Quote
Old 02-03-2006, 03:50 PM   #2
Confused
 
Magos's Avatar
 
Join Date: Sep 2001
Location: Sweden
Posts: 3,125
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.
Magos is offline   Reply With Quote
Old 02-03-2006, 03:56 PM   #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.
Kurisu is offline   Reply With Quote
Old 02-03-2006, 11:36 PM   #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...).
Stuka is offline   Reply With Quote
Old 02-04-2006, 04:18 PM   #5
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
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.
dwks is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:54 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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