Can't display text from file

This is a discussion on Can't display text from file within the C Programming forums, part of the General Programming Boards category; Okay, I'm trying to learn how to use scroll bars in windows, and I thought it would be easy to ...

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

    Can't display text from file

    Okay, I'm trying to learn how to use scroll bars in windows, and I thought it would be easy to just read in some text file and display it in my window instead of creating a bunch text in the source code. Well, the code doesn't seem to work. What I wanted it to do was open a file and first scan through it and get the number of lines and the length of the largest line. Next, store the text in a dynamicaly allocated 2-dim array, and put it on the screen. I've created a few functions for this. They are:

    void **Create_Array(int iSize, int iNumLines, int iLineWidth)
    - allocates memory for array.
    void Delete_Array(void **array, int iNumLines)
    - deletes a 2-dim array
    int Scan_File(FILE *file, int *iNumLines, *iLineWidth)
    - counts the number of lines and max line width

    One thing I couldn't figure out was why I couldn't use the increment operator(++) for *iNumLines and *iLineWidth in Scan_File(). *iNumLines++ does not work. It just returns 0. So instead I just used two other variables and stored their final values in iNumLines and iLineWidth, and that did work.

    Anyway, instead of printing text to the window it just prints one line of random text on the screen and then the rest of the lines are some other character. It looks something like this:

    Sample line five ..ine ree.
    XXXXXXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXXXXXX
    XXXXXXXXXXXXXXXXXXXX

    Where line one is a composition of the last line and the two lines preceeding it. I tried testing the code in a console window instead and it does display the text properly, but after each line there are four characters which are the ascii(253) "square" symbol.

    If I had to guess I'd say it has something to do with the way the array was created, but I really don't know.

    Heres the code. Sorry it's so long. I tried to clean it up a bit.

    main1.c
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "mylib.h"
    
    LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM);
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   PSTR szCmdLine, int iCmdShow)
    
    {
    	static TCHAR	szAppName[] = TEXT("insert name here") ;
    	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	=	NULL ;
    	wndclass.lpszClassName	= szAppName ;
    
    	if(!RegisterClass(&wndclass))
    	{
    		MessageBox(NULL,TEXT("This program requires Windows NT!"),
    				   szAppName, MB_ICONERROR) ;
    		return 0 ;
    	}
    
    	hwnd = CreateWindow(szAppName, TEXT("program name here"),
    		WS_OVERLAPPEDWINDOW|WS_VSCROLL,
    		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)
    {
    	HDC hdc ;
    	PAINTSTRUCT ps ;
    	TEXTMETRIC tm ;
    	static FILE *myfile ;
    	static char **my_array ;
    	static int cyChar,cxChar,iNumLines,iLineWidth, error ;
    	int ch = 0 ;
    	int row = 0 ;
    	int col = 0 ;
    
    	switch(message)
    	{
    	case WM_CREATE:
    
    		hdc = GetDC(hwnd) ;
    		 
    		 GetTextMetrics(hdc,&tm) ;
    		 cxChar = tm.tmAveCharWidth ;
    		 cyChar = tm.tmHeight + tm.tmExternalLeading ;
    		
    		ReleaseDC(hwnd,hdc) ;
    		
    		if((myfile = fopen("test.txt","r")) == NULL)
    		{
    			error = -1 ;
    		}
    		else
    		{
    			if((Scan_File(myfile,&iNumLines,&iLineWidth)) != DONE) 
    				error = -1 ;
    			if((my_array = (char **) Create_Array(sizeof(char),iNumLines,(iLineWidth + 1))) 
    				== NULL)
    				error = -1 ;
    			
    			if(my_array == NULL)
    				error = -1 ;
    			if(iNumLines <= 0)
    				error = -1 ;
    			else
    			{				
                                    //Now that array is created. Read text into array
    				for(row = 0, col = 0 ; row < iNumLines; row++)
    				{
    					while((ch = fgetc(myfile)) != EOF)
    					{
    						switch(ch)
    						{
    						case '\n':
    							col = 0 ;
    							break ;
    						default:
    							my_array[row][col] = (char) ch ;
    							col++ ;
    							break ;
    						}
    					}
    					if(ch == EOF) //This shouldn't happen
    						row = (iNumLines - 1) ; 
    				}
    			}
    				
    		}
    		break ;
    
    	case WM_PAINT:
    		hdc = BeginPaint(hwnd,&ps) ;
    			if(error == -1)
    			{
    				TextOut(hdc,0,0,"Could not open file",sizeof("Could not open file")) ;
    			}
    			else
    			{
    
    				for(row = 0 ; row < iNumLines ; row++)
    				{
    					TextOut(hdc,0,(cyChar * row),my_array[row],iLineWidth) ;
    				}
    			}
    
    		EndPaint(hwnd,&ps) ;
    		break ;
    
    	case WM_DESTROY:
    		Delete_Array(my_array,iNumLines) ;
    		if(error != -1)
    			fclose(myfile) ;
    		PostQuitMessage(0) ;
    		break ;
    
    
    		
    	}
    	return DefWindowProc(hwnd,message,wParam,lParam) ;
    }
    lib1.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "mylib.h"
    
    
    void **Create_Array(int size,int rows, int cols) 
     {
    	 void **array ;
    	 int x = 0 ;
    
    	 if((array = (void **) calloc(rows,size)) == NULL)
    		 return NULL ;
    
    	 for(x = 0 ; x < rows ; x++)
    		if((array[x] = (void *) calloc(cols,size)) == NULL)
    			return NULL ;
    	return array ;
    
     }
    
     void Delete_Array(void **array, int rows)
     {
    	int x = 0 ;
    
    	for(x = 0 ; x < rows ; x++)
    		free(array[x]) ;
     }
    
     int Scan_File(FILE *file,int *iNumLines, int *iLineWidth)
     {
    	 int iCharCount = 0 ;
    	 int ctr = 0 ;
    	 int iLineCtr = 0 ;
    	 int ch = ' ' ;
    	
    
    	 //check to see if the file is open
    	 if(file == NULL)
    		 return FILE_NOT_OPEN ;
    	 else
    	 {
    		 while((ch = fgetc(file)) != EOF)
    		 {
    			 if(ch == '\n')
    			 {
    				 if(iCharCount > *iLineWidth)
    					 *iLineWidth = iCharCount ;
    				 iCharCount = 0 ;
    				 iLineCtr++ ;
    			 }
    			 else 
    			 {
    				 iCharCount++ ;
    			 }
    		 }
    		 *iNumLines = iLineCtr ;
    		 rewind(file) ;
    		
    	 }
    	 return DONE ;
    	
     }
    mylib.h
    Code:
    #ifndef _MYLIB_H_
    
    #define	FILE_NOT_OPEN		1
    #define DONE				2
    
    
    void **Create_Array(int size,int rows, int cols) ;
    void Delete_Array(void **array, int rows) ;
    int Scan_File(FILE *file,int *iNumLines, int *iLineWidth) ;
    
    #endif
    I appreciatte any help, thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    There are quite a few problems with your program. Some are with the way it is allocating the array and others with the way it is reading the file. Attached are the corrections -- and I've taken the liberty of (optimizing) the way the file is read by using fgets() instead of reading character-by-character.

    The attached files are c++ and compiled with VC++ 6.0 compiler. Just remove the first line of each c++ file that includes stdafx.h and rename the c++ files to whatever you want. They should still compile with a C compiler.

    Well, I see this board doesn't like *.zip files, so I had to upload the files individually.
    Attached Files Attached Files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read and display contents from text file
    By spadez in forum C Programming
    Replies: 2
    Last Post: 02-03-2009, 02:25 PM
  2. Display selected words from text file
    By picasso in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2007, 09:06 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. checking values in a text file
    By darfader in forum C Programming
    Replies: 2
    Last Post: 09-24-2003, 02:13 AM

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