1

So i have a 20 x 20 array, and its all filled with -1. I also have an input.txt which contains some numbers, for example :
2,3
5,6

How can i fill my array so that array[0][0] will be 2, array[0][1] will be 3, array[1][0] will be 5, array[1][1] will be 6, and every other element will be -1.

I've tried with a for loop thats using fscanf until there is a new line, but that won't work.

Stan Marsh
  • 31
  • 1
  • 8
  • 1
    Please read [the help pages](http://stackoverflow.com/help), especially ["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). Also [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask) and [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly learn how to create a [mcve]. – Some programmer dude Nov 24 '18 at 16:21
  • 1
    "I've tried with a for loop thats using fscanf until there is a new line, but that won't work." --> Post that code and explain how it did not work. Post a [mcve] – chux - Reinstate Monica Nov 24 '18 at 16:30
  • 1
    How did https://stackoverflow.com/q/53436800/2410359 not work for you? That question is still open. – chux - Reinstate Monica Nov 24 '18 at 16:31

2 Answers2

0

Try using "end of file"(eof), where m and n are the rows and columns

for (i = 0; i < m && !feof(input); i++)
  for (j = 0; j < n && !feof(input); j++)
  fscanf(input, "%f", &a[i][j]);

If the size of the array (m and n) is also given in the file write an if condition before

if (!feof(input))
  fscanf(input, "%d %d", &m, &n);
for (i = 0; i < m && !feof(input); i++)
      for (j = 0; j < n && !feof(input); j++)
      fscanf(input, "%f", &a[i][j]);
  • 1
    [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/q/5431941/2410359) is a useful read here. Better to check the return value of `fscanf()`. – chux - Reinstate Monica Nov 24 '18 at 16:45
0

I've tried with a for loop thats using fscanf until there is a new line, but that won't work.

Variations on fscanf(in, "%d,%d\n", &x, &y), as in OP's prior question, fail to detect end-of-line. The '\n' in the format will match any white-space on input including '\n', ' ', '\t',etc.

Simplistic usage of fscanf(..., "%d",...) can readily fail as "%d" will consume all leading white-space with no discrimination between '\n' and other white-spaces.

How can i fill my array ...

Although possible to use fscanf() to solve this task, consider fgets(), strtol().

The best approach is to use fgets() to reach a line: all characters up to and including the one final '\n'. Then use strtol(), strtoll(), etc. to read the integers.

long integers and spacing each needs reasonable less than 21 characters. To accommodate extra leading zeros spacing, etc, let use 2x the anticipated size needs.

#define CHAR_PER_NUMBER 21
#define NUM_PER_LINE 20
#define LINE_NUM 20
#define LINE_SIZE (NUM_PER_LINE * CHAR_PER_NUMBER * 2)

long array[LINE_NUM][NUM_PER_LINE];

// read data
for (int i = 0; i < LINE_NUM; i++) {
  char buf[LINE_SIZE + 1]; // +1: room for the appended the null character
  if (fgets(buf, sizeof buf, in) == NULL) {
    buf[0] = '\0';
  }
  // Now parse the line
  char *p = buf;
  for (int j = 0; j < NUM_PER_LINE; j++) {
    char *endptr;
    array[i][j] = strtol(p, &endptr, 10);
    if (p == endptr) {
      array[i][j] = -1; // no conversion  
    }
    p = endptr; // advance to the next number
  }
}

Additional concerns including handling pathological long lines, values outside the long range, I/O error handling and efficiency details.

Should input consists of text representing floating-point, a more generous max size per value is warranted.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256