need advice2

This is a discussion on need advice2 within the C++ Programming forums, part of the General Programming Boards category; hello again. 1. My program below can read one line only from my open file px.txt how to loop it. ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    6

    Question need advice2

    hello again.
    1. My program below can read one line only from my open file px.txt
    how to loop it. i need to use while loop command.
    2. what happen with this command: if ((line[100] == line2[100]))
    it must say " hello success" but output is "oooo"


    #include <stdio.h>

    void main( void )
    {
    FILE *stream;
    FILE *stream2;
    char line[100];
    char line2[100]="!!Begin";

    stream2=fopen("pxo.txt","w");
    if ( (stream = fopen( "px.txt", "r" )) != NULL )
    {

    if( fgets( line, 100, stream ) ==NULL)
    printf( "fgets error\n" );
    else
    fputs(line,stream2);
    printf( "%s", line);
    if ((line[100] == line2[100]))
    printf("hello success\n");
    else
    printf("oooo\n");
    fclose( stream );
    fclose( stream2 );
    }
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    I'm not as familiar with this C style coding, but if you never print "hello success", it's because line[100] never equals line2[100].
    Unless this style of coding is different than what I'm used to, you should never have a line[100] or a line2[100]. You declare the lines to have 100 elements each near the beginning of your program. That's fine, but those elements are numbered from 0 - 99, i.e., line[0] through line[99].
    In your if statement, you're trying to access the element line[100] and line2[100]. These elements are outside the bounds of your array, which ends at line[99] and line2[99]. A drawback of C and C++ is that the compiler lets you do this. The problem is that who knows what is in memory at spot line[100]? It could be anything.
    If you're trying to compare the whole line, you don't need the brackets at all.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

  4. #4
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Shouldn't you be posting this in the C board?

    [EDIT]
    hehe Prelude you beat me to it
    [/EDIT]

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21