This is C, you might get more responses by posting it in the C board, not the C++ board. Okay, I'll go through the problems then post my version.
>void main ( void )
void main is wrong, the main function returns an int and nothing else.
>FILE *stream;
>FILE *stream2;
One problem, these names are hidious. What does stream do? What does stream2 do? Make your names more descriptive.
>char line[100];
>char line2[100]="!!Begin";
Same problem with the names, but not quite so bad in this case since line is a common name for a line of textual data. The biggest problem is the waste of space for line2, you alocate 100 elements yet only use 8 of them. This would be better as a string literal.
>stream2=fopen("pxo.txt","w");
You don't test to see if this worked anywhere.
>if( fgets( line, 100, stream ) ==NULL)
Your formattng is terrible, but it only effects the readability of the program. The problem here is that the size you pass to fgets is bound too closely to the size of line, if you change the number of elements that line has then you have to change this too. A better way is to use the sizeof operator on line.
>if ((line[100] == line2[100]))
This is the biggest problem. Since you didn't initialize the 99th element of either line or line2, the test will almost surely fail. If you want to test both strings in their entirety, use strcmp in string.h.
Here's my version of the same program:
Code:
#include <stdio.h>
#include <string.h>
int main ( void )
{
FILE *input, *output;
char line[100],
*line2 = "!!Begin";
if ( ( input = fopen ( "px.txt", "r" ) ) != NULL &&
( output = fopen ( "pxo.txt", "w" ) ) != NULL )
{
if ( fgets ( line, sizeof line, input ) != NULL ) {
/* Remove the trailing newline */
line[strlen( line ) - 1] = '\0';
fputs ( line, output );
printf ( "%s\n", line );
if ( strcmp ( line, line2 ) == 0 )
printf ( "hello success\n" );
else
printf ( "oooo\n" );
}
else
printf( "fgets error\n" );
fclose ( input );
fclose ( output );
}
else
printf ( "File open error\n" );
return 0;
}
-Prelude