![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4
![]() |
Why dont you modify your program to store the information in a dilimited way.
010410,010510,010610,010710,010810,010910,... |
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
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 |
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Oct 2004
Location: England, UK
Posts: 139
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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() )
__________________
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 |
|
|
|
|
|
#9 |
|
Hobbyist
Join Date: Sep 2005
Posts: 266
Rep Power: 4
![]() |
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!; }
} |
|
|
|
|
|
#10 | ||
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
@David: Maybe I'm just forgetful, but I thought that only feof() was the problem because of: Quote:
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 |
||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|