1

My question is that I would like to get rid of null's by replacing them with the phrase "Apples". If someone can please take a look at my code and tell me where I went wrong and what fix I can apply to do that task that would be great.

Input(Text file):

A B C

E F G

I J K L

char *map[10][10];
int loadMap(char * filename){
    FILE *fp;
    int row = 0;
    int col= 0;

    char buffer[1000];
    char phrase[100] = "pass";

    fp = fopen(filename,"r");
    if(fp == NULL){
      perror(filename);
      return(1);
 }

 char ch;
while (1) {
    fscanf(fp, "%s", buffer);

    map[row][col] = (char *)malloc(sizeof(char) * (strlen(buffer) + 1));

    strcpy(map[row][col], buffer);

    ch = fgetc(fp);

    if (ch == ' ') {
        col += 1;
    }
    else if (ch == '\n') {
        row += 1;
        col = 0;
    }
    else if (ch == EOF) {
        break;
    }
}

 return(0);
}
void DisplayMap(int size){

  int row, columns;

  for (row=0; row<DUNGEONSIZE; row++)
  {
      for(columns=0; columns<DUNGEONSIZE; columns++)
           printf("%s  ", map[row][columns]);
      printf("\n");
   }
  }

Output:

A  B  C  (null)  (null)  (null)  (null)  (null)  (null)  (null)
E  F  G  (null)  (null)  (null)  (null)  (null)  (null)  (null)
I  J  K  L  (null)  (null)  (null)  (null)  (null)  (null)
L  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)
(null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)
(null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)
(null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)
(null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)
(null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)
(null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)  (null)
Arjun Dhiman
  • 164
  • 8
  • i assume that map is a 2d array of pointer to char? – engineer14 Jun 28 '16 at 04:16
  • Yes. char *map[10][10]; – Arjun Dhiman Jun 28 '16 at 04:17
  • 2
    Please provide a [mcve]. Also, what's the input and where's that output coming from (your code as shown has no output)? And finally, take the time to fix up the indentation of your code to make it more readable. – kaylum Jun 28 '16 at 04:19
  • I just added the input. Now take a look. – Arjun Dhiman Jun 28 '16 at 04:25
  • And the rest of the [mcve]? You haven't even shown the code that produces that output. – kaylum Jun 28 '16 at 04:26
  • Sorry, it got deleted on the edit. – Arjun Dhiman Jun 28 '16 at 04:27
  • `printf("%s ", map[row][columns]);` --> `printf("%s ", map[row][columns] ? map[row][columns] : "Apples");`. This assumes `map` is a global variable (uatomatically initialised to all 0) or has been explicitly initialised to all 0 (again, code you have not shown). – kaylum Jun 28 '16 at 04:33
  • Is there a way to put Apples into the array, instead of converting null to Apples in display function. – Arjun Dhiman Jun 28 '16 at 04:36
  • `map[row][col] = "Apples";`. Do that for all `map` entries that are not set from the file (or init all the entries to "Apples" and overwrite the relevant entries with the file contents). – kaylum Jun 28 '16 at 04:39
  • What's wrong with the obvious: `if(map[row][columns] == NULL) printf("Apples"); else printf("%s ", map[row][columns]);`? – user253751 Jun 28 '16 at 05:13
  • @immibis He cares more about storing it than displaying it. – engineer14 Jun 28 '16 at 05:35
  • @Zero -- break bad habits early. Do *NOT* cast the return of `malloc`. It is totally unnecessary. See: [**Do I cast the result of malloc?**](http://stackoverflow.com/q/605845/995714) for thorough explanation. – David C. Rankin Jun 28 '16 at 06:27
  • Please edit your post and fix the indention. It's a complete mess - I won't read this question. – Lundin Jun 28 '16 at 06:46

2 Answers2

1

It looks like your problem resides in your display size function. It is going up to the size of the array (map). All the elements you did not get to (due to switching lines/rows) are left uninitialized, in this case, which is NULL.

When displaying, you want to check and make sure it is not NULL. If map was declared as a global variable, you should be okay by checking it is null before displaying (ie if(map[x][y] == NULL...). If it wasn't, you will have to populate the array as shown below.

BTW: as @kaylum said, using printf("%s ", map[row][columns] ? map[row][columns] : "Apples"); in your display function is the must efficient method if map is a global.

Now, if you want to use apples instead. In your populate function, when you see a new line, run from that index until the end and populate with apple. And then after you find your end of file, populate EVERYTHING LEFT with apple:

while{...
 else if (ch == '\n') {
    while (col < 10){ //or col < sizeof(map[0])/sizeof(map[0][0])
       col++
       //malloc map here
       strcpy(map[row][col], "Apple"); }
    row += 1;
    col = 0;
 }...}
 for (; row < 10; row++){ 
  for(; col < 10; col++){ 
    //malloc map[row][col] here
    strcpy(map[row][col],"APPLE");} 
    col = 0;}
engineer14
  • 607
  • 4
  • 13
0

fscanf reads till Either space or '\n'.

First of all, how is NULL represented in the file.

When it reaches NULL, it will stop reading. Now if you have represented NULL as space. You can do fgetc, because it reads till '\n' and continue with your logic. You can declare

char *s = malloc(..) snprintf(s,"%s","Apple")

and assign the char* [][] entries to s.

Do let me know, if I understood wrongly.

while(1)
{
  int num = fscanf(fp, "%s", buffer);
  int ch;
  if(!feof(fp) && num==0)
  // meaning either end of file or not read (space/'\n')
  {
     while(!feof(fp) )
       {
        ch = fgetc(fp);
        if(ch == '\n')
         {
            row += 1;
            col = 0;
            break;
         }
        else if(ch == ' ')
        {

           map[row][col] = strdup("Apple");
           col += 1;
        }
       }
      if(ch == '\n')
        continue;
  }
  if(feof(fp))
     break;

  map[row][col] = (char *)malloc(sizeof(char) * (strlen(buffer) + 1));
  strcpy(map[row][col], buffer); 
}

In your example, once the NULLs start no non null value comes. If it does you can use rewind(fp) to move the pointer 1 step back and continue using fscanf.

Ishan
  • 46
  • 4