-3

ERROR

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 
#define size 4 

void read_world(FILE *inp,x[][size])

  int i, j, t;
  if (inp==NULL) {
    printf("File is missing\n");
 return (0);
  }
  else {
    for (i=0;i < 4;i++) {
      for (j=0;j < 4;j++) {
        (fscanf(inp, "%d", &t));  // error only reads first row of text
        x[i][j]=t;
      }
      printf("%d\n", x[i][j]);

    }
  }
  return(0);
}
int main(void) {
  int i, j;
  int g[4][4];
  FILE*inp=fopen("world.txt", "r");  // reads file
  read_world(inp, g);
  return (0);
}

Code needed to read text from .txt file called world. Its a very simple code that do sent seem to read properly, the file consist of numbers and characters like "0"s and " * " which are arranged in a 4 by 4 matrix. the outcome is just to print the content of the file on the screen.

  • 1
    What's the problem you have with the code? What is it supposed to do? You might want to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). – Some programmer dude Oct 25 '15 at 13:17
  • How is it not clear....read text and characters from file thats it...zzzzz – Reep Sanders Oct 25 '15 at 13:20
  • What is the contents of the file? What is the actual and expected output from the program? And does the code really compile? How can you return values from a function declared to not return anything? – Some programmer dude Oct 25 '15 at 13:24
  • Numbers and characters in 4 by 4 matrix >1 1 1 1 * * * 1 – Reep Sanders Oct 25 '15 at 13:25
  • Can you *please* edit the question to include that information, the contents, the actual and expected output, all the details needed to make your question a good question that people want to answer (see my links in the first comment). – Some programmer dude Oct 25 '15 at 13:29
  • I just want to read the file bro thats it....i cant get it to read the integers correctly. – Reep Sanders Oct 25 '15 at 13:30
  • Also, how do you expect to read non-numeric values with the `"%d"` format? Besides other problems, that is a big one. – Some programmer dude Oct 25 '15 at 13:30
  • Done..So what do you suggest – Reep Sanders Oct 25 '15 at 13:34
  • **Please edit the question to include relevant information** as @JoachimPileborg has said. – Spikatrix Oct 25 '15 at 13:39

2 Answers2

1

I think this might be useful to read. Use the return value from fscanf to check for data

Community
  • 1
  • 1
Subito
  • 36
  • 1
  • 5
  • Ignoring the return value won't help solve the problem, however actually *checking* the return value from `fscanf` would give the OP a hint to the problem. – Some programmer dude Oct 25 '15 at 14:12
1

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define size 10
void read_world(FILE *inp, int x[][size])
{
 int i, j,t;
  
 if (inp == NULL)
 {
  printf("File is missing\n");
  return (0);
 }
 else
 {
  for (i = 0; i < 10; i++)
  {
   for (j = 0; j < 10; j++)
   {
    (fscanf(inp,"%d", &t));
    x[i][j]=t;
   }
   
  }
 }
 return(0);
}

int main(void)
{
 int i, j;
 int g[10][10];
 FILE*inp = fopen("world.txt", "r");
 read_world(inp, g);
 for (i = 0; i < 10; i++)
 {
  for (j = 0; j < 10; j++)
  {
   g[i][j];
   if (g[i][j] == 0)
    printf("0 ");
   else
    printf("* ");
  }
  printf("\n");
 }

 return (0);
}

Screenshots

Simon Hayter
  • 3,131
  • 27
  • 53