-
need advice/help :)
Hello again. i need help/advice regarding my program here.
My input filename is : px.txt. it contains data like below:
my program will read from first line "!!Begin" and give output
what ever contain between !!Begin and !!End.
!!Begin
Source bserver:bmp_spool:the, Job 80c481ac001f6b07, Inst 80c481ac001f6b07
Title BP Page 003 Date 070402 Black 3
Action Finished Transmit, Ok Date Fri Apr 5 00:02:05 SGT 2002
(2) Source Queue = Ajil_Tabloid
(1) Destination = ajil:ajil1:bmp_spool:the
(3) Dest Queue = Futuro Tabloid
Transmitting to ajil:ajil1:bmp_spool:the
!!End
Here my coding.
#include <stdio.h>
#include <string.h>
int main ( void )
{
FILE *input, *output;
char line[100],
*line2 = "!!Begin",
*line3 = "!!End";
input = fopen ( "px.txt", "r" );
output = fopen ( "pxo.txt", "w" );
while ( fgets ( line, sizeof line, input ) != NULL ) {
/* Remove the trailing newline */
line[strlen( line ) - 1] = '\0';
if ( strcmp ( line, line2 ) == 0 )
{
fgets(line,sizeof line, input);
fputs(line,output);
//printf ( "%s\n", line );
}
}
fclose ( input );
fclose ( output );
return 0;
}
I need output like this inside my output file pxo.txt.
Source bserver:bmp_spool:the, Job 80c481ac001f6b07, Inst 80c481ac001f6b07
Title BP Page 003 Date 070402 Black 3
Action Finished Transmit, Ok Date Fri Apr 5 00:02:05 SGT 2002
(2) Source Queue = Ajil_Tabloid
(1) Destination = ajil:ajil1:bmp_spool:the
(3) Dest Queue = Futuro Tabloid
Transmitting to ajil:ajil1:bmp_spool:the
BUt my coding give output only second line
Source bserver:bmp_spool:the, Job 80c481ac001f6b07, Inst 80c481ac001f6b07
-
First, use the break statement to break out of the while-loop when the strcmp() becomes true.
Second, create a second while-loop just like the first, printing each line until the strcmp() becomes true.
while ( fgets ( line, sizeof line, input ) != NULL )
{
/* Remove the trailing newline */
line[strlen( line ) - 1] = '\0';
if ( strcmp ( line, line3 ) == 0 )
break;
fprintf ( output, "%s\n", line );
}
-
Another option on the comparision is strncmp(), to compare only the first few characters of the strings.
Popular pages
Recent additions