"Michael Mair" <
[email protected]>
wrote in message
Hi Magix,
or any
better
It will print num:
11111
22222222
333
You can also use fscanf():
while (1) {
int howmany = fscanf(fp, "<%"MAKESTRING(FIELDWIDTH)
"[^>]%*[^<]<%*[^>]><%*[^>]>",
str);
The format string does the following:
It gobbles a '<', then scans for up to FIELDWIDTH non-'>'
characters into str. As we do not know whether this covers
the first "<tag>", we now scan for the beginning of
the second, then gobble the second and third tags, respectively.
The leading ' ' is for gobbling any left over white spaces
from previous line.
Note, however, that this does not give you the certainty that
your line was of the required format. You could scan for the
final '>' or try to fgetc() the following '\n' to be on the
safe side.
It might be a good idea to #define left/right delimiter
characters instead of '<','>'. You can insert them quite
in the same way as the FIELDWIDTH.
Why so complicated?
What happens if there is a space in the input string?
What happens if there aren't exactly three pairs of tags on a
line (which I don't see any requirement for - the example just
happens to have three on each line)?
For what the OP is asking for, if I understand it correctly, you
don't need to perform string operations at all.
This is the way I read what is being asked for (which,
admittedly, involved reading between the lines):
1) Lines consist of strings of characters delimited by '<'
(LDELIM) and '>' (RDELIM) characters.
2) There may be one or more such strings on a given line.
3) Output should consist only of the first such string on each
line.
IF we are willing to assume (for the first pass at the algorithm)
that the strings really do obey Rule #1, then we have the
following:
1) WHILE: End of File not reached
1.1) WHILE: (Next character is NOT LDELIM)
1.1.1) Do nothing - go on to next character.
1.2) WHILE: (Next character is NOT RDELIM)
1.2.1) Print the character.
1.3) Print a newline character.
1.4) WHILE: (Next character is NOT the end of the line)
1.4.1) Do nothing - go on to the next character.
Each of the tests should include checks for EOF and (if desired)
newline characters in unexpected places.
At the risk of doing someone's homework (hopefully they'll at
least learn something from seeing the problem stated clearly, an
algorithm developed based on the problem statement, and code
developed based on the algorithm).
#include <stdio.h> /* FILE, NULL, fopen(), putchar(), printf()
*/
#define EXIT_OK (0)
#define EXIT_FAIL_NoOpen (1)
#define FILENAME "data.txt"
#define LDELIM ('<')
#define RDELIM ('>')
int main(void)
{
int c;
FILE *fp;
fp = fopen(FILENAME, "rt");
if(NULL == fp)
{
printf("Error opening %s for reading - aborting.\n",
FILENAME);
return EXIT_FAIL_NoOpen;
}
do /* Until end of file is reached */
{
/* Strip up to and including first occurrence of LDELIM
*/
while( (EOF != (c = getc(fp))) && ('\n' != c) &&
(LDELIM != c) )
/* EMPTY LOOP */;
/* Print out characters up to first occurrence of
RDELIM */
while( (EOF != (c = getc(fp))) && ('\n' != c) &&
(RDELIM != c) )
putchar(c);
putchar('\n');
/* Strip remaining characters on line */
while( (EOF != (c = getc(fp))) && ('\n' != c) )
/* EMPTY LOOP */;
} while (EOF != c);
fclose(fp);
return EXIT_OK;
}
As usual, I welcome comments and criticisms.
NOTE TO THE OP: By using the subject line you did, you imply that
your question is specifically dealing with a specific function -
namely fgets() - and people might assume that, for whatever
reason, you either have to use that function or are trying to
learn more about that function by doing so. Your post content
implies that this is not the case. A better subject line might
have been something like: "Trying to output initial part of each
line".