Re: getchar still with problems

Search this archive.

From: Russell Marks (russell.marks@ntlworld.com)
Date: Thu 07 Sep 2000 - 14:47:19 IDT


> Look at the comments and pliiiiiiiiiiiiiiiiiiisssssssssss help.

Oh, well since you use *nineteen* i's, how can I possibly refuse? ;-)

> #include <stdio.h>
> #include <stdlib.h>
> #include <vgagl.h>
> #include <vga.h>
> #include <string.h>
> #include <unistd.h>
> #include <fcntl.h>
> 
> struct fecha {
>  int dia;
>  int mes;
>  int anio;
>  };
> 
> struct personas {
>  int id;
>  char apellido [40];
>  char nombre [40];
>  char direccion [50];
>  char email [40];
>  char tel1 [40];
>  char tel2 [40];
>  struct fecha cumple;
>  };
> 
> 
> void limpiar();
> void llenardatos();
> void abrir();
> 
> 
> #define TEXT 0
> #define VIDEOMODE 12

Don't do that. I did something a bit like that in 1993 and it haunted
me for years. :-) Use "#define VIDEOMODE G1024x768x256", which also
gives the reader some idea of what the mode *is*...

> int i, j, rta, xmax, ymax, midx, midy, x1, x2, y1, b, c, fd;
> 
> GraphicsContext *physical_screen;
> GraphicsContext *virtual_screen;
> 
> 
> struct personas *contactos;
> 
> int main (void)
> {
> 
> 
> extern unsigned char *gl_font8x8;

Not a good place to put an extern, IMHO.

> vga_disabledriverreport();
> vga_init();
> vga_setmode(VIDEOMODE);
> gl_setcontextvga(VIDEOMODE);
> 
> physical_screen = gl_allocatecontext();
> gl_getcontext(physical_screen);
> gl_setcontextvgavirtual(VIDEOMODE);
> virtual_screen = gl_allocatecontext();
> gl_getcontext(virtual_screen);
> gl_setcontext(virtual_screen);
> gl_setfont(8,8,gl_font8x8);
> gl_setwritemode(FONT_COMPRESSED + WRITEMODE_MASKED);
> 
> xmax = vga_getxdim();
> ymax = vga_getydim();
> midx = xmax / 2;
> midy = ymax / 2;
> 
> 
> 
> 
> 
> gl_clearscreen(0);
> vga_setmode(TEXT);
> 
> exit(EXIT_SUCCESS);
> }

Your main() doesn't actually call agregar() - maybe you should have
sent us the program which *did* segfault, rather than one which
couldn't have. ;-) But I'll assume you meant the call to be in the gap
above.

> void abrir ()
> {
> 
> if(fd = open("contactos.dat", O_CREAT, 0644) < 0) {

"foo.c:84: warning: suggest parentheses around assignment used as truth value"

gcc -Wall is your friend. :-) I think you meant:

if((fd = open("contactos.dat", O_CREAT, 0644)) < 0) {

With the line you previously had, you were only ever going to get 0 or
1 in `fd' - stdin or stdout. (In the `successful' case, it would be
stdin.) Probably not what you meant.

>  mensaje("Hubo un error al abrir el Archivo");
>  exit(EXIT_FAILURE);
>  }
> }

We didn't get to see mensaje(), but I'll assume it's basically
puts()-like. (That doesn't seem very important though.)

> void agregar()
> {
> abrir();
> llenardatos();
> }
> 
> void llenardatos (void)
> {
> 
> limpiar();
> gl_printf(300,100,"Apellido");
> gl_copyscreen(physical_screen);
> 
> i = 0;
> 
> /* i believe the problem is here. When this line arrives i get a
> segmentation fault. Signal 11. Core  Dumped. Im not sure, but perhaps there
> is something bad with the structures. Please test the code and see in which
> way it can work: receiving the characters and printing it to the location.
> (I will make the 'cursor' to move, dont worry, but for now i just want to
> see where is the mistake and in which way I can get a char, store it in the
> structure and printing it. */

The first time the loop is entered and the while() condition
evaluated, `contactos' is NULL, leading to the segfault. In fact, you
don't *ever* give `contactos' a value. I suspect you meant your
earlier `struct personas *contactos' to be an array rather than a
pointer, or perhaps you meant to malloc() some space for it but
forgot.

> while((contactos->apellido[i] = getchar()) != '\n') {

It would be better to use vga_getch() than getchar().

>  gl_printf(600,100, "%c", contactos->apellido[i]);
>  gl_copyscreen(physical_screen);

Rewriting the entire 768k screen for every keypress may be overdoing
it slightly. :-)

>  i++;
>  }
> 
> 
> getchar();
> }
> 
> 
> void limpiar(void)
> {
> gl_clearscreen(0);
> }

-Rus.


This archive was generated by hypermail 2.1.4 : Wed 21 Jan 2004 - 22:10:23 IST