Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read file into array 1

Status
Not open for further replies.

sunaj

Technical User
Feb 13, 2001
1,474
DK
Hi,

I don't know much about C++, so here's an easy one for you:

I have a file from which I want to read through line by line and if the line meets certain criteria, I want to put it into an array.
1) How do you do that in C++? I don't know how long the array should be, can you change the length of an array at runtime? Or please suggest better solution.

2) Is this the best (=fastest, my file is VERY large) way to read through the file?
-------------------------------------------
ifstream inFile("c:\tmp\test.txt");
String inStr;
while(!inFile.eof())
{
getline(inFile,inStr);
if (inStr[78]="0" && inStr[79]="J") {
//Add the line to the array
}
}
------------------------------------------

Thank you in advance
Sunaj
 
char big_array[MAX_STRINGS][MAX_SIZE]

int current_index=0;
while(!inFile.eof() && inFile.peek()!= EOF && counter<MAX_STRINGS)
{
getline(inFile,inStr);
if (inStr[78]=&quot;0&quot; && inStr[79]=&quot;J&quot;) {
strcpy(big_array[counter++],inStr);
}
}

That is the basic idea

Matt
 
Hi,

I got your code working - thank you.
So you are basically saying that there is no way to have dynamic arrays in c++?
My problem is that some of my files are very large (100dres of mb), so I would have to set MAX_STRING to a very large number - not very economical in all the cases were the files are smaller....

Sunaj
 
No... there are ways. The first thing that comes to mind is a linked list.

struct Node{
char data[MAX_SIZE];
Node* next;
};

every time you want to add onto it you just create a new node. The implimentation of a linked list can be found in just about any C++ book or scattered about the internet.

If you have any questions, feel free to ask

Matt
 
Sunaj.
I think the most efficient way would be to use a linked list (maybe a derivative of the CList or CStringList class). This way you would only store the data that you were interested in, and would not have to have a huge array just to be able to cope with worst case scenario.

my code would be
Code:
CString str;
CString m_list;

CStdioFile file;

if (file.open(&quot;TEST.TXT&quot;)) {
    while (file.ReadString(str)) {
        if (str[78] == '0' && str[79] =='J')
            m_list.AddTail(str);
    }
}
Then you can iterate through the list by using a POSITION variable :

Code:
POSITION pos;

for (pos = m_list.GetHeadPosition(); pos; ) {
   CString str = m_list.GetNext(pos);
   // Here you can do what you want with the string
}

Hope this helps :)

Ali
 
Hi guys

Thank for your help.
I actually got this working with a linked list and it works great!
I'm reading a very large file, which contains records of oceanographic data. The file is read into a linked list, the data is sorted by time (>100.000 records) using quicksort, and then written from the linked list to a new file.
I still got a few detail to work out, but I'm almost there...

Sunaj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top