I am writing a program that reads the elements of a 2D array. The
program should tell how many elements there are in the first row.
Getting my program to count the elements of the first row is where I
am having problems.
This program is being written in C, compiled with gcc under Unix.
The file I read (using redirective "<") is this:
3 5 7 8 1
6 7 10 1 2
3 4 5 6 7
8 1 12 13 14
15 6 7 8 11
12 13 14 5 6
7 8
There are 5 elements in the first row.
What I thought is that I'd use a counter that would be incremented
after every scan of each element, and when the program reaches the end
of the first row, the counter stops counting.
Now, my problem is when checking for the end of line. I've used '\n',
'\0', '\r' but none worked, because those are treated as integers, for
example: the ascii value of new line '\n' is 10, the ascii value of
carriage return '\r' is 13, so the counter stops counting only when it
finds either 10 or 13, in which case the counter tells me that there
are 7 elements in the first row or 18.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Does anyone have any idea as to how I can check for that end of
line??????????
Here's a copy of the code I've written.
#include <stdio.h>
int main() {
/* r is row suffix, c is column suffix, chars is the element counter*/
int r = 0, c = 0, chars = 0;
/* pic is a 2D array with 256 elements in each row and column
because that's the larges value will be tested with */
int pic[256][256] = {0};
/* scan the first element */
scanf("%d", &pic[r][c]);
/* Here's the problem
I'm checking for new line with '\n' but it stops counting
when it encounters a 10.
I've tried with '\r' but stops when it encounters a 13.
'\0' only stops at the end of file.
How can I check for the end of line???????? */
while ( pic[r][c] != '\n' ) {
chars++;
c++;
scanf("%d", &pic[r][c]);
}
printf("elements in first row are = %d\n", chars);
return 0;
}
Thank you for your help!
--
comp.lang.c.moderated - moderation address: cl...@plethora.net
> /* Here's the problem
> I'm checking for new line with '\n' but it stops counting
> when it encounters a 10.
> I've tried with '\r' but stops when it encounters a 13.
> '\0' only stops at the end of file.
> How can I check for the end of line???????? */
> while ( pic[r][c] != '\n' ) {
Wow. No offence, but this certainly breaks this year's record for the
most silly error in a C program posted to c.l.c.m ;-)
Your scanf() reads numbers from that file, not characters. There's no
way it can make sense to compare the output of such a scanf() to a
character like '\n'.
What you should be doing is: read your textbook again, and this time
concentrate on what methods of reading input from a file exist, and
which of those directly support the concept of a "line of text". A
hint: scanf(), unless wielded by an expert, doesn't.
--
Hans-Bernhard Broeker (bro...@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
use fgets() to read a single line from the file into a sufficiently
large buffer (it gets more complicated if you cannot guarantee a
sufficiently large one because you now have to apply the various
procedures to detect incomplete reading of a line)
Now you have various options such as applying sscanf to the buffer until
it fails.
>
>Here's a copy of the code I've written.
>
>
>#include <stdio.h>
>
>int main() {
>
>/* r is row suffix, c is column suffix, chars is the element counter*/
>
>int r = 0, c = 0, chars = 0;
>
>
>/* pic is a 2D array with 256 elements in each row and column
>because that's the larges value will be tested with */
>
>int pic[256][256] = {0};
>
>
>/* scan the first element */
>scanf("%d", &pic[r][c]);
>
>
>/* Here's the problem
> I'm checking for new line with '\n' but it stops counting
> when it encounters a 10.
> I've tried with '\r' but stops when it encounters a 13.
> '\0' only stops at the end of file.
> How can I check for the end of line???????? */
>
>while ( pic[r][c] != '\n' ) {
>
>chars++;
>c++;
>scanf("%d", &pic[r][c]);
>
>}
>
>printf("elements in first row are = %d\n", chars);
>
>return 0;
>}
>
>Thank you for your help!
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Try
while (!feof(stdin) && getchar() != '\n')
{
instead.