Question

I'm having some problem at writing a matrix in a file, and I can't realise why my code is not working. The code is the following:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

FILE*data, *temp;
char *s;
int ch,i,y,j;
char n,m;
char buffer[10];
char field[10][10];
int main()
{
  i=0;
  data=fopen("data.txt", "r");
  while((ch = fgetc(data))!= EOF)
  {
    if(ch != '\n')
    {
      buffer[i] =ch;
      i++;
    }

  }

  for(y = 0; y<i; y++)
  {
    printf("%c ", buffer[y]);
  }

  fclose(data);
  n=buffer[0];
  m=buffer[1];
  //char field[n][m];
  printf("\n n=%c m=%c\n",n,m);
  for(i=1; i<=n; i++)
  {
    for(j=1; j<=m; j++)
    {
      field[i][j]=0;
    }
  }
  field[buffer[3]][buffer[4]]='c';
  field[buffer[6]][buffer[7]]='e';
  for(i=buffer[9]; i<=buffer[12]; i++)
  {
    for(j=buffer[10]; j<=buffer[11]; j++)
    {
      field[i][j]='y';
    }
  }
  temp=fopen("temp.txt","w");
  for(i=1; i<=n; i++)
  {
    for(j=1; j<=m; j++)
    {
      printf("%d", field[i][j]);
      fprintf(temp,"%",field[i][j]);
      if(j==m)
      {
        fprintf(temp,"\n");
      }
    }
  }
  fclose(temp);


  return 0;
}

Also, I've inserted some code so that the matrix gets printed in the console. There is a file called data.txt, which represents the input data, that contains something like:

56
c11
e16
y4456

the first line gives the dimensions of the matrix, 5X6, the second says that on line 1,column1 in the matrix should be a character 'c', 3rd line says that on line 1, col. 4 should be 'e', and in the last line, there a given the coordinates of 2 corners inside the matrix: line4, col.4, and line 5, col.6. In this space, delimites by these corners, I need to have the character 'y'. on the other spaces of the matrix, I chose to put '0'. I want to write ths matrix in the file "temp.txt", but is not working:(. I kept trying, but useless. Please, give me a hint or something. Thank you in advance!

Was it helpful?

Solution

for(i=1; i<=n; i++)

At this point, n is a char, not a number. It is a char '5' that is equal to 53. Which is why you're getting such a massive matrix. m is a char '6' which is equal to 54 also.

Add int values nNum and mNum:

int nNum, mNum;

/* ...code code */

n=buffer[0];
m=buffer[1];
//char field[n][m];
printf("\n n=%c m=%c\n",n,m);
nNum = (n - '0');
mNum = (m - '0');

Now use nNum and mNum everywhere for your loops and you will get the correct sized matrix.

Your other problem is here:

field[buffer[3]][buffer[4]]='c';
field[buffer[6]][buffer[7]]='e';

See? You're using chars as numbers again. Convert these to integers and then use them, and I think everything should work okay.

OTHER TIPS

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define toN(x) x - '0'

int main(void){
    FILE*data, *temp;
    int ch, i,j, n, m;
    char buffer[10];

    data=fopen("data.txt", "r");
    fgets(buffer, sizeof(buffer), data);
    n = toN(buffer[0]);
    m = toN(buffer[1]);
    char field[n][m];
    printf("\nn=%d m=%d\n", n, m);
    for(i=0; i<n; i++)
        for(j=0; j<m; j++)
            field[i][j]='0';

    while(fgets(buffer, sizeof(buffer), data)){
        int len = strlen(buffer);
        if(buffer[len-1]=='\n')
            --len;
        switch(len){
        case 3:
            field[toN(buffer[1])-1][toN(buffer[2])-1] = buffer[0];
            break;
        case 5:
            for(i=toN(buffer[1])-1;i <= toN(buffer[3])-1;++i)
                for(j=toN(buffer[2])-1;j <= toN(buffer[4])-1;++j)
                    field[i][j] = buffer[0];
            break;
        default:
            //format error!
            break;
        }
    }
    fclose(data);
    temp=fopen("temp.txt","w");
    for(i=0; i<n; i++){
        for(j=0; j<m; j++){
            printf("%c", field[i][j]);
            fprintf(temp,"%c",field[i][j]);
        }
        printf("\n");
        fprintf(temp,"\n");
    }
    fclose(temp);

  return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top