file load
2 posters
RUSES :: Part 1 :: Programming :: Coursework
Page 1 of 1
file load
trying to load a 2d array atm but cant work out the pointers
probly just somethimg stupid just cant get my head around it atm
- Code:
{
char *level = "one.txt";
char maze[ROWS][COLS];
FILE *mapfile = fopen(level, "r");
int row,col;
//openfile(level, maze[ROWS][COLS]);
for (row = 0; row < ROWS; row++) {
for (col = 0; col < COLS; col++) {
maze[row][col] = ' ';
}
}
for (row = 0; row < ROWS; row++)
{
for (col = 0;col < COLS;col++)
{
maze[row][col] = fgetc(mapfile);
}
}
probly just somethimg stupid just cant get my head around it atm
Re: file load
Not sure if this is the problem, but you seem to be using C so:
You forgot the null terminator on your string
If that fails try:
Failing that:
You forgot the null terminator on your string
- Code:
char *level = "one.txt\0";
If that fails try:
- Code:
char level[8] = "one.txt";
Failing that:
- Code:
char *level = (char*) malloc (sizeof(char)*8);
memset(level,'\0',8);
level = "one.txt";
//do stuff with level
free(level);
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: file load
If it's the actual file reading that's failing, I'll sum the maze array is a 2d array of characters, try
If you are using C++ this would be much easier
- Code:
int i;
for (i=0;i<ROWS;i++)
maze[ROWS] = getline(&maze[ROWS],COLS*sizeof(char),mapfile);
If you are using C++ this would be much easier
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: file load
Just noticed your variable declaration which means you are using C++ but you are using lots of C concepts - the main being:
Therefore I think the problems are the non-null terminated string, and when you are reading the file you aren't accounting for the '\n' characters in the file - which the getline function accounts for
- You should take a more OO approach to file reading: http://www.cplusplus.com/doc/tutorial/files/
- Strings should be using the inbuilt C++ 'string' type (instead of char arrays) as you don't need to worry about length management
Therefore I think the problems are the non-null terminated string, and when you are reading the file you aren't accounting for the '\n' characters in the file - which the getline function accounts for
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: file load
thanks for that helpped get my head around it
im not sure how c++ like it is now but i have a working file load into a 2d array how i wanted it soo all good!!
im not sure how c++ like it is now but i have a working file load into a 2d array how i wanted it soo all good!!
- Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string maze[20][40];
string line;
ifstream myfile ("one.txt");
if (myfile.is_open())
{
int x = 0, y=0;
for (x=0;x<20; )
{
getline (myfile,line);
maze[x][y] = line;
x++;
}
myfile.close();
}
else cout << "Unable to open file";
int loopx =0,loopy =0;
do{
loopx=0;
do{
string output;
output = maze[loopy][loopx];
cout<< output;
loopx++;
}while(loopx < 40-1);
cout<<endl;
loopy++;
}while(loopy<20);
return 0;
}
RUSES :: Part 1 :: Programming :: Coursework
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum