|
|
#1 | |
|
Registered User
Join Date: Jun 2009
Posts: 2
|
Sorry to make this my first post.
I've been reading all over the internet about arrays and I just can't seem to find exactly what I need. All I want is to store strings into an array. The hope is that I can make an array with strings like infile1.txt, infile2.txt ect ect. Then use the fstream to open a file based on a varible. So if array[32] = infile32.txt then somewhere down the line I could have a function that would be called something like filetoopen(string). So I'd have a while loop. that called the filetoopen() function using a varible like string helpfulVariable ; That would cycle through the array. Is this making any sense? While looking all the internet I couldn't find how to make an array of strings. Not an array of characters that happen to be words like C. ACTUAL strings. Can anyone point me in the correct direction? |
|
|
|
|
|
|
#2 | |
|
Un-Ripped
Join Date: Oct 2004
Location: Montreal, Canaduh!
Posts: 1,968
|
CString array[32] = {"infile1.txt","infile2.txt....};
Replace ... by actual string, or better, use a loop to construct the filename. CString array[32]; for (int i=0; i < 32; i++) array[i].FormatMessage((_T("infile%1!d!.txt"),i); See http://msdn.microsoft.com/en-us/libr...43(VS.60).aspx
__________________
Evga X58|I7 965 3.8GHz|Ultra 120 Extreme|Evga GTX 480 SC|2 x Intel X25-M 80GB RAID 0|Intel 520 - 240 GB|WD Black Caviar 1TB|6GB Corsair XMS3 1600C8 |Corsair 1000W PSU|Antec Twelve-Hundred|DELL U3011|Logitech G500|Logitech G19 |
|
|
|
|
|
|
#3 |
|
Registered User
Join Date: Jun 2009
Posts: 2
|
Thanks I knew it was something simple. Can't actually use the loop cause its gonna but stuff like torch.txt and wall.txt but it made a simple example. Thank you again.
Edit: Ok, I have a follow up question. How do I reference it? Assume that I want the array called locationArray. I want to use a new line for each entry of the array for readiability and ability to change it later on. Would this work? CString locationArray[4] locationArray[0] = "example0.txt" ; locationArray[1] = "example1.txt" ; locationArray[2] = "example2.txt" ; locationArray[3] = "example3.txt" ; would that be correct? assuming so would I then do. string storeVariable = locationArray[2] ; cout << storeVariable ; Thanks again for any help. I am very new to arrays. |
|
|
|
|
|
#4 | |
|
*BANNED*
Join Date: Jan 2008
Posts: 184
|
Is there a specific reason you are mixing and matching std::string and CString?
One is standard C++, the other is a Microsoft-specific class. More importantly, where is your book? Something is way wrong if you feel the need to be asking these trivial of questions on a forum. I recommend Accelerated C++ by Koenig and Moo - it is a great book for a beginner to C++. |
|
|
|
|
|
|
#5 |
|
Registered User
Join Date: Jan 2005
Location: Rochester, MN
Posts: 4,018
|
This being C++, you almost certainly don't want to be using raw arrays. The idiomatic C++ method is to use containers like vectors and lists. You can find references for using them all over the place, I like using this one.
That said, I agree with Zhivago. You probably want to find yourself a basic C++ intro that would explain all of this to you. |
|
|
|
![]() |
| Thread Tools | |
|
|