-
October 2nd, 2012, 04:43 PM
#1
storing filenames in an array
Hello,
I want to create filenames such as Query.student.1.bin and save them in an array.
I have no problem when I don't include a "." between student and number "1" but as I add a "." there my code does not run. ( it does complie but crashes during the run)
string *filename_str;
filename_str =new string[Number_X_Axis];
for(i=0;i<Number_X_Axis;i++)
filename_str[i]="Query."+Table_Name+"."+convertInt(i)+".bin";
I would appreciate any of your comments on this.
Best
PA
-
October 2nd, 2012, 04:52 PM
#2
Re: storing filenames in an array
Code:
string *filename_str;
filename_str =new string[Number_X_Axis];
Learn to use vectors.
Code:
#include <vector>
#include <string>
typedef std::vector<std::string> StringArray;
//...
StringArray filename_str(Number_X_Axis);
for(i=0;i<Number_X_Axis;i++)
filename_str[i]="Query."+Table_Name+"."+convertInt(i)+".bin";
In this day and age of C++, there is hardly a reason to use new[] / delete[] to create a dynamic array. Use container classes such as vector -- that is what vector is designed for.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 2nd, 2012 at 04:56 PM.
-
October 2nd, 2012, 05:06 PM
#3
Re: storing filenames in an array
Originally Posted by palireza
I have no problem when I don't include a "." between student and number "1" but as I add a "." there my code does not run. ( it does complie but crashes during the run)
You more than likely corrupted the memory due to incorrect usage of new / delete (which is why you should use containers, so these mistakes are not made).
In actuality, we don't know what you really did, since you didn't post your actual program that you're running. We don't know the data you're using, the flow of the program, where these lines are called/used, etc. We don't even know what "Table_Name", or convertInt() is.
Post a full, but simple program that demonstrates the error.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 2nd, 2012 at 05:08 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|