RUSES
Would you like to react to this message? Create an account in a few clicks or log in to continue.
May 2024
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  

Calendar Calendar


file load

2 posters

Go down

file load  Empty file load

Post  John Thu Mar 08, 2012 12:14 pm

trying to load a 2d array atm but cant work out the pointers

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
John
John
Admin

Posts : 36
Join date : 2012-02-23
Age : 32

https://ruses.forumotion.co.uk

Back to top Go down

file load  Empty Re: file load

Post  Alex Thu Mar 08, 2012 8:16 pm

Not sure if this is the problem, but you seem to be using C so:

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
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

file load  Empty Re: file load

Post  Alex Thu Mar 08, 2012 8:20 pm

If it's the actual file reading that's failing, I'll sum the maze array is a 2d array of characters, try

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 Razz
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

file load  Empty Re: file load

Post  Alex Thu Mar 08, 2012 8:23 pm

Just noticed your variable declaration which means you are using C++ but you are using lots of C concepts - the main being:

  • 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
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

file load  Empty Re: file load

Post  John Thu Mar 08, 2012 9:40 pm

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!! Very Happy
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;
}
John
John
Admin

Posts : 36
Join date : 2012-02-23
Age : 32

https://ruses.forumotion.co.uk

Back to top Go down

file load  Empty Re: file load

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum