Re:[suse-programming-e] text file into array (c++)
Hi Axel, My understanding was that by using an array of pointers, you won't need to resize any arrays. The strings pointed to by the pointers will automatically resize to the length of the sting + 1 for the terminating char ('\0'). I have not read about vectors as yet, but they are sure to be next on my list. Thanks, Ged.
From: <gigatronik.finger@daimlerchrysler.com> Date: 2003/12/12 Fri AM 09:04:50 GMT To: ged.suse@ntlworld.com Subject: Antwort: [suse-programming-e] text file into array (c++)
Hi Ged,
I am struggeling with this problem too. First thing is that you should use a vector. So you are not tied up in an array bounds. If your textfile changes in length you have to recompile your code, because your array is not big enough. Here is some sample code for acting with vectors:
#include <iostream> #include <vector>
using namespace std;
int main(int argc, char* argv[]) { vector<int> vec;
int i, p; cout << "Anzahl eingeben: "; cin >> p; for(i = 0; i < p; i++) vec.push_back(i);
for(i = p; i > 0; i--) cout << vec[i-1] <<" "; }
Benefit is that you can access the vector like an array and you are not bound to any array restrictions. Resizing is done by the OS and you do not have to bother about this.
Mit freundlichen Grüßen, Axel Finger
Erreichbar bei der Daimler-Chrysler AG unter: Tel. +49 (0)7031 / 90 - 41516 Fax: +49 (0)711 / 3052 163951 E-Mail: gigatronik.finger@daimlerchrysler.com
Axel Finger DIAGNOSE (DI/TB)
GIGATRONIK Gesellschaft für Automobil- elektronikentwicklung mbH Hortensienweg 21 70374 Stuttgart
Tel: +49 (0)711 / 84 96 09 - 0 Fax: +49 (0)711 / 84 96 09 - 99 Email: Axel.Finger@gigatronik.com
ged.suse@ntlworld.com 12.12.2003 09:51 Bitte antworten an ged.suse
An: suse-programming-e@suse.com Kopie: Thema: [suse-programming-e] text file into array (c++)
How do I insert a textfile of stings, separated by carrige returns into an string array, or an array of pointers.
I have been scratching my head over this for a while now and eventually have to turn for help.
Here is as far as I have got, although I now know it won't work.
ifstream fpi("names.dat"); char *name[5];
if (!fpi) { cout << "Error opening file\n"; exit(0); }
for (int i=0; i<5; ++i) { fpi >> *(name+i); }
for (int j=0; j<5; ++j) { cout << *(name+j) << endl; }
fpi.close();
As you can see, i'm still pretty new to programming.
Thanks,
Ged.
----------------------------------------- Email provided by http://www.ntlhome.com/
-- To unsubscribe, email: suse-programming-e-unsubscribe@suse.com For additional commands, email: suse-programming-e-help@suse.com Archives can be found at: http://lists.suse.com/archive/suse-programming-e
----------------------------------------- Email provided by http://www.ntlhome.com/
On Fri, Dec 12, 2003 at 09:50:23AM +0000, ged.suse@ntlworld.com wrote:
Hi Axel,
My understanding was that by using an array of pointers, you won't need to resize any arrays. The strings pointed to by the pointers will automatically resize to the length of the sting + 1 for the terminating char ('\0').
I have not read about vectors as yet, but they are sure to be next on my list.
That is incorrect. Allocating pointers only provides enough space to hold the memory address of the first character in the string. You would still need to allocate a character array to hold the string itself, and you would have to resize that character array yourself. Your best bet would be to read about vectors and strings. They are both part of the C++ standard library, and they both manage their own memory. Victor
participants (2)
-
ged.suse@ntlworld.com
-
Victor R. Cardona