Thread: File Processing

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    3

    Cool File Processing

    I am attempting to create a program which reads data from an input file of the Phylip format. The arrays I have created basically work except for the occasional loss of information necessary. I get no warnings and compile with MSVC 6.5. This is part of a Windows program, but the problem is not Windows specific. I believe the logic of my read is flawed. Any help would be greatly appreciated.

    Code:
    arraysequences = (char **)malloc(ntaxa * sizeof( char *));
    			   arraynames = (char **)malloc(ntaxa * sizeof( char *));
    
    				for( i = 0; i < ntaxa ; i++)
    				{
    					arraysequences[i] = (char *)malloc(seqlength * sizeof( char)) ;
    					arraynames[i] = (char *)malloc(namelength * sizeof( char)) ;
    				}
                    
    				for ( count = 0 ; count < (ntaxa + 1) ; )
    				
    				{
    					newchar = (char)getc(In_File) ;	
    
    					if ( newchar == EOF )
    						break ;
    
    					if (( count == ntaxa ) && ( newchar == '\n' ))
    						break;
    			
    					if ( newchar == '\n' )
    					{
    						fscanf(In_File, "%s", arraynames[count] ) ;
    						storeNumber = charCount ;
    						storeNumber2 = storeNumber ;
    						charCount = 0 ;
    						count++ ;
    					}
    
    	                if (( newchar != ' ' ) && ( newchar != '\n' ))
    					{
    						arraysequences[count-1][charCount] = newchar ;
    						charCount++ ;
    
    					}
    										
    				}
    
    			while ( storeNumber2 < seqlength )
    			{
         
    				for ( count = -1 ; count < ntaxa ; )
    					
    				{
    
    					newchar = (char)getc(In_File) ;	
    
    					if ( newchar == EOF )
    						break ;
    
    					if (( count == ntaxa ) && ( newchar == '\n' ))				
    						break;
    
    					if ( newchar == '\n' )
    					{
    					   
    						charCount = storeNumber ; 
    						count++ ;
    
    					}
    
    					 if (( newchar != ' ' ) && ( newchar != '\n' ))
    				
    					 {
    						
    						arraysequences[count][charCount] = newchar ;
    						charCount++ ;
    					
    					 }
    
    				}
    
    				
    				    storeNumber = charCount + storeNumber ;
    					storeNumber2 = storeNumber ;
    
    					if ( newchar == EOF )
    						break ;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1) Stop typecasting malloc. If you're compiling as C you don't need to, and shouldn't. Readthe FAQ if you care as to why you shouldn't.

    2) sizeof( char ) is always 1.

    3) It is impossible for this to ever give you EOF:
    Code:
    newchar = (char)getc(In_File) ;	
    
    if ( newchar == EOF )
    Once again, your typecasting is hindering your program. Also, if newchar is in fact a char, you'll never get EOF.

    4) The extra parenthesis, while not wrong, aren't really needed. Also, this could be written like so:
    Code:
    if ( newchar == '\n' )
    {
        fscanf(In_File, "%s", arraynames[count] ) ;
        storeNumber = charCount ;
        storeNumber2 = storeNumber ;
        charCount = 0 ;
        count++ ;
    }
    else
    if ( newchar != ' ' )
    {
        arraysequences[count-1][charCount] = newchar ;
        charCount++ ;
    }
    That'll work for now. When you post your changes, be sure to fix your indentation. It's a pain to read as is.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    3

    Cool Reply to Quzah's advice

    Your advice was very good. Everything you said worked fine, but my logic error continues. I think the while loop is flawed, however I think somebody might need to see what the code does to a Phylip file to find the error. Anyway, here is the complete c file with changes:

    Code:
    #include <windows.h>
    #include <commdlg.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <malloc.h>
    #include "resource.h"
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    TCHAR szAppName[] = TEXT ("DibConv") ;
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
         HWND     hwnd ;
         MSG      msg ;
         WNDCLASS wndclass ;
    
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = szAppName ;
         wndclass.lpszClassName = szAppName ;
    
         if (!RegisterClass (&wndclass))
         {
              MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
    
         hwnd = CreateWindow (szAppName, TEXT ("Gene Weaver 1"),
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT, CW_USEDEFAULT,
                              CW_USEDEFAULT, CW_USEDEFAULT, 
                              NULL, NULL, hInstance, NULL) ;
    
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
    
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
    
         }
         return msg.wParam ;
    }
    
    
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        
         static int          cxClient, cyClient ;
    	 static int			 cxChar, cxCaps, cyChar ;
    	 const int           namelength = 11 ;
         static OPENFILENAME ofn ;
    	 TEXTMETRIC			 tm ;
         static TCHAR        szFileName [MAX_PATH], szTitleName [MAX_PATH] ;
         static TCHAR        szFilter[] = TEXT ("Phylip Files (*.phy)\0*.PHY\0")
                                          TEXT ("All Files (*.*)\0*.*\0\0") ;
         
    	 TCHAR				 *CharFileName ;
    	 static char         **arraysequences ;
    	 static char         **arraynames ;
    	 static int 		 newchar = 0 ;
    	 static int 		 seqlength = 0, storeNumber = 0, storeNumber2 = 0, storeNumber3 = 0 ;
    	 static int			 count = 0, charCount = 0, ntaxa = 0 ;
    	 int				 i = 0 ;
         HDC                 hdc ;
         PAINTSTRUCT         ps ;
    	 FILE                *In_File = NULL ;
    
         switch (message)
         {
         case WM_CREATE:
    
    		 hdc = GetDC (hwnd) ;
    		 GetTextMetrics (hdc, &tm) ;
    		 cxChar = tm.tmAveCharWidth ;
    		 cxCaps = ( tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar/2 ;
    		 cyChar = tm.tmHeight + tm.tmExternalLeading ;
    
    
              ofn.lStructSize       = sizeof (OPENFILENAME) ;
              ofn.hwndOwner         = hwnd ;
              ofn.hInstance         = NULL ;
              ofn.lpstrFilter       = szFilter ;
              ofn.lpstrCustomFilter = NULL ;
              ofn.nMaxCustFilter    = 0 ;
              ofn.nFilterIndex      = 0 ;
              ofn.lpstrFile         = szFileName ;
              ofn.nMaxFile          = MAX_PATH ;
              ofn.lpstrFileTitle    = szTitleName ;
              ofn.nMaxFileTitle     = MAX_PATH ;
              ofn.lpstrInitialDir   = NULL ;
              ofn.lpstrTitle        = NULL ;
              ofn.Flags             = 0 ;
              ofn.nFileOffset       = 0 ;
              ofn.nFileExtension    = 0 ;
              ofn.lpstrDefExt       = TEXT ("phy") ;
              ofn.lCustData         = 0 ;
              ofn.lpfnHook          = NULL ;
              ofn.lpTemplateName    = NULL ;
    
    		  ReleaseDC (hwnd, hdc) ;
              return 0 ;
    
         case WM_SIZE:
              cxClient = LOWORD (lParam) ;
              cyClient = HIWORD (lParam) ;
              return 0 ;
    
         case WM_COMMAND:
              switch (LOWORD (wParam))
              {
              case IDM_FILE_OPEN:
    
                        // Show the File Open dialog box
    
                   if (!GetOpenFileName (&ofn))
                        return 0 ;
    			   CharFileName = (TCHAR*)szFileName ;
    			   In_File = fopen(CharFileName, "r");
    			   fscanf(In_File, "%d%d", &ntaxa, &seqlength);
    
    			   arraysequences = malloc(ntaxa * sizeof( char *));
    			   arraynames = malloc(ntaxa * sizeof( char *));
    
    				for( i = 0; i < ntaxa ; i++)
    				{
    					arraysequences[i] = malloc(seqlength) ;
    					arraynames[i] = malloc(namelength) ;
    				}
                    
    				for ( count = 0 ; count < (ntaxa + 1) ; )
    				
    				{
    					newchar = getc(In_File) ;	
    
    					if ( newchar == EOF )
    						break ;
    
    					if (( count == ntaxa ) && ( newchar == '\n' ))
    						break;
    			
    					if ( newchar == '\n' )
    					{
    						fscanf(In_File, "%s", arraynames[count] ) ;
    						storeNumber = charCount ;
    						storeNumber2 = storeNumber ;
    						charCount = 0 ;
    						count++ ;
    					}
    
    	                if (( newchar != ' ' ) && ( newchar != '\n' ))
    					{
    						arraysequences[count-1][charCount] = newchar ;
    						charCount++ ;
    
    					}
    										
    				}
    
    			while ( storeNumber2 < seqlength )
    			{
         
    				for ( count = -1 ; count < ntaxa ; )
    					
    				{
    
    					newchar = getc(In_File) ;	
    
    					if ( newchar == EOF )
    						break ;
    
    					if (( count == ntaxa ) && ( newchar == '\n' ))				
    						break;
    
    					if ( newchar == '\n' )
    					{
    					   
    						charCount = storeNumber ; 
    						count++ ;
    
    					}
    
    					 else if ( newchar != ' ' ) 
    				
    					{
    						
    						arraysequences[count][charCount] = newchar ;
    						charCount++ ;
    					
    					}
    
    				}
    
    				
    				    storeNumber = charCount + storeNumber ;
    					storeNumber2 = storeNumber ;
    
    					if ( newchar == EOF )
    						break ;
    
    				
    
    			}
    
    			
    
                   InvalidateRect (hwnd, NULL, TRUE) ;
    
                   
                   return 0 ;
    
              }
    
              break ;
    
         case WM_PAINT:
              hdc = BeginPaint (hwnd, &ps) ;
    		  
    		  for ( count = 0 ; count < ntaxa ; count++ )
    		  {
    			  TextOut (hdc, 0, cyChar * count, arraynames[count], lstrlen (arraynames[count])) ;
    			  TextOut (hdc, 11*cxCaps, cyChar * count, arraysequences[count], seqlength ) ; 
    		  }
    
    
              
    
              EndPaint (hwnd, &ps) ;
              return 0 ;
              
         case WM_DESTROY:
    
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    3

    Question An Explanation

    The indentation is good on the MSVC IDE. Sorry guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM