Invalid Page Fault? 
Author Message
 Invalid Page Fault?

Could anyone tell me me if the following code could result in an "Invalid Page Fault". I've tested the program on several different PC's using different operating systems ranging from windows 95 to XP and it works fine on each. Unfortunately it crashes on a customer's PC producing an Invalid Page Fault error. My C is a little rusty so I would appreciate any suggestions for improving the code. Reading round this news group this sort or error seems to be due to pointer misuse or addressing a non-existant array element. The program prints a test label on a dot matrix for alignment purposes on a pre-printed label.

/* Stick on 3"x5" white labels   */
/* 12 cpi x 6 lpi      */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CR  0x0d
#define LF  0x0a
#define FF  0x0c
#define SP  0x20
#define ESC  0x1b
#define RST  0x18
#define CPI  0x1c /* 12 cpi OKI use 0x1c no ESC (0x1e for 10 cpi) */
#define LPI6 0x36 /* 6 lpi OKI use ESC 0x36 */
#define LPI8 0x38
#define PAG  0x47 /* OKI Page Length of 3" = ESC 47 06 (06 is the number of 1/2 inch) */
#define LEN  0x06 /* Leave first and last 2 lines blank */
                  /* Reset printer defaults OKI 0x18 no ESC */

/* Prototypes */
void Initform(char form[][62]);
int testdata(int coords[][2], char form[][62]);
FILE *Initprinter();
void Printoutput(char form[][62], FILE *printer);

void main(int argc, char *argv[]) /* To close window use run prog.exe /c */
{

 char form[12][62];   /* Size of form assumimg 12 cpi * 6 lpi */

 static int coords[16][2] =  /* R,C Locates field on form 16x60  */
  { 0,4,    /* fld 0 To Consignment Name (36)  */
   1,4,    /* fld 1 To Address Line 1 (25)   */
   2,4,    /* fld 2 To Address Line 2 (25)  */
   3,4,    /* fld 3 To Town (20)     */
   4,4,    /* fld 4 To Post Code (10)    */
   6,7,    /* fld 5 Note Number (7)    */    
   8,7,    /* fld 6 Item (12)      */
   10,7,    /* fld 7 Customer Reference (30)  */  
   0,34,    /* fld 8 Parent Consignment Name (36) */
   1,34,    /* fld 9 Parent Town (20)    */
   5,34,    /* fld 10 Account Code (8)    */
   6,34,    /* fld 11 From Name (25)    */
   7,34,    /* fld 12 From Address Line 1 (25)  */
   8,34,    /* fld 13 From Address Line 2 (25)  */
   9,34,    /* fld 14 From Town (20)    */
   10,34,    /* fld 15 From Post Code (10)   */
  };      

  testdata(coords, form);

Quote:
}

int testdata(int coords[][2], char form[][62])
{
 FILE *printer; /* *printer */

 printer = fopen("PRN","w");

 printer = Initprinter();
 Initform(form);

 strncpy(&form[coords[0][0]][coords[0][1]], "Consignment Name1********", 25);
 strncpy(&form[coords[1][0]][coords[1][1]], "Address Line 1***********", 25);
 strncpy(&form[coords[2][0]][coords[2][1]], "Address Line 2***********", 25);
 strncpy(&form[coords[3][0]][coords[3][1]], "Town****************", 20);
 strncpy(&form[coords[4][0]][coords[4][1]], "PostCode**", 10);
 strncpy(&form[coords[5][0]][coords[5][1]], "Note No", 7);
 strncpy(&form[coords[6][0]][coords[6][1]], "Item********", 12);
 strncpy(&form[coords[7][0]][coords[7][1]], "Customer Ref*************", 25);
 strncpy(&form[coords[8][0]][coords[8][1]], "Parent Consignee*********", 25);
 strncpy(&form[coords[9][0]][coords[9][1]], "Parent Town*********", 20);
 strncpy(&form[coords[10][0]][coords[10][1]], "Account Code********", 20);
 strncpy(&form[coords[11][0]][coords[11][1]], "From Name****************", 25);
 strncpy(&form[coords[12][0]][coords[12][1]], "From Add 1***************", 25);
 strncpy(&form[coords[13][0]][coords[13][1]], "From Add 2***************", 25);
 strncpy(&form[coords[14][0]][coords[14][1]], "From Town***********", 20);
 strncpy(&form[coords[15][0]][coords[15][1]], "F Post Cod", 10);

 Printoutput(form, printer); /* printer */
 fclose(printer);

 return(0);

Quote:
}

void Initform(char form[][62])
{
 int i, j;
 /* Initialise Array with spaces and LF CR */
 for(i = 0;i < 12; i++)
  for(j = 0;j < 62; j++)
   if(j == 60)
    form[i][j] = CR;
   else if(j == 61)
    form[i][j] = LF;
   else
    form[i][j] = SP;

Quote:
}

FILE *Initprinter()
{
 FILE *printer;
 /* Initialise the printer */
 printer = fopen("PRN", "w");
  /*fputc(ESC, printer); */

 fputc(CPI, printer);  /* 12 cpi        */
 fputc(ESC, printer);
 fputc(LPI6, printer);  /* 6 lpi        */
 fputc(ESC, printer);
 fputc(PAG, printer);  /* Form length 3"      */
 fputc(LEN, printer);

 return(printer);

Quote:
}

void Printoutput(char form[][62], FILE *printer)
{
 int i, j;
 for(i = 0; i < 4; i++)
  {
   fputc(CR, printer);
   fputc(LF, printer);
  }

 for(i = 0;i < 12; i++)
 {
  /* Change Line Spacing after line 5 from 6 lpi to 8 lpi then feed 2 lines */
  if(i == 5)
  {
   fputc(ESC, printer);
   fputc(LPI8, printer);  /* 8 lpi   */
   fputc(CR, printer);
   fputc(LF, printer);
   fputc(CR, printer);
   fputc(LF, printer);

  }
  for(j = 0;j < 62; j++)
   fputc(form[i][j], printer); /* fo */

 }
  /* Reset Printer */
  fputc(ESC, printer);
  fputc(LPI6, printer);  /* 6 lpi   */

  fputc(FF, printer);

Quote:
}



Sun, 27 Feb 2005 02:51:29 GMT  
 Invalid Page Fault?
Repeat ten times:
NEVER POST IN HTML TO NEWSGROUPS.
Never post in HTML to newsgroups.
NEVER POST IN HTML TO NEWSGROUPS.
Never post in HTML to newsgroups.
NEVER POST IN HTML TO NEWSGROUPS.
Never post in HTML to newsgroups.
NEVER POST IN HTML TO NEWSGROUPS.
Never post in HTML to newsgroups.
NEVER POST IN HTML TO NEWSGROUPS.
Never post in HTML to newsgroups.

Right. Now, to your C problem:

Quote:

> Could anyone tell me me if the following code could
> result in an "Invalid Page Fault". I've tested the
> program on several different PC's using different
> operating systems ranging from windows 95 to XP and
> it works fine on each. Unfortunately it crashes on
> a customer's PC producing an Invalid Page Fault
> error. My C is a little rusty so I would appreciate
> any suggestions for improving the code. Reading
> round this news group this sort or error seems to
> be due to pointer misuse or addressing a
> non-existant array element. The program prints a
> test label on a dot matrix for alignment purposes
> on a pre-printed label.

An "Invalid Page Fault" usually happens when an invalid pointer is
dereferenced.

[snip]

Quote:
> void main(int argc, char *argv[])

The main function should return int. However, this is probably not the
cause of your invalid page fault.

Quote:
> int testdata(int coords[][2], char form[][62])
> {
>  FILE *printer; /* *printer */

>  printer = fopen("PRN","w");

Here you try to open the file PRN for writing, but you don't check whether
it opened or not.

Quote:
>  printer = Initprinter();

And here you write over the previous FILE* handle. This is similar to a
memory leak, but it's a file leak. You should always close the file before
re-using the pointer.

[snip]

Quote:
> FILE *Initprinter()
> {
>  FILE *printer;
>  /* Initialise the printer */
>  printer = fopen("PRN", "w");
>   /*fputc(ESC, printer); */

So, here you try to open the same file PRN again, without closing it first.
This has every chance of failing, and yet again you don't check whether it
actually opened successfully or not.

Quote:
>  fputc(CPI, printer);  /* 12 cpi        */

And here's one place where an Invalid Page Fault is likely: if the file
wasn't opened successfully, you just passed a null pointer to fputc.

Quote:
>  fputc(ESC, printer);
>  fputc(LPI6, printer);  /* 6 lpi        */
>  fputc(ESC, printer);
>  fputc(PAG, printer);  /* Form length 3"      */
>  fputc(LEN, printer);

>  return(printer);
> }

[snip]

--
Simon.



Sun, 27 Feb 2005 08:54:35 GMT  
 Invalid Page Fault?


Quote:
>Could anyone tell me me if the following code could result in an "Invalid Page Fault". I've tested the program on several different PC's using different operating systems ranging from windows 95 to XP and it works fine on each. Unfortunately it crashes on a customer's PC producing an Invalid Page Fault error. My C is a little rusty so I would appreciate any

When does it crash?  Before any output?  In the middle of the output?
At the end of the output?

Quote:
> suggestions for improving the code. Reading round this news group this sort or error seems to be due to pointer misuse or addressing a non-existant array element. The program prints a test label on a dot matrix for alignment purposes on a pre-printed label.

Maybe you would like to fix the line length on your newsgroup reader.

Quote:

>/* Stick on 3"x5" white labels   */
>/* 12 cpi x 6 lpi      */

>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>

>#define CR  0x0d
>#define LF  0x0a
>#define FF  0x0c
>#define SP  0x20
>#define ESC  0x1b
>#define RST  0x18
>#define CPI  0x1c /* 12 cpi OKI use 0x1c no ESC (0x1e for 10 cpi) */
>#define LPI6 0x36 /* 6 lpi OKI use ESC 0x36 */
>#define LPI8 0x38
>#define PAG  0x47 /* OKI Page Length of 3" = ESC 47 06 (06 is the number of 1/2 inch) */
>#define LEN  0x06 /* Leave first and last 2 lines blank */
>                  /* Reset printer defaults OKI 0x18 no ESC */

>/* Prototypes */
>void Initform(char form[][62]);
>int testdata(int coords[][2], char form[][62]);
>FILE *Initprinter();
>void Printoutput(char form[][62], FILE *printer);

>void main(int argc, char *argv[]) /* To close window use run prog.exe /c */

int main(void) if you please.

Quote:
>{

> char form[12][62];   /* Size of form assumimg 12 cpi * 6 lpi */

> static int coords[16][2] =  /* R,C Locates field on form 16x60  */
>  { 0,4,    /* fld 0 To Consignment Name (36)  */

snip additional initialization

Quote:
>   10,34,    /* fld 15 From Post Code (10)   */
>  };      

>  testdata(coords, form);

return 0; /* to match the correct declaration of main */

Quote:

>}

>int testdata(int coords[][2], char form[][62])
>{
> FILE *printer; /* *printer */

> printer = fopen("PRN","w");

PRN is now open.

Quote:

> printer = Initprinter();

You open PRN again.  What does your system do if you try to open the
same file a second time for output?  Do all your systems treat this
problem the same?  Could the fopen in Initprinter return NULL?  Don't
you think you should at least check that fopen succeeded before using
the value it returns?

- Show quoted text -

Quote:
> Initform(form);

> strncpy(&form[coords[0][0]][coords[0][1]], "Consignment Name1********", 25);
snip initialization
> strncpy(&form[coords[15][0]][coords[15][1]], "F Post Cod", 10);

> Printoutput(form, printer); /* printer */
> fclose(printer);

> return(0);

>}

>void Initform(char form[][62])
>{
snip
>}

>FILE *Initprinter()
>{
> FILE *printer;
> /* Initialise the printer */
> printer = fopen("PRN", "w");
>  /*fputc(ESC, printer); */

> fputc(CPI, printer);  /* 12 cpi        */
> fputc(ESC, printer);
> fputc(LPI6, printer);  /* 6 lpi        */
> fputc(ESC, printer);
> fputc(PAG, printer);  /* Form length 3"      */
> fputc(LEN, printer);

> return(printer);
>}

>void Printoutput(char form[][62], FILE *printer)
>{
snip
>}

<<Remove the del for email>>


Sun, 27 Feb 2005 10:52:45 GMT  
 Invalid Page Fault?
Sorry for the HTML
It was indeed the printer file pointer that was returning NULL, curiously
when I used LPT1 instead of PRN it worked?

Thanks



Mon, 28 Feb 2005 03:19:52 GMT  
 Invalid Page Fault?
in comp.lang.c i read:

Quote:
>It was indeed the printer file pointer that was returning NULL, curiously
>when I used LPT1 instead of PRN it worked?

unknown, ask in a group dealing with your platform.

--
bringing you boring signatures for 17 years



Mon, 28 Feb 2005 13:34:50 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Invalid page fault in Windows98

2. Invalid page faults when accessing large arrays

3. caused an invalid page fault in module ...

4. Invalid Page fault in module MSJT3032.DLL

5. Invalid page fault

6. Invalid page fault

7. invalid page fault

8. Invalid Page Fault in Release mode with ML compiler option

9. invalid page fault ...

10. MSDEV caused an invalid page fault in Module SSSCC.DLL After Install SP2

11. invalid page fault when I exit my program

12. Help - receiving invalid page fault in kernel32.dll

 

 
Powered by phpBB® Forum Software