Thread: File with character and number

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    19

    File with character and number

    I ask this question before but here is another way.

    I have a text file that says de3 dn4 dw5 ds8

    the de, dn , dw and ds stand for the position of the door(d), and the number stand for the increment of the cursor...
    i'm using ncurse for this.

    so if the program reads de3, its will post a door to east side at position 3 and so on.

    My question is how do i get the "de" and the number and place it to a array or pointer so i can get it to draw the door at that position

    here is what i have:
    i hope its more clear.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ncurses.h>
    #include <stdlib.h>
    
    #define door '+'
    int main ()
    {
        initscr()
        FILE * pFile;
        int c;
      
        pFile=fopen ("de3.txt","r");
        if (pFile==NULL) perror ("Error opening file");
        else
        {
    
        c = fgetc(pFile);
         while(c)
         {
             if( c == 'd' && c == 'e')
                {
                    mvaddch(0, takenum[0], door); /* takenum[0] will be the number
    position of the door*/
                }
                else
                if( c == 'd' && c == 'n')
                {
                    mvaddch(0, takenum[1], door); /* takenum[0] will be the number
                    position of the door*/
                }
                }
    
        }
        fclose (pFile);
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > My question is how do i get the "de" and the number and place it to a array or pointer so i can get it to draw the door at that position
    I think you answered your own question - use an array.

    Creating some kind of 'map' of your game world is pretty much essential.

    Code:
    void handleCommand(const char *c,char board[][100])
    {
      char  item;
      char  direction;
      int   distance;
    
      // analyse for dw3 etc
      sscanf(c,"%c%c%d",&item,&direction,&distance);
      
      if (item == 'd') {
        if (direction == 'e') {
          board[x-direction][y] = DOOR;
        } else if (direction == 'n') {
          board[x][y-direction] = DOOR;
        }
      }
    }
    
    
    int main()
    {
      char board[100][100];
      initscr()
      FILE *pFile;
      char command[10];
    
      pFile = fopen("de3.txt", "r");
      if (pFile == NULL)
        perror("Error opening file");
      else {
        while (fscanf(fp, "%s", command) == 1) {
          handleCommand(command,board);
        }
      }
      fclose(pFile);
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    i changed some stuffs but the thing is..its doesnt get the other item, its only gets the first one, i tried putting it into an array but its still doesnt work, i even tried a do while loop. should i increment the item for it to move and check the other one??

    Code:
    void handleCommand(const char *c,char board[][100])
    {
        char  item;
        char  direction;
        int   distance;
        int x = 0, y = 0;
        
        /* analyse for dw3 etc  */
        sscanf(c,"%c%c%d",&item,&direction,&distance);
        initscr();
        while(item == 'd') {
            if (direction == 'e') {
                mvaddch(distance,0,DOOR);
            }
            if (direction == 'n') {
                mvaddch(0,distance,DOOR);
            }
            
         getch();
         endwin();
        }
    }
    
    
    int main()
    {
        char board[100][100];
        initscr();
        FILE *pFile;
        char command[10];
        
        
        pFile = fopen("room.txt", "r");
        if (pFile == NULL)
            perror("Error opening file");
        else {
            while (fscanf(pFile, "%s", command) == 1) {
                handleCommand(command,board);
            }
        }
        fclose(pFile);
        return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like this.
    Code:
    void handleCommand(const char *c,char board[][100])
    {
      char  item;
      char  direction;
      int   distance;
    
      // analyse for dw3 etc
      sscanf(c,"%c%c%d",&item,&direction,&distance);
      
      if (item == 'd') {
        if (direction == 'e') {
          board[x-direction][y] = DOOR;
        } else if (direction == 'n') {
          board[x][y-direction] = DOOR;
        }
      }
    }
    
    void display ( char board[][100] ) {
        initscr();
        // mvaddch in a loop, to display whatever part of the board is interesting
        getch();
        endwin();
    }
    
    int main()
    {
      char board[100][100];
      initscr()
      FILE *pFile;
      char command[10];
    
      pFile = fopen("de3.txt", "r");
      if (pFile == NULL)
        perror("Error opening file");
      else {
        while (fscanf(fp, "%s", command) == 1) {
          handleCommand(command,board);
        }
        display(board);
      }
      fclose(pFile);
      return 0;
    }
    You read the WHOLE file, then display the whole board just the once.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    so if i understand, mvaddch should be in a loop so its display the board? what is fp...shouldnt it be the file name?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > so if i understand, mvaddch should be in a loop so its display the board?
    Yes

    > what is fp...shouldnt it be the file name?
    No, it should be the opened file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    i still dont understand how to put mvaddch in a loop to display the board...how do i do it?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    In essence, it looks something like this.
    But you need to get the r,c thing the right way round, and if the board is bigger than the screen, select the right part of the board.
    Code:
    for ( r = 0 ; r < 100 ; r++ ) {
      for ( c = 0 ; c < 100 ; c++ ) {
        mvaddch(r,c,board[r][c]);
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    i did it but as a result it only show the first element, and never gets to the rest...so its only gets de3

    Code:
     for ( x = 0 ; x < distance ; x++ ) {
            for ( y = 0 ; y < distance ; y++ ) {
                mvaddch(0,distance,DOOR);
            }
        }

  10. #10
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    ok but now its work but it print the other element only after i click enter.

    how can i print it without clicking enter.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    One last time, stop trying to draw the screen while you're reading the file.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ncurses.h>
    #include <stdlib.h>
    
    #define BOARD_SIZE  10
    
    void display ( char board[][BOARD_SIZE] ) {
      initscr();
      for ( int r = 0 ; r < BOARD_SIZE ; r++ ) {
        for ( int c = 0 ; c < BOARD_SIZE ; c++ ) {
          mvaddch(r,c,board[r][c]);
        }
      }
      refresh();
      getch();
      endwin();
    }
     
    int main()
    {
      char board[BOARD_SIZE][BOARD_SIZE];
      for ( int r = 0 ; r < BOARD_SIZE ; r++ ) {
        for ( int c = 0 ; c < BOARD_SIZE ; c++ ) {
          board[r][c] = ' ';
        }
      }
      // cut here =====
      board[rand()%BOARD_SIZE][rand()%BOARD_SIZE] = '+';
      board[rand()%BOARD_SIZE][rand()%BOARD_SIZE] = '-';
      board[rand()%BOARD_SIZE][rand()%BOARD_SIZE] = '*';
      board[rand()%BOARD_SIZE][rand()%BOARD_SIZE] = '/';
      // cut here =====
      display(board);
      return 0;
    }
    Now, replace the random positions with a call to your function to read the file, and populate the board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    yeah i did this before, its only display + all over the screen 10 times...its doesnt take the position of the + and puts it in that position...

  13. #13
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    Thanks Salem, i got it to work but just a little different from what you suggested...thanks for helping me

  14. #14
    Registered User
    Join Date
    Mar 2013
    Posts
    19
    [solve]

    this is done

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing from an opened file character by character
    By SneakySnake in forum C Programming
    Replies: 1
    Last Post: 12-07-2012, 01:05 PM
  2. how many number of character many be in a variable name?
    By surrounded in forum C Programming
    Replies: 3
    Last Post: 02-26-2009, 05:41 PM
  3. Replies: 7
    Last Post: 01-01-2008, 12:30 PM
  4. Number or character?
    By snaidis in forum C Programming
    Replies: 11
    Last Post: 11-16-2005, 01:26 PM
  5. number to a character
    By steven in forum C Programming
    Replies: 2
    Last Post: 09-06-2001, 02:45 PM

Tags for this Thread