help on this text file i/o code

C

c

hi all
i read this code from "sudoku.c", the source code for the console game
sudoku on my ubuntu box.
and i have some questions with the text i/o operations in this code.

1)
static char line[ 80 ];
static char title[ 80 ];
FILE * f; (has been initialised, i put it here for clarity)

/* Skip lines until a '%' is found */
line[ 0 ] = ' ';
while( '%' != line[ 0 ] )
if( 0 == fgets( line, sizeof( line ), f ) )
return -1;

here i am not sure of the use of fgets
does it read from file line by line ? what keeps track of which line
we will read next ?
is there a sort of internal position marker ?



2) the above code is followed by

/* Read optional title, and removing trailing whitespace */
for( p = line + 1 ; *p && isspace( *p ) ; ++p )
;

if( *p )
{
for( q = title ; '\0' != ( *q++ = *p++ ) ; )
;
--q;
while( isspace( *--q ) )
;
*++q = '\0';
}
else
strcpy( title, "(untitled)" );

i'm really confused about the pointer uses here
and how this code achieves its function, namely, removing trailing
whitespace

btw: i'm not able to judge the coding style here, whether it's the
right way to code in c. so any comments on that will be helpful too.
also can anyone point me to some well commented and well coded
opensource c program? i'm sure there's a lot out there
thanks in advance
 
M

Mark Bluemel

c said:
hi all
i read this code from "sudoku.c", the source code for the console game
sudoku on my ubuntu box.
and i have some questions with the text i/o operations in this code.

1)
static char line[ 80 ];
static char title[ 80 ];
FILE * f; (has been initialised, i put it here for clarity)

/* Skip lines until a '%' is found */
line[ 0 ] = ' ';
while( '%' != line[ 0 ] )
if( 0 == fgets( line, sizeof( line ), f ) )
return -1;

here i am not sure of the use of fgets

If you are not sure about fgets() I suggest reading a book or an online
tutorial - see the resources links from http://www.c-faq.com
2) the above code is followed by

/* Read optional title, and removing trailing whitespace */
for( p = line + 1 ; *p && isspace( *p ) ; ++p )
;

skip over the leading '%' and any leading whitespace.

If there's anything to be processed, i.e. we haven't reached the end of
the input line.

The pointer p points to our current position in the input line.

The pointer q will be used to point to our position in the title buffer.
{
for( q = title ; '\0' != ( *q++ = *p++ ) ; )
;

Copy from after white space into "title".
--q;
while( isspace( *--q ) )
;

Work backwards over trailing whitespace
*++q = '\0';

Terminate "title"
}
else
strcpy( title, "(untitled)" );

i'm really confused about the pointer uses here
and how this code achieves its function, namely, removing trailing
whitespace

It's probably not the clearest code for a newbie...
 
A

Army1987

hi all
i read this code from "sudoku.c", the source code for the console game
sudoku on my ubuntu box.
and i have some questions with the text i/o operations in this code.

1)
static char line[ 80 ];
static char title[ 80 ];
FILE * f; (has been initialised, i put it here for clarity)

/* Skip lines until a '%' is found */
line[ 0 ] = ' ';
while( '%' != line[ 0 ] )
if( 0 == fgets( line, sizeof( line ), f ) )
return -1;

here i am not sure of the use of fgets
does it read from file line by line ?
Yes, it does (or better it reads up to sizeof(line) characters).
what keeps track of which line we will read next ?
is there a sort of internal position marker ?
The same one which keeps track of which character we will read
next using getc().
2) the above code is followed by

/* Read optional title, and removing trailing whitespace */
for( p = line + 1 ; *p && isspace( *p ) ; ++p )
;
This increments p until it points to non-whitespace.
if( *p )
{
for( q = title ; '\0' != ( *q++ = *p++ ) ; )
;
This copies bytes from p to q until they're nonzero...
Now q points on the terminating null of title.
And now to the last nonzero character of title.
while( isspace( *--q ) )
;
This decrements q until it points to non-whitespace...
*++q = '\0';
And sets the next character to '\0', thus terminating the string.
}
else
strcpy( title, "(untitled)" );

i'm really confused about the pointer uses here and how this code
achieves its function, namely, removing trailing whitespace
See above.
 
R

Richard

c said:
hi all
i read this code from "sudoku.c", the source code for the console game
sudoku on my ubuntu box.
and i have some questions with the text i/o operations in this code.

1)
static char line[ 80 ];
static char title[ 80 ];
FILE * f; (has been initialised, i put it here for clarity)

/* Skip lines until a '%' is found */
line[ 0 ] = ' ';
while( '%' != line[ 0 ] )
if( 0 == fgets( line, sizeof( line ), f ) )
return -1;

here i am not sure of the use of fgets
does it read from file line by line ? what keeps track of which line
we will read next ?
is there a sort of internal position marker ?

Don't ask here for that - use google or your Ubuntu documentation and
then ask if you are unsure. To the c library documentation:

In Ubuntu (in a terminal)

sudo apt-get install build-essential
sudo apt-get install manpages-dev

then

info fgets

2) the above code is followed by

/* Read optional title, and removing trailing whitespace */
for( p = line + 1 ; *p && isspace( *p ) ; ++p )
;

While there are valid characters (*p) and they are spaces (isspace) then
move forward (++p). In other words the comment is wrong. It should say
"remove prefix whitespace, read optional title and remove trailing white
space" or similar.
if( *p )
{
for( q = title ; '\0' != ( *q++ = *p++ ) ; )

aargh. I detest it when the constant is first. Anyway .... copy the
title into the title buffer pointed to by q including any trailing
whitespace. He could have used strcpy.

look at last character+1 in the title.
while( isspace( *--q ) )
;

go back to find the first non white space
*++q = '\0';

mark the end of the title.

He could have done it a different way.

1) find first non white space.

2) keep copying char by char and maintaining a pointer to the last non
whitespace character.

e.g (2)

while(((isspace(*p))?1:(lastPtr=q)) && (*q++=*p++));
*++lastPtr='\0';

I didn't test the above but would be interested in seeing it ripped apart
by the standards commission :-; What the above says is (or is supposed
to say)

while we copy the string, if the character is not a white space then
store a pointer to it so that we can later terminate the string after
the last non white space. You would need NULL checks for lastptr I
suspect. Anyway, just to get you thinking .....
 
C

c

c said:
hi all
i read this code from "sudoku.c", the source code for the console game
sudoku on my ubuntu box.
and i have some questions with the text i/o operations in this code.
1)
static char line[ 80 ];
static char title[ 80 ];
FILE * f; (has been initialised, i put it here for clarity)
/* Skip lines until a '%' is found */
line[ 0 ] = ' ';
while( '%' != line[ 0 ] )
if( 0 == fgets( line, sizeof( line ), f ) )
return -1;
here i am not sure of the use of fgets
does it read from file line by line ? what keeps track of which line
we will read next ?
is there a sort of internal position marker ?

Don't ask here for that - use google or your Ubuntu documentation and
then ask if you are unsure. To the c library documentation:

In Ubuntu (in a terminal)

sudo apt-get install build-essential
sudo apt-get install manpages-dev

then

info fgets


2) the above code is followed by
/* Read optional title, and removing trailing whitespace */
for( p = line + 1 ; *p && isspace( *p ) ; ++p )
;

While there are valid characters (*p) and they are spaces (isspace) then
move forward (++p). In other words the comment is wrong. It should say
"remove prefix whitespace, read optional title and remove trailing white
space" or similar.


if( *p )
{
for( q = title ; '\0' != ( *q++ = *p++ ) ; )

aargh. I detest it when the constant is first. Anyway .... copy the
title into the title buffer pointed to by q including any trailing
whitespace. He could have used strcpy.

look at last character+1 in the title.
while( isspace( *--q ) )
;

go back to find the first non white space
*++q = '\0';

mark the end of the title.

He could have done it a different way.

1) find first non white space.

2) keep copying char by char and maintaining a pointer to the last non
whitespace character.

e.g (2)

while(((isspace(*p))?1:(lastPtr=q)) && (*q++=*p++));
*++lastPtr='\0';

I didn't test the above but would be interested in seeing it ripped apart
by the standards commission :-; What the above says is (or is supposed
to say)

while we copy the string, if the character is not a white space then
store a pointer to it so that we can later terminate the string after
the last non white space. You would need NULL checks for lastptr I
suspect. Anyway, just to get you thinking .....
}
else
strcpy( title, "(untitled)" );
i'm really confused about the pointer uses here
and how this code achieves its function, namely, removing trailing
whitespace

thanks a lot
especially to richard's explanation, very clear
one question though: why *p can be a test of valid character if(*p)
what does the program mean by valid character ?
thx again
 
A

Army1987

aargh. I detest it when the constant is first. Anyway .... copy the
title into the title buffer pointed to by q including any trailing
whitespace. He could have used strcpy.
Well, he needed the end of the copied string in q. Maybe strchr()
after strcpy(), but why scan the string twice when you can scan
it once?
He could have done it a different way.

1) find first non white space.

2) keep copying char by char and maintaining a pointer to the last non
whitespace character.

e.g (2)

while(((isspace(*p))?1:(lastPtr=q)) && (*q++=*p++));
*++lastPtr='\0';
Good point, but how 'bout to write it in a clearer way?
I didn't test the above but would be interested in seeing it ripped
apart by the standards commission :-; What the above says is (or is
supposed to say)

while we copy the string, if the character is not a white space then
store a pointer to it so that we can later terminate the string after
the last non white space. You would need NULL checks for lastptr I
suspect.
Why? The string has at least a non-whitespace character, namely
the null. Replace isspace(*p) with *p && isspace(*p) (just because
the Standard doesn't forbid a locale to be so brain-dead to
consider '\0' whitespace), and you're done.
 
P

pete

It's called a "file position indicator".
The file position indicator is one of the things
recorded by the FILE type object associated with the stream.
Don't ask here for that

Why not?
 
B

Barry Schwarz

snip 100 lines
thanks a lot

Please trim the amount of quoted material to what is relevant to your
addition.
especially to richard's explanation, very clear
one question though: why *p can be a test of valid character if(*p)
what does the program mean by valid character ?

*p is the character that p points to. If that character is '\0', then
it is the nul character that terminates the string. In that case, "if
(*p)" will evaluate to false and the statement following the if will
not execute.

In many types of string processing, it is common not to include the
terminal nul (such as strlen). In this case, the phrase "While there
are valid characters" meant "until we reach the end of the string".


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top