Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 3rd, 2006, 7:39 AM   #1
Ade
Hobbyist Programmer
 
Ade's Avatar
 
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0 Ade is an unknown quantity at this point
How much can a C++ programme extract from a .txt file?

Hey guys, I've given up on trying to use MySQL++ with my graphics package for my game and have taken to using .txt files to hold the game data.

My programme is working fine with a few lines of data. However, when I try to add more lines to the file (the file is just lines of 8 numbers ie. 00100110 or 01108121) my programme just doesn't run. I don't get any errors with my code but I get the classic "suchandsuch.exe has encountered a problem and needs to close. We are sorry for the incovenience. Please tell microsoft about this problem."

Just wondering if programmes can only handle a small ammount of data from a .txt file? Does it use a lot of system resources?


Ta Ade
__________________
Don't wound what you can't kill
Ade is offline   Reply With Quote
Old May 3rd, 2006, 7:46 AM   #2
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
No, you can use big files too, I've used a program which made a file of 11 GigaBytes, of course you don't load the whole file into memory when working with files that size. The limit on FAT32 are 4 GB files I believe.

Why not use SQLite? It's a self-contained, zero-configuration SQL database engine. PHP 5 uses it also.

You'll have to show us some code before we can say why suchandsuch.exe crashes.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 3rd, 2006, 7:55 AM   #3
Ade
Hobbyist Programmer
 
Ade's Avatar
 
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0 Ade is an unknown quantity at this point
This is the section of code that deals with extracting information from the file. When I comment this section out the programme no longer crashes.

	string line;
	int map_x[102];
        int map_y[102];
	int map_terrain[102];
	int map_extra[102];
	string map_x_st;
	string map_y_st;
	string map_terrain_st;
	string map_extra_st;
	int map_array_counter = 0;

  ifstream myfile ("mapfile.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
			map_x_st = line.substr (0,2);
			map_y_st = line.substr (2,2);
			map_terrain_st = line.substr (4,1);
			map_extra_st = line.substr (5,1);
			map_x[map_array_counter] = atoi(map_x_st.c_str());
			map_y[map_array_counter] = atoi(map_y_st.c_str());
			map_terrain[map_array_counter] = atoi(map_terrain_st.c_str());
			map_extra[map_array_counter] = atoi(map_extra_st.c_str());

		map_array_counter++;
    }
    myfile.close();
  }

This is a small section of the .txt file (mapfile.txt)

000010
000110
000210
000310
000410
000510
000610
000710
000810
000910
010010
010110
010210
010310
010410
010510
010610
010710
010810
010910

I used to this I was a good programmer, then I stopped using PHP! C++ is a lot of fun, but mainly it just wants to make me pull my hair out!!


Ta Ade.
__________________
Don't wound what you can't kill
Ade is offline   Reply With Quote
Old May 3rd, 2006, 8:04 AM   #4
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Why dont you modify your program to store the information in a dilimited way.
010410,010510,010610,010710,010810,010910,...
That will save you a lot of space on the disk.
OpenLoop is offline   Reply With Quote
Old May 3rd, 2006, 8:43 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by OpenLoop
Why dont you modify your program to store the information in a dilimited way.
010410,010510,010610,010710,010810,010910,...
That will save you a lot of space on the disk.
He is using a newline as delimter instead of a comma.

@Ade: Works fine for me..
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

// TM DaWei
int uhOh(char * troubleInRiverCity)
{
	cerr << troubleInRiverCity << endl;
	return 1;
}

int main()
{
	string line;
	int map_x[102];
	int map_y[102];
	int map_terrain[102];
	int map_extra[102];
	string map_x_st;
	string map_y_st;
	string map_terrain_st;
	string map_extra_st;
	int map_array_counter = 0;

	ifstream myfile ("mapfile.txt");
	if (myfile.is_open())
	{
		while (! myfile.eof() )
		{
			getline (myfile, line);
			map_x_st = line.substr (0,2);
			map_y_st = line.substr (2,2);
			map_terrain_st = line.substr (4,1);
			map_extra_st = line.substr (5,1);
			map_x[map_array_counter] = atoi(map_x_st.c_str());
			map_y[map_array_counter] = atoi(map_y_st.c_str());
			map_terrain[map_array_counter] = atoi(map_terrain_st.c_str());
			map_extra[map_array_counter] = atoi(map_extra_st.c_str());

			// CONSOLE OUTPUT PLEASE :)
			cout << "map_x_st: " << map_x_st << endl;
			cout << "map_y_st: " << map_y_st << endl;
			cout << "map_terrain_st: " << map_terrain_st << endl;
			cout << "map_extra_st: " << map_extra_st << endl;

			cout << "map_x[" << map_array_counter << "]: " << map_x[map_array_counter] << endl;
			cout << "map_y[" << map_array_counter << "]: " << map_y[map_array_counter] << endl;
			cout << "map_terrain[" << map_array_counter << "]: " << map_terrain[map_array_counter] << endl;
			cout << "map_extra[" << map_array_counter << "]: " << map_extra[map_array_counter] << endl;

			cout << "map_array_counter: " << map_array_counter << endl << endl;


			map_array_counter++;
		}
		myfile.close();
	}
	else
		return uhOh("Error opening file");
	return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates

Last edited by nnxion; May 3rd, 2006 at 8:53 AM. Reason: Console output
nnxion is offline   Reply With Quote
Old May 3rd, 2006, 8:49 AM   #6
Ade
Hobbyist Programmer
 
Ade's Avatar
 
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0 Ade is an unknown quantity at this point
It works fine for me too a that stage but I need the programme to handle like 1000ish lines. I've been able to make it take 100 six-digit numbers by putting them in a continuous line which is more than I was able to do having them on different lines. However, when I attempt more than about 100 it crashed again, I've tried using multiple files each with 50 entries in it but it doesn't like that either.

Could I use some sort of header file with the definitions in it? Would VS prefer that?
__________________
Don't wound what you can't kill
Ade is offline   Reply With Quote
Old May 3rd, 2006, 9:01 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
That code above is not the problem. Maybe you can zip your stuff, and post it?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old May 3rd, 2006, 9:08 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
This is plucked right out of my nether regions, but a good guess is you are trying to store more in an array than you allocated room for. Over and above that is the fact that
while (! myfile.eof() )
is a problematic way of winding up the read process successfully will all data intact and all data used. It's been covered thoroughly on this forum, as well as on other sites.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old May 3rd, 2006, 9:27 AM   #9
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
I'm starting to think I'm missing something here, lol. Does the following not sum up the problem?

int map_x[102];
int map_array_counter = 0;

// ...

while (! myfile.eof() )
{
   map_x[map_array_counter]
   
   // ...

   map_array_counter++;  // if ( map_array_counter > 101 ) { BOOM!; }
}
Cache is offline   Reply With Quote
Old May 3rd, 2006, 9:42 AM   #10
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Cache
I'm starting to think I'm missing something here, lol.
Hmm yeah, lol.

@David: Maybe I'm just forgetful, but I thought that only feof() was the problem because of:
Quote:
Originally Posted by C standard
7.19.10.2 The feof function

Synopsis

1 #include <stdio.h>
int feof(FILE *stream);

Description
2 The feof function tests the end-of-file indicator for the stream pointed to by stream.

Returns
3 The feof function returns nonzero if and only if the end-of-file indicator is set for stream.
It checks the end-of-file indicator, not the stream itself. In C++ the eof-bit is set instead of the 'end-of-file indicator', of course. I can't find more information on it, not on the forums nor on google.
As you have probably seen the feof problem is described in further detail here.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:37 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC