I need to write a program which compares a string of numbers such as "256432" with an entire list of numbers located on a separate text file.
There's about 1000 different entries on the text file but I'm not sure of the exact amount therefore I want to somehow dynamically allocate an array having the size of it dependant on the amount of entries on the file.
But... Since I want to store it as a char array too it will have to be 2D. So I want to dynamically allocate the size of the array pertaining to the amount of entries but I do know that each field will only be 6 chars long.
How can I possibly allocate the size of the array as I loop through each line of the file until eof()? Also how wll I take in the input from the file and store it into the array? Thanks.
why not just make a struct to include both data types, then make a single dimensional array to account for that, or you could define the first dimension then have the second dynamic.
Exactly, I want to have the second dynamic as I don't feel like counting up all the fields myself. I do know the length of each entry which is 6. My main issue is the syntax for inputting the values from the file into the 2D array.
Also, how will I be able to count the amount of entries inside the text file in the first place when I don't have an array to sift through each line? (as I'm dynamically allocating)
try using a <vector> of strings. have a look at this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <vector> //don't forget these!
#include <string>
#include <fstream>
usingnamespace std;
vector<string> strvec;
string mystr;
ifstream file;
file.open("name of your text file", ifstream::in);
while(getline(file,mystr))
{
strvec.push_back(mystr); // this adds the string mystr which is one line of your file to
// the vector of strings
}
It was actually specified that you cannot use vectors for this particular assignment. Is there a simpler way of determining the amount of lines in a file without using arrays? (since i'm dynamically allocating) I appreciate the help though.
ifstream file;
int n = 0;
string mystr;
file.open("name of your text file", ifstream::in);
while(!getline(file,mystr).eof())
{
++n;
}
string strarray[n];
file.seekg(0, ios_base::beg);
n = 0;
while (!getline(file,mystr).eof())
{
strarray[n] = mystr;
++n;
}
class strList
{
private:
string m_str;
strList * next;
public:
string Get(){return m_str;}
strList * Add(string p_str)
{
if (next != NULL) return NULL;
next = new strList(p_str);
return next;
}
strList * Next() {return next;}
strList(string p_str)
{
next = NULL;
m_str = p_str;
}
~strList()
{
if(next) delete next;
}
};
int main()
{
ifstream file;
strList * first, *current;
string mystr;
file.open("filename", ifstream::in);
getline(file,mystr);
first = new strList(mystr);
current = first;
while(!getline(file,mystr).eof())
{
current = current->Add(mystr); //every time a string is added, the pointer is assigned to the new object
}
file.close();
current = first;
while (current != NULL) //this code loops through the list of strings
{
mystr = current->Get();
//code to check if mystr matches
current = current->Next();
}
}