473,670 Members | 2,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Opening large text file makes program unhappy.

I am writing a program will have a function where it opens a text file,
and fills a list box line by line from the text file. It works fine,
except for when I tried to load a dictionary which is ~ 50mb, it
appears to freeze. When I press Ctrl Alt Del I can see that it is still
loading the file - the mem usage if shooting up. Its been going for a
while and the memory usage is currently at verging on 600,000K. If i
click on the program it stops responding.

Is there a way to stop it from behaving like this? (I think I remember
reading somthing about DoEvents but I'm not sure). Also, is there a way
to stop the entire text file loading into the RAM, or processing it bit
by bit. Eventally it will process the loaded data so I guess there will
be no real need to load it into the text box.

Thanks, and sorry for the ignorance

Jun 25 '06 #1
8 2088
ha****@gmail.co m wrote:
I am writing a program will have a function where it opens a text file,
and fills a list box line by line from the text file. It works fine,
except for when I tried to load a dictionary which is ~ 50mb, it
appears to freeze. When I press Ctrl Alt Del I can see that it is still
loading the file - the mem usage if shooting up. Its been going for a
while and the memory usage is currently at verging on 600,000K. If i
click on the program it stops responding.

Is there a way to stop it from behaving like this? (I think I remember
reading somthing about DoEvents but I'm not sure). Also, is there a way
to stop the entire text file loading into the RAM, or processing it bit
by bit. Eventally it will process the loaded data so I guess there will
be no real need to load it into the text box.

Thanks, and sorry for the ignorance


Check your documentation for a function that yields control back to the
OS. When doing memory and time intensive things, you should periodically
(read every few thousand lines) call this yielding function.

Inmatarian
2993 Forever.
Jun 25 '06 #2
ha****@gmail.co m wrote:
I am writing a program will have a function where it opens a text file,
and fills a list box line by line from the text file. It works fine,
except for when I tried to load a dictionary which is ~ 50mb, it
Millibits? How is that even possible? ;-)
appears to freeze. When I press Ctrl Alt Del I can see that it is still
loading the file - the mem usage if shooting up. Its been going for a
while and the memory usage is currently at verging on 600,000K. If i
click on the program it stops responding.
Can you post the function here so we can see it? See this FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
Is there a way to stop it from behaving like this?
Certainly. There's also a good chance your program has a bug in it.
(I think I remember
reading somthing about DoEvents but I'm not sure).
Not in standard C++, which is the topic of this group. You might ask in
a group dedicated to your platform. See this FAQ:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9
Also, is there a way
to stop the entire text file loading into the RAM, or processing it bit
by bit. Eventally it will process the loaded data so I guess there will
be no real need to load it into the text box.


Of course there are alternate strategies. Try some out and ask here
again if you have a C++ *language* question, not an algorithmic one
(for that, try comp.programmin g or similar).

Cheers! --M

Jun 25 '06 #3
Thanks a lot for the replys, they're really helpful. That makes sense
about giving control back to the os every few thousand lines. Oops
sorry not mb, MB :-)
Here is the code:

int ReadFile( HWND *hWnd )
{

fstream file( "words.txt" ,ios::in);
if( file.fail() )
{
MessageBox(NULL ,"Error finding file. Make sure words.txt is in the
same directory as this program","Statu s",MB_OK);
PostQuitMessage (0);
}
vector<string> lines;
string line;
int n = 0;

while( !file.eof() )
{
getline( file, line );
lines.push_back ( line );
n++;
}
file.close();

int lno = n;
int x = 0;

for( x; x < lno; x++ )
{
SendDlgItemMess age(*hWnd, IDC_WORDLIST, LB_ADDSTRING, 0,
(LPARAM)lines[x].c_str());
}

return 0;
}
IDC_WORDLIST is a list box. So would I tell it to take a break every
time x is divisible by 3000? Also, how would I tell it to take a break
- is there something like sleep( n ) in c++. Sorry I'm so ignorant, I
only started with C++ a few days ago, before that I'd only used PHP,
tiny bit of perl, javascript, vb.

Would this question count as language or alogrithmic? Sorry if its in
the wrong place, this i my first post in a newsgroup ever :(

Jun 25 '06 #4
haz...@gmail.co m wrote:
Thanks a lot for the replys, they're really helpful. That makes sense
about giving control back to the os every few thousand lines. Oops
sorry not mb, MB :-)
Here is the code:

int ReadFile( HWND *hWnd )
{

fstream file( "words.txt" ,ios::in);
How about just ifstream?
if( file.fail() )
Prefer:

if( !file )
{
MessageBox(NULL ,"Error finding file. Make sure words.txt is in the
same directory as this program","Statu s",MB_OK);
PostQuitMessage (0);
}
vector<string> lines;
string line;
int n = 0;

while( !file.eof() )
{
getline( file, line );
Bad form (cf.
http://www.parashift.com/c++-faq-lit...html#faq-15.2). What
if there is some other failure? Prefer this:

while( getline( file, line ) )
{
lines.push_back ( line );
You might consider using vector<>::reser ve() to prevent so many
reallocations along the way.
n++;
}
file.close();
Unnecessary. fstreams are closed automatically, and unless you have a
good reason for closing it before the end of scope, it's standard
practice to omit this.

int lno = n;
int x = 0;

for( x; x < lno; x++ )
{
SendDlgItemMess age(*hWnd, IDC_WORDLIST, LB_ADDSTRING, 0,
(LPARAM)lines[x].c_str());
}
This part is off-topic here, but there may well be ways to handle this
better. Ask in a Microsoft newsgroup, some of which are listed here:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9

return 0;
}
IDC_WORDLIST is a list box. So would I tell it to take a break every
time x is divisible by 3000? Also, how would I tell it to take a break
- is there something like sleep( n ) in c++. Sorry I'm so ignorant, I
only started with C++ a few days ago, before that I'd only used PHP,
tiny bit of perl, javascript, vb.
Try asking in a group dedicated to your platform. In standard C++,
sleeping only wastes time. <OT>On many platforms, sleeping lets other
threads/processes run, which may or may not do what you want. If you
created a "worker thread" (hint: search the docs for that), it
might.</OT>

Would this question count as language or alogrithmic?


Half and half?

Cheers! --M

Jun 25 '06 #5
* ha****@gmail.co m:
Thanks a lot for the replys, they're really helpful. That makes sense
about giving control back to the os every few thousand lines.
Generally it can, but in this specific case it probably won't.

Oops sorry not mb, MB :-)
Here is the code:

int ReadFile( HWND *hWnd )
{

fstream file( "words.txt" ,ios::in);
if( file.fail() )
{
MessageBox(NULL ,"Error finding file. Make sure words.txt is in the
same directory as this program","Statu s",MB_OK);
PostQuitMessage (0);
}
vector<string> lines;
string line;
int n = 0;

while( !file.eof() )
{
getline( file, line );
lines.push_back ( line );
n++;
}
file.close();

int lno = n;
int x = 0;

for( x; x < lno; x++ )
{
SendDlgItemMess age(*hWnd, IDC_WORDLIST, LB_ADDSTRING, 0,
(LPARAM)lines[x].c_str());
}

return 0;
}


In addition to the comments from M.Limber:

* Reading line by line is generally extremely inefficient. In theory
the std::ifstream abstraction will fix some of that for you, but in
practice it adds to the mess instead. For a 50 MiB file I'd read
the contents raw into a suitably large pre-allocated buffer, or I'd
use OS-specific functionality to map the file to memory.

* Many GUI things, regardless of OS, have strict limits on how much
data they can handle. The limit for a Windows listbox is about
one thousandth of what you're trying to push into it.

* It's not a good idea to /combine/ responsibilitie s, as you have
done here (both reading data and filling a listbox), unless by that
combination you can capitalize on some huge advantage. Try instead
to /separate/ responsibilitie s. I.e., one function for reading, and
some other function that uses the result.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 25 '06 #6
> * Many GUI things, regardless of OS, have strict limits on how much
data they can handle. The limit for a Windows listbox is about
one thousandth of what you're trying to push into it.


From another point of view, the OP is simply making a UI mistake. Even
if a list box can handle that much information, a user would probably
not want to scroll through 50 lines.

Regards,
Ben
Jun 25 '06 #7
I V
On Sat, 24 Jun 2006 19:57:18 -0700, hazmaz wrote:
Thanks a lot for the replys, they're really helpful. That makes sense
about giving control back to the os every few thousand lines. Oops
Rather than periodically returning control to the OS, it might be simpler
to change your design so that your function does a small amount of work
each time it's called, rather than all the work at once. Like:

class FileReader
{
std::vector<std ::string> lines_;
std::ifstream file_;

void on_complete();
void on_error();

bool read_line();
public:
FileReader(cons t char* name)
: file_(name)
{ }

static bool callback(void* user_data);
};
// Read a line, return true if the function
// should be called again to read more lines,
// false otherwise. (You probably really
// want to read more than one line at a time,
// but this is easier for the purposes of the
// example).
bool FileReader::rea d_line()
{
if( file_.fail() )
{
if( file_.eof() )
on_complete();
else
on_error();
return false;
}
std::string line_;
std::getline(fi le_, line);
lines_.push_bac k(line);
return true;
}

// This is a static function so that you can pass
// a pointer to it to whatever API your system
// offers to do delayed execution.
bool FileReader::cal lback(void* user_data)
{
FileReader* r = static_cast<Fil eReader*>user_d ata;

return r->read_some_line s();
}

And implement on_complete and on_error to do whatever you need to do to
handle the completion of the read, or the error condition. Depending on
how your system works, you may have to have the callback function request
that it be invoked again, something like:

bool FileReader::cal lback(void* user_data)
{
FileReader* r = static_cast<Fil eReader*>user_d ata;

if( r->read_some_line s() )
{
call_me_later(F ileReader::call back, user_data);
return true;
}
else
{
return false;
}
}

Replacing "call_me_la ter" with whatever is the appropriate function on
your platform.

BTW,
for( x; x < lno; x++ )
{
SendDlgItemMess age(*hWnd, IDC_WORDLIST, LB_ADDSTRING, 0,
(LPARAM)lines[x].c_str());
}


Do you need to read into the vector and _then_ add to the listbox? Or
could you just add the data to the listbox as you read it in?

People on windows programming groups will probably be able to help you
more - they'll be familiar with event-based programming, and with the
specific API functions you'll need to call.
Jun 25 '06 #8
ha****@gmail.co m wrote:
Thanks a lot for the replys, they're really helpful. That makes sense
about giving control back to the os every few thousand lines. Oops
sorry not mb, MB :-)


You shouldn't have to, the OS should be able to take care of it's own
scheduling. Unless you are running at a real time or very high other
priority.

--
Ian Collins.
Jun 25 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1559
by: possibilitybox | last post by:
this code here: def wordcount(lines): for i in range(len(lines)/8): words = lines.split(" ") if not locals().has_key("frequency"): frequency = {} for word in words: if frequency.has_key(word):
4
5969
by: Matthew Crema | last post by:
Hello, Say I have 1000 text files and each is a list of 32768 integers. I have written a C program to read this data into a large matrix. I am using fopen in combination with fscanf to read the data in. However, it takes about 20 seconds to complete and I wonder if there is a faster way. For example, I found that I could use 'fread' to read the data into a string that looks like this:
11
3592
by: aldrin | last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0 compiler with great difficulty.There is no problem with running this under KDevelop in linux. Any help would be greatly appreciated. Enter an interesting string. Too many cooks spoil the broth Error opening C:\myfile.txt for writing. Program termnated. This application has requested the Runtime to terminate it in an unusual way.
8
2128
by: Claudio Grondi | last post by:
Here an example of what I mean (Python 2.4.2, IDLE 1.1.2, Windows XP SP2, NTFS file system, 80 GByte large file): Traceback (most recent call last): File "<pyshell#1>", line 1, in -toplevel- f = file('veryBigFile.dat','r+') IOError: No such file or directory: 'veryBigFile.dat'
8
6938
by: gazza67 | last post by:
Hi, I want to do something that I thought would be simple but i cant seem to work it out, perhaps someone out there could help me. I want to browse for a file (it will be a word document), save the file name to a string and then at some later stage open that file with word. The operating system will be windows 2000 (dont know if that makes a difference or not).
12
1430
by: mohdalibaig | last post by:
Is it possible to open other files in C during main() execution. Example, i want to open a wave file that generates a sound when ever main() function is called.
6
2017
by: nikhil | last post by:
Hello every one, Can any one tell me the syntax for opening and closing a file in associated program from C++. For an instance I got a MS word file. How can I open it in MS word from a console program in C++. and also how can I close any application from the console program ( like closing MS word opened in previous step). I'm using Visual Studio 2005.
0
976
by: =?Utf-8?B?Vmlua2k=?= | last post by:
Hello Everyone, I need to open a text file with .net remoting. This text file resides on another server. My server program running on the server has this method that opens the text file System.Diagnostics.Process.Start(filePath); My client program has a button which I click and it call the server program method that opens the file. When I click the button, the file opens on the server itself, is it possible that I can see that file...
34
5339
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> os.startfile("C:\Documents and Settings\Alex\My Documents\My
0
8466
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8590
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7410
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4387
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2035
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.