News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Cannot open file

Started by mojo0716, December 17, 2007, 11:19:37 PM

Previous topic - Next topic

mojo0716

Hello, I'm a Computer Science student doing an assignment for an Intel x86 Assembly Programming class.  I am using the textbook Assembly Language for Intel-Based Computers, 5th Edition by Kip Irvine.

The assignment involves reading in student records from the keyboard and writing them to a file and then reading them back and displaying them on the screen

I got everything working ok "except" reading back from the file.  For some reason, I cannot open the file.

Below is the code.  I would appreciate any help.

thanks


TITLE Student Records

; Description: This program invokes a macro, located in Macro.inc, which takes two 32-bit memory
; operands and prints to the screen the product.


; Creation Date: 12/15/2007

; Revisions:
; Date: Modified by:


INCLUDE Irvine32.inc
INCLUDE Macros.inc ; macro definitions

.data
bytesWritten DWORD ?

fileErrorMsg BYTE "File could not be created",0dh,0ah,0
fileName BYTE "Student_Records.txt",0
fileHandle HANDLE ?

buffer BYTE ?

promptForIdNum BYTE "Enter the student's ID Number: ",0
promptForFirstName BYTE "Enter the student's first name: ",0
promptForLastName BYTE "Enter the student's last name: ",0
promptForDateOfBirth BYTE "Enter the student's date of birth(ddmmyyyy): ",0
promptForNumberOfRecords BYTE "Enter the number of student records you want to enter: ",0

Student STRUCT
idNumber BYTE 15 DUP(?)
lastName BYTE 30 DUP(?)
firstName BYTE 30 DUP(?)
dateOfBirth BYTE 15 DUP(?)
Student ENDS

theStudent Student <>

numberOfRecords DWORD ?

loopCount DWORD ?



.code
main PROC
      ;Create Student_Records.txt file
        mov edx,OFFSET fileName
        call CreateOutputFile
        mov fileHandle,eax
  ;Check to see whether file was successfully created
cmp eax,INVALID_HANDLE_VALUE
jne file_created
mov edx,OFFSET fileErrorMsg
call WriteString
jmp terminate
file_created:
mov edx,OFFSET promptForNumberOfRecords
call WriteString
Call ReadInt
mov numberOfRecords,eax
Call WriteInt
Call Crlf
Call Crlf

mov ecx,numberOfRecords

NumberOfRecordsLoop:
Call EnterRecord
loop NumberOfRecordsLoop

Call CloseFile

Call ReadRecord


jmp terminate
terminate:
call CloseFile
exit ; Exit program
EnterRecords:


main ENDP

;-------------------------------------------------------------------------------------------
EnterRecord PROC USES eax ecx edx
;
; Receives: nothing
; Returns: nothing
;--------------------------------------------------------------------------------------------

mov edx,OFFSET promptForIdNum
call WriteString
mReadString theStudent.idNumber
Call Crlf
mWriteString theStudent.idNumber
Call Crlf

mov edx,OFFSET promptForFirstName
Call WriteString
mReadString theStudent.firstName
Call Crlf
mWriteString theStudent.firstName
Call Crlf

mov edx,OFFSET promptForLastName
Call WriteString
mReadString theStudent.lastName
Call Crlf
mWriteString theStudent.lastName
Call Crlf

mov edx,OFFSET promptForDateOfBirth
Call WriteString
mReadString theStudent.dateOfBirth
Call Crlf
mWriteString theStudent.dateOfBirth
Call Crlf

mov eax,fileHandle
mov edx,OFFSET theStudent.idNumber
mov loopCount,ecx
mov ecx,15
call WriteToFile

INVOKE SetFilePointer,
fileHandle,0,0,FILE_END
   
mov eax,fileHandle
mov edx,OFFSET theStudent.firstName
mov ecx,30
call WriteToFile

INVOKE SetFilePointer,
fileHandle,0,0,FILE_END

mov eax,fileHandle
mov edx,OFFSET theStudent.lastName
mov ecx,30
call WriteToFile

INVOKE SetFilePointer,
fileHandle,0,0,FILE_END

mov eax,fileHandle
mov edx,OFFSET theStudent.dateOfBirth
mov ecx,15
call WriteToFile
mov ecx,loopCount
ret
EnterRecord ENDP

;-------------------------------------------------------------------------------------------
ReadRecord PROC USES eax ecx edx
;
; Receives: nothing
; Returns: nothing
;--------------------------------------------------------------------------------------------
BUFFER_SIZE = 5000
.data
newBuffer BYTE BUFFER_SIZE DUP(?)
newFileHandle HANDLE ?
.code
; Open file for input
mov edx,OFFSET fileName
call OpenInputFile
mov newFileHandle,eax
; Check for errors
cmp eax,INVALID_HANDLE_VALUE
jne file_ok
mWrite<"Cannot open file",0dh,0ah>
jmp quit
file_ok:

; Read the file into a buffer
mov edx,OFFSET newBuffer
mov ecx,BUFFER_SIZE
call ReadFromFile
jnc check_buffer_size
mWrite "Error reading file. "
call WriteWindowsMsg
jmp close_file

check_buffer_size:
cmp eax,BUFFER_SIZE
jb buf_size_ok
mWrite<"Error: Buffer too small for the file",0dh,0ah>
jmp quit
buf_size_ok:
mov newBuffer[eax],0
mWrite "File size: "
call WriteDec
call Crlf
; Display the buffer
mWrite<"Buffer:" ,0dh,0ah,0dh,0ah>
mov edx,OFFSET buffer
call WriteString
call Crlf
close_file:
mov eax,fileHandle
call CloseFile
quit:
ret
ReadRecord ENDP


END main


mojo0716

Well if I comment out everything except the ReadRecord Procedure, it opens the file, but doesn't display the buffer.

Tedd

Make sure the file you're trying to open isn't already still open :wink
And also check which parameters you're giving to the functions.
No snowflake in an avalanche feels responsible.