I am trying to create an array, with the data coming from a text file. I am then trying to count how many pieces of data there are in that array. In the array, there are two pieces of data, however when the counter executes, it says there is only 1.
Here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int n = 0;
ifstream show_version_library;
show_version_library.open("version_library.txt");
char show_version_library_var[1024];
if (show_version_library.is_open())
{
while (!show_version_library.eof())
{
show_version_library >> show_version_library_var;
}
}
// string replace * to ,
string show_version_library_actual( show_version_library_var );
int position = show_version_library_actual.find( "*" ); // find first space
while ( position != string::npos )
{
show_version_library_actual.replace( position, 1, "," );
position = show_version_library_actual.find( "*", position + 1 );
}
// string replace ^ to "
string show_version_library_actual2( show_version_library_actual );
int position2 = show_version_library_actual2.find( "^" ); // find first space
while ( position2 != string::npos )
{
show_version_library_actual2.replace( position, 1, "\"" );
position2 = show_version_library_actual2.find( "^", position2 + 1 );
}
// convert show_version_library_actual2 to char*
char* lib2;
lib2 = &show_version_library_actual2[0];
// array counter
char* library_actual[100] = {
lib2
};
char* p;
while (p != '\0')
{
n++;
p = library_actual[n];
}
cout << "\nN is " << n << " that was n\n"; // should be outputting 2
show_version_library.close();
system("PAUSE");
return 0;
}
(code has been reformatted so that it will show up as code here)
And in version_library.txt
"1.8_HACK"*"1.8"*
I tried outputting "lib2" and it comes up with
"1.8_HACK", "1.8",
.. as it should...
However, when I output library_actual[0], the whole line comes up, instead of just "1.8_HACK".
I am fairly new to cpp, so please excuse my terrible code..if any.
Thank you!