Fill a 2d array from a file in C [duplicate]
This question is an exact duplicate of:
fscanf into a 2d array in C
2 answers
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.
c arrays
marked as duplicate by user1118321, Jean-François Fabre
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 20:20
This question was marked as an exact duplicate of an existing question.
add a comment |
This question is an exact duplicate of:
fscanf into a 2d array in C
2 answers
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.
c arrays
marked as duplicate by user1118321, Jean-François Fabre
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 20:20
This question was marked as an exact duplicate of an existing question.
1
Please read the help pages, especially "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also take the tour and read about how to ask good questions and this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.
– 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 Minimal, Complete, and Verifiable example
– chux
Nov 24 '18 at 16:30
1
How did stackoverflow.com/q/53436800/2410359 not work for you? That question is still open.
– chux
Nov 24 '18 at 16:31
add a comment |
This question is an exact duplicate of:
fscanf into a 2d array in C
2 answers
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.
c arrays
This question is an exact duplicate of:
fscanf into a 2d array in C
2 answers
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.
This question is an exact duplicate of:
fscanf into a 2d array in C
2 answers
c arrays
c arrays
asked Nov 24 '18 at 16:17
Stan MarshStan Marsh
216
216
marked as duplicate by user1118321, Jean-François Fabre
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 20:20
This question was marked as an exact duplicate of an existing question.
marked as duplicate by user1118321, Jean-François Fabre
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 27 '18 at 20:20
This question was marked as an exact duplicate of an existing question.
1
Please read the help pages, especially "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also take the tour and read about how to ask good questions and this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.
– 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 Minimal, Complete, and Verifiable example
– chux
Nov 24 '18 at 16:30
1
How did stackoverflow.com/q/53436800/2410359 not work for you? That question is still open.
– chux
Nov 24 '18 at 16:31
add a comment |
1
Please read the help pages, especially "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also take the tour and read about how to ask good questions and this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.
– 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 Minimal, Complete, and Verifiable example
– chux
Nov 24 '18 at 16:30
1
How did stackoverflow.com/q/53436800/2410359 not work for you? That question is still open.
– chux
Nov 24 '18 at 16:31
1
1
Please read the help pages, especially "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also take the tour and read about how to ask good questions and this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.
– Some programmer dude
Nov 24 '18 at 16:21
Please read the help pages, especially "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also take the tour and read about how to ask good questions and this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.
– Some programmer dude
Nov 24 '18 at 16:21
1
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 Minimal, Complete, and Verifiable example
– chux
Nov 24 '18 at 16:30
"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 Minimal, Complete, and Verifiable example
– chux
Nov 24 '18 at 16:30
1
1
How did stackoverflow.com/q/53436800/2410359 not work for you? That question is still open.
– chux
Nov 24 '18 at 16:31
How did stackoverflow.com/q/53436800/2410359 not work for you? That question is still open.
– chux
Nov 24 '18 at 16:31
add a comment |
2 Answers
2
active
oldest
votes
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,%dn", &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] = '';
}
// 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.
add a comment |
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? is a useful read here. Better to check the return value offscanf().
– chux
Nov 24 '18 at 16:45
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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,%dn", &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] = '';
}
// 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.
add a comment |
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,%dn", &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] = '';
}
// 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.
add a comment |
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,%dn", &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] = '';
}
// 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.
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,%dn", &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] = '';
}
// 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.
answered Nov 24 '18 at 21:04
chuxchux
83.4k872152
83.4k872152
add a comment |
add a comment |
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? is a useful read here. Better to check the return value offscanf().
– chux
Nov 24 '18 at 16:45
add a comment |
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? is a useful read here. Better to check the return value offscanf().
– chux
Nov 24 '18 at 16:45
add a comment |
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]);
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]);
answered Nov 24 '18 at 16:38
Marko PanushkovskiMarko Panushkovski
111
111
1
Why is “while ( !feof (file) )” always wrong? is a useful read here. Better to check the return value offscanf().
– chux
Nov 24 '18 at 16:45
add a comment |
1
Why is “while ( !feof (file) )” always wrong? is a useful read here. Better to check the return value offscanf().
– chux
Nov 24 '18 at 16:45
1
1
Why is “while ( !feof (file) )” always wrong? is a useful read here. Better to check the return value of
fscanf().– chux
Nov 24 '18 at 16:45
Why is “while ( !feof (file) )” always wrong? is a useful read here. Better to check the return value of
fscanf().– chux
Nov 24 '18 at 16:45
add a comment |
1
Please read the help pages, especially "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also take the tour and read about how to ask good questions and this question checklist. Lastly learn how to create a Minimal, Complete, and Verifiable example.
– 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 Minimal, Complete, and Verifiable example
– chux
Nov 24 '18 at 16:30
1
How did stackoverflow.com/q/53436800/2410359 not work for you? That question is still open.
– chux
Nov 24 '18 at 16:31