Fopen Question

This is a discussion on Fopen Question within the C Programming forums, part of the General Programming Boards category; I'm doing a project for my class. I'm having serious memory issues. I have to read in a 512 x ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    Fopen Question

    I'm doing a project for my class. I'm having serious memory issues. I have to read in a 512 x 512 picture image and perform an edge detection technique on 3x3 sections. I originally tried to read in three lines at a time, but that didn't work. It ate up all my memory. Now I'm trying to open and close the input file. I keep getting access violations. Is there any way to make this work?

    Code:
    fin = fopen( pixel_file, "r" );
    fin = fin + ROW*(i-1) + COLUMN*(j-1);
    // Retrieve values for the center_pixel and surrounding pixels
    for( k=0; k<3; k++ )
    {
         for( l=0; l<3; l++ )
         {
                fscanf( fin, "%c", & center_pixel );
                current_array[k][l] = (int)center_pixel;
                fin+=1;
         }
         fin += ( ROW - 2 );
    }
    fclose( fin );

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    1/4MB ate all your memory - you're not still using DOS are you?
    Or an old and crusty DOS compiler on your brand new machine with a reasonable OS on it...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    My problem is that, my original way (reading three lines of code at a time), for some reason, the program would stop writing at some point to my output file (also a display file). I really don't know what's going on. I suppose it's a memory problem. When I debug the original program, it can go from row 0 column 0 to row 512 column 512. When I run the program itself it doesn't do the whole process.

    Also, I'm using a resonably new Windows 2000 machine--MS Visual Studio 6.0.

  5. #5
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    post us the code you're using

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Code:
    /* This program accepts any pixel file ( *.DAT ) as input and outputs another image.  That image will have highlighted edges as detected by one of the following approaches:
       1) Robert Operator
       2) Laplacian Operator
       3) Gradient Operator
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXGRAY 127
    #define ROW 512                                                           
    #define COLUMN 512
    #define ROBERTSLIMIT 20
    #define LAPLACIANLIMIT 100
    #define GRADIENTLIMIT 100
    
    void main( void )
    {			
       const char vRoberts[3][3] = { {-1,0,1}, {-1,0,1}, {-1,0,1} };
       const char hRoberts[3][3] = { {-1,-1,-1}, {0,0,0}, {1,1,1} };
       char center_pixel = ' ';
       int approach = 0;
       int g, h, i, j, k, l, m, n, p;
       char pixel_file[20];
       int pixel_array[3][COLUMN];
       int current_array[3][3];
       int vRob, hRob;
       FILE *fin, *fout;
    
       // Prompt user for file name
       printf( "This program will draw an edge-enhanced image for a specified picture file.\n" );
       printf( "Enter the file name:\n" );
    	
       // Get file name
       scanf( "%s", &pixel_file );
    	
       // Open input file
       fin = fopen( pixel_file, "r" );
    	
       // Check to make sure pixel_file exists
       if( !fin )
       {
         printf( "\nFile open failure!\n" );
         printf( "File %s does not exist.\n", &pixel_file );
         exit(0);
       } 
    
       // Determine edge detection approach
       printf( "\nChoose one of the following approaches:\n");
       printf( "  (1) Robert operator\n" );
       printf( "  (2) Laplacian operator\n" );
       printf( "  (3) Gradient operator\n" );
    
       scanf( "%d", &approach );
    
       //Check validity of answer
       while( (approach < 1) | (approach > 3) )
       {
          printf( "Invalid choice.  Please enter 1, 2, or 3.\n" );
          scanf( "%d", &approach );
       }
    
       // Open output file
       fout = fopen( "edge.dat", "w" );
    
       // Access first three rows
       for( g=0; g<3; g++ )
       {
         // Access every column
         for (h=0; h<COLUMN; h++ )
         {
              // Read in ASCII value of the pixel at row g and column h
              fscanf( fin, "%c", &center_pixel );
              // Convert center_pixel to integer value
              pixel_array[g][h] = center_pixel;
              // Assure gray_value is legal--valued 0 to 127
              pixel_array[g][h] %= ( MAXGRAY + 1 );
         }
       }
    
       // Access every row
       for( i=0; i<ROW; i++ )
         // Access every column
         for( j=0; j<COLUMN; j++ )
         {
              // Make perimeter pixels edges
              if( ( i == 0 ) | ( i == ROW-1 )| ( j == 0 ) | ( j == COLUMN-1 ) )
                center_pixel = ( char )0;
              else
              {				
                 // Retrieve values for the center_pixel and surrounding pixels
                 for( k=0; k<3; k++ )
                     for( l=0; l<3; l++ )
                           current_array[k][l] = pixel_array[k][j-1+l];
    
                 // Use Roberts Operator for edge detection
                 if( approach == 1 ) 
                 {
                     vRob = hRob = 0;
                     for( m=0; m<3; m++ )
                         for( n=0; n<3; n++ )
                         {
                            vRob += ( current_array[m][n] * vRoberts[m][n] );
                            hRob += ( current_array[m][n] * hRoberts[m][n] );
                         }
                         // Take absolute value of vRob and hRob--un-negate
                         if( vRob < 0 )
                             vRob = 0 - vRob;
                          if( hRob < 0 )
                             hRob = 0 - hRob;
                          if( ( vRob >= ROBERTSLIMIT ) | ( hRob >= ROBERTSLIMIT ) )
                                center_pixel = ( char )0;
                          else
                                 center_pixel = ( char )current_array[1][1];
                       } // end Roberts
    
                   } // end else--non-edge pixel
    
               fprintf( fout, "%c", center_pixel );
             } // done with column j
    
        // Get next row, if there is one
        if( i>0 && i<ROW-1 )
        for( p=0; p<COLUMN; p++ )
          {
             // Rename first two rows
            // Move row one to row zero and row two to row one
            pixel_array[0][p] = pixel_array[1][p];
            pixel_array[1][p] = pixel_array[2][p];
             // Read in ASCII value of the pixel at row i and column j
             fscanf( fin, "%c", &center_pixel );
            // Convert center_pixel to integer value
             pixel_array[2][p] = center_pixel;			 
            // Assure gray_value is legal--valued 0 to 127
            pixel_array[2][p] %= ( MAXGRAY + 1 );
          }
         } // Done with row i calculations
    
       fclose( fin );
       fclose( fout );
    }


    Mod edit - its / not \ in the closing code tag

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I am just asking here but isn't the // non ansi standard. And I don't think he needs the & for the
    scanf( "%s", &pixel_file );
    because it is a string and already has its memory address right

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. help with stat() and fopen()
    By movl0x1 in forum C Programming
    Replies: 6
    Last Post: 07-25-2007, 05:28 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. fopen() and open()
    By Encrypted in forum C Programming
    Replies: 8
    Last Post: 02-09-2003, 03:57 PM
  5. fopen question
    By DramaKing in forum C Programming
    Replies: 17
    Last Post: 12-19-2001, 10:09 PM

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