![]() |
| | #1 |
| Registered User Join Date: Jan 2006
Posts: 9
| Reading text file twice, can it be done 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);
|
| CaeZaR is offline | |
| | #2 |
| Confused 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 | |
| | #3 |
| Registered User 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 | |
| | #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 | |
| | #5 |
| Frequently Quite Prolix 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |