Using SDL-C library, mouse motion bug in simple game - c

I am trying to make a simple game. I have the following code with which I am placing some images on the screen. I want to make it so that when left click is pressed and I move the mouse, those images are continuously put on the screen (so that the player does not have to click many times over.) I have the following:
SDL_Surface *mur = NULL, *caisse = NULL, *objectif = NULL, *mario = NULL;
SDL_Rect position;
SDL_Event event;
int continuer = 1, clic_gauche = 0, clic_droit = 0;
int objet_actuel = MUR, i = 0, j = 0;
int carte[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR] = {0};
//Chargement des objets et du niveau.
mur = IMG_Load("mur.jpg");
caisse = IMG_Load("caisse.jpg");
objectif = IMG_Load("objectif.png");
mario = IMG_Load("mario_bas.gif");
if(!Charger_Niveau(carte)) //Erreur de chargement du niveau.
exit(EXIT_FAILURE);
while(continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
{
carte[event.button.x/TAILLE_BLOC][event.button.y/TAILLE_BLOC] = objet_actuel; //On met l'objet actuellement choisi a l'endroit du clic.
clic_gauche = 1; //On retient le fait que le dernier clic etait un clic gauche.
}
else if (event.button.button = SDL_BUTTON_RIGHT)
{
carte[event.button.x/TAILLE_BLOC][event.button.y/TAILLE_BLOC] = VIDE; //On vide le bloc ou s'est produit le clic droit.
clic_droit = 1; //On retient le fait que le dernier clic etait un clic droit.
}
break;
case SDL_MOUSEBUTTONUP: //On desactive le booleen qui disait qu'un bouton etait enfonce.
if(event.button.button == SDL_BUTTON_LEFT)
clic_gauche = 0;
else if(event.button.button == SDL_BUTTON_RIGHT)
clic_droit = 0;
break;
case SDL_MOUSEMOTION:
if(clic_gauche == 1)
{
carte[event.motion.x / TAILLE_BLOC][event.motion.y / TAILLE_BLOC] == objet_actuel;
}
else if(clic_droit == 1)
{
carte[event.motion.x/TAILLE_BLOC][event.motion.y/TAILLE_BLOC] == VIDE;
}
break;
The loop then continues checking if some keys have been pressed, then I blit all the images that were loaded using the double matrix carte and I refresh the screen. The problem is mouse motion doesn't work. Debugging events is rather difficult...does anyone see an evident problem with the code?

You're not assigning into carte in the SDL_MOUSEMOTION handler, you're comparing.
(Unfortunately, any expression is allowed to be a statement in C, even if it has no effects/side-effects on the program, so the compiler doesn't necessarily help you find these issues.)
Your handler should be:
case SDL_MOUSEMOTION:
if(clic_gauche == 1)
{
carte[event.motion.x / TAILLE_BLOC][event.motion.y / TAILLE_BLOC] = objet_actuel;
}
else if(clic_droit == 1)
{
carte[event.motion.x/TAILLE_BLOC][event.motion.y/TAILLE_BLOC] = VIDE;
}
break;
(Note the single = in each case of the if/else)
Also, the inverse issue affects the handler for SDL_MOUSEBUTTONDOWN where you're assigning to event.button.button rather than comparing its value against the constant SDL_BUTTON_RIGHT.
Note: for this reason, many developers have taken to putting the constant on left-hand side of a comparison, so that the compiler will give you an error if you mistype the operator. E.g.,
else if (SDL_BUTTON_RIGHT == event.button.button)

Related

Create program Random

i want to randomize this program that recreates a quiz
I tried to add the rand function but I don't know how to proceed
#include <stdio.h>
int main(void) {
int domanda, punti=0;
puts("Quiz sul Riscaldamento Globale\n Otterrai 1 punto per ogni risposta esatta e 0 per ogni risposta sbagliata o non valida\n");
puts("D1) Secondo il tredicesimo Obiettivo di Sviluppo Sostenibile, la minaccia piu' grande allo sviluppo e'/sono:\n");
puts("[1] I cambiamenti climatici\n"
"[2] L'inquinamento \n"
"[3] Lo scioglimento della calotta glaciale polare \n"
"[4] La crescita demografica\n");
scanf("%d", &domanda);
if(domanda == 1){
puts("risposta corretta\n");
punti = punti + 1;
}else{
puts("risposta sbagliata\n");
punti= punti + 0;
}
puts("D2) Il tempo e il clima sono la stessa cosa.");
puts("[1] Vero \n"
"[2] Falso \n");
scanf("%d", &domanda);
if(domanda == 2){
printf("risposta corretta\n");
punti =punti + 1;
}else{
printf("risposta sbagliata\n");
punti= punti + 0;
}
printf("hai fatto un punteggio di %d su 2", punti);
}
to work it works but I don't understand how to add the random if anyone knows how to do it help me thanks.
A rand() function is available in C standard library: man 3 rand. If you want to explore random number generation, check random.org.
As is, your program is not designed to support randomness: Questions and answers are hard-coded. A first step to make it randomizable would be to store them into a variable. For instance (with correct answer always at first position, end of questions identified with NULL, end of answers of a question identified with NULL, maximum of nine answers per question):
struct {
char * question;
char * answers[10];
} qa[] = {
{
"Secondo il tredicesimo Obiettivo di Sviluppo Sostenibile, la minaccia piu",
{
"I cambiamenti climatici",
"L'inquinamento",
"Lo scioglimento della calotta glaciale polare",
"La crescita demografica",
NULL,
}
},
{
"Il tempo e il clima sono la stessa cosa.",
{
"Falso",
"Vero",
NULL,
}
},
{ NULL, NULL }
};
A program using this variable would then count the number of questions, take one randomly, then display the answers randomly and remember what is the display index of the correct one.

Split a senctence into words and store them in linked list

I'm trying to write a program that gets a string, split it into words and then stores every single word in a linked list. The idea here is that every time the program reads a separator(space, comma, semicolon...) it appends the word into a matrix to store them into linked lists afterwards.
The problem here is that when it displays the matrix or linked list content, it display random characters at the end.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct Mot_De_La_Phrase {
char Mon_mot[50];
struct Mot_De_La_Phrase* Adresse_Mot_suivant;
}Mot;
Mot* Inserer_au_debut(Mot* les_mots_de_laphrase,char mot[]){
// creer un nouvel element
Mot* premier_mot = malloc(sizeof(Mot));
// copier la chaine de caractere
strcpy(premier_mot->Mon_mot, mot);
// affecter le pointeur du suivant au debut de le liste principale
premier_mot->Adresse_Mot_suivant=les_mots_de_laphrase;
// retourner l'# du 1er element
return premier_mot;
}
Mot* Inserer_a_la_fin(Mot* les_mots_de_laphrase, char* mot){
// si la liste chainee est vdie
if(les_mots_de_laphrase == NULL){
// cration de la liste / reservation de memoire pour le 1er element
les_mots_de_laphrase=malloc(sizeof(Mot));
// c'est le 1er et dernier element (donc le prochain element est NULL)
les_mots_de_laphrase->Adresse_Mot_suivant=NULL;
// copier le mot
strcpy(les_mots_de_laphrase->Mon_mot,mot);
// retourner l'addresse du 1er element
return les_mots_de_laphrase;
}
// si la liste chainee n'est pas vide affecter le 1er element a un pointeur temporaire
Mot* dernier_mot=les_mots_de_laphrase;
// tanque le pointeur suivant n'es pas NULL (tanqu'on est pas arrivé au dernier
while(dernier_mot->Adresse_Mot_suivant!=NULL){
// passer a l'element suivant
dernier_mot=dernier_mot->Adresse_Mot_suivant;
}
// creer un nouvel element a la fin de la liste
dernier_mot->Adresse_Mot_suivant=malloc(sizeof(Mot));
// copier le mot
strcpy(dernier_mot->Adresse_Mot_suivant->Mon_mot,mot);
// c'est le 1er et dernier element (donc le prochain element est NULL)
dernier_mot->Adresse_Mot_suivant->Adresse_Mot_suivant=NULL;
// retourner l'addresse du 1er element
return les_mots_de_laphrase;
}
void Afficher_Liste(Mot* les_mots_de_laphrase){
if(les_mots_de_laphrase == NULL)
printf("La Liste est vide.\n");
else{
Mot* tmp = les_mots_de_laphrase;
while(tmp!=NULL)
{
printf("%s\n",tmp->Mon_mot);
tmp = tmp->Adresse_Mot_suivant;
}
}
}
int separateur(char a){
return(a == ' ' || a == ',' || a == ';' || a == '.' || a == '?' || a == '!' || a == '-');
}
int decoupage(char phrase[], char mots[][50]){
int i,j,k;
i=0;
j=0;
k=0;
for(i=0;phrase[i]!='\0';i++){
if(separateur(phrase[i])){
mots[k][j] = '\0';
k++;
j=0;
i++;
}
mots[k][j] = phrase[i];
j++;
}
for(i=0;i<=k;i++)
printf("%s\n",mots[i]);
return k;
}
void main(){
char phrase[100];
char mots_phrase[50][50];
Mot *mots = NULL;
int i, n;
gets(phrase);
printf("%s\n",phrase);
n = decoupage(phrase, mots_phrase);
mots = Inserer_a_la_fin(mots, mots_phrase[0]);
for(i=1;i<=n;i++)
mots = Inserer_a_la_fin(mots, mots_phrase[i]);
Afficher_Liste(mots);
}
I know it looks messy as hell. I'm just getting back to C after a while.
Also, I had to write keywords and comments in french because I have to send it to my college, sorry.

Drawing a line: Problems with the mouse cursor on C Vga graphics

I´m trying to make a program where you can draw a line by clicking the mouse, the line will then change size and direction while you move the with the button pressed and will stay on the screen when you release it (Just as in paint or any other photo editor). I´m working on C with 256-VGA. The problem I´m having is then drawing a line on a space where the mouse cursor is. I have this two functions to show and hide the mouse:
typedef struct MOUSE{
byte on;
byte button1;
byte button2;
byte button3;
int num_buttons;
sword x;
sword y;
byte under[MOUSE_SIZE];
MOUSEBITMAP *bmp;
} MOUSE;
/**************************************************************************
* show_mouse *
* Displays the mouse. *
**************************************************************************/
void show_mouse(MOUSE *mouse){
int x,y;
int mx = mouse->x - mouse->bmp->hot_x;
int my= mouse->y - mouse->bmp->hot_y;
long screen_offset = (my<<8)+(my<<6);
word bitmap_offset = 0;
byte data;
//memcpy(double_buffer,VGA,SCREEN_SIZE);
/* allocate memory for double buffer and background image */
/*if ((double_buffer = (byte *) malloc(SCREEN_SIZE)) == NULL)
{
printf("Not enough memory for double buffer.\n");
exit(1);
} */
for(y=0;y<MOUSE_HEIGHT;y++){
for(x=0;x<MOUSE_WIDTH;x++,bitmap_offset++){
mouse->under[bitmap_offset] = VGA[(word) (screen_offset+mx+x)];
if(mx+x < SCREEN_WIDTH && mx+x >=0 &&
my+y < SCREEN_HEIGHT && my+y >=0) //Verificando que estemos dentro de pantalla
{
/*Pintando en pantalla*/
data = mouse->bmp->data[bitmap_offset];
if(data)
{
VGA[(word)(screen_offset+mx+x)] = data;
}
}
}
screen_offset+=SCREEN_WIDTH;
}
/*show_buffer(double_buffer);
free(double_buffer);*/
}
/**************************************************************************
* hide_mouse *
* hides the mouse. This code is not optimized. *
**************************************************************************/
void hide_mouse(MOUSE *mouse){
int x,y;
int mx = mouse->x - mouse->bmp->hot_x;
int my = mouse->y - mouse->bmp->hot_y;
long screen_offset = (my<<8)+(my<<6);
word bitmap_offset = 0;
/*memcpy(double_buffer,VGA,SCREEN_SIZE);
/* allocate memory for double buffer and background image */
/*if ((double_buffer = (byte *) malloc(SCREEN_SIZE)) == NULL)
{
printf("Not enough memory for double buffer.\n");
exit(1);
} */
for(y=0;y<MOUSE_HEIGHT;x++,y++){
for(x=0;x<MOUSE_WIDTH;x++,bitmap_offset++){
//Verificando que este dentro de pantalla
if(mx+x < SCREEN_WIDTH &&mx+x >=0 &&
my+y <SCREEN_HEIGHT && my+y >=0){
/*Pintando en pantalla*/
VGA[(word)(screen_offset+mx+x)] = mouse->under[bitmap_offset];
}
}
screen_offset+=SCREEN_WIDTH;
}
/*show_buffer(double_buffer);
free(double_buffer);*/
}
I have the under variable of my MOUSE struct, which has the background of the screen before it´s drawn. In order to make the line move with the mouse when button is held down I store the previous position of the mouse and draw a white line (to clean it up) and then another line with the new position.
The problem is that then I make a line smaller in size and move the mouse pointer along the line, the repainting of the mouse is drawing once again the line that was before.
I´m getting something like this:
Before shrinking the line:
After Shrinking the line:
This is the rest of the code related to the loop controlling the mouse buttonsvoid realizar_accion(){
void realizar_accion(){
/*Caso1: Pintar pixel simple (lapiz)*/
if(accion==1){
plot_pixel(mouse_g.x,mouse_g.y,2);
plot_pixel(mouse_g.x-1,mouse_g.y,2);
plot_pixel(mouse_g.x,mouse_g.y-1,2);
plot_pixel(mouse_g.x-1,mouse_g.y-1,2);
}
/*Caso2: Pintar Linea*/
else if(accion==2){
init_x = mouse_g.x;
prevx = mouse_g.x;
init_y = mouse_g.y;
prevy = mouse_g.y;
//line(mouse_g.x,mouse_g.y,mouse_g.x-20,mouse_g.y-20,2);
}
}
void realizar_accion_mantenido(){
/*Caso1: Pintar pixel simple (lapiz)*/
if(accion==1){
plot_pixel(mouse_g.x,mouse_g.y,2);
plot_pixel(mouse_g.x-1,mouse_g.y,2);
plot_pixel(mouse_g.x,mouse_g.y-1,2);
plot_pixel(mouse_g.x-1,mouse_g.y-1,2);
}
/*Caso2: Pintar Linea*/
else if(accion==2){
if(on_draw==-1){
line(init_x,init_y,mouse_g.x,mouse_g.y,2);
prevx=mouse_g.x;
prevy=mouse_g.y;
on_draw=0;
}
else{
if(prevx!=mouse_g.x&&prevy!=mouse_g.y){
line(init_x,init_y,prevx,prevy,0xFFFF); //borrando la linea anterior
line(init_x,init_y,mouse_g.x,mouse_g.y,2);
prevx=mouse_g.x;
prevy=mouse_g.y;
}
}
}
}
int sobre_barra(){
if(new_x1>0 && new_x1<33 &&new_y1>0 &&new_y1<181){
return 1;
}
else{
return -1;
}
}
void boton_soltado(){
/*Caso1: Pintar pixel simple (lapiz)*/
if(accion==1){
}
/*Caso2: Pintar Linea*/
else if(accion==2){
if(on_draw==0){
line(init_x,init_y,prevx,prevy,0xFFFF); //borrando la linea anterior
wait_for_retrace();
hide_mouse(&mouse_g);
if (mouse_new!=NULL) mouse_g.bmp=mouse_new;
mouse_g.x = new_x1;
mouse_g.y=new_y1;
show_mouse(&mouse_g);
line(init_x,init_y,mouse_g.x,mouse_g.y,2);
prevx=mouse_g.x;
prevy=mouse_g.y;
on_draw=-1;
}
}
}
void boton_mantenido(){
/*Verificar que este dento del buffer de dibujo....*/
if(sobre_barra()!=1){
realizar_accion_mantenido();
}
}
void boton_presionado(){
if(sobre_barra()==1){
cambiar_herramienta();
}
else{
realizar_accion();
}
}
/**************************************************************************
* paint_screen *
* show main screen paint *
**************************************************************************/
void paint_screen(BITMAP *fondo){
int mantenido;
BITMAP barra,barra_color,normal_ptr_image;
int anterior_presionado;
word last_time;
word redraw,press,release;
sword dx,dy=0;
MOUSEBITMAP *mouse_new=NULL;
int i,done = 0;
on_draw=-1;
accion =2;
current_color=2;
/*Pintando fondo blanco*/
clear_screen();
/*Pintando barra de herramientas*/
load_bmp("normal.bmp",&normal_ptr_image);
load_bmp("mainbar.bmp",&barra);
load_bmp("colores.bmp",&barra_color);
set_pallete(fondo->pallete);
draw_bitmap(&barra,0,0);
draw_bitmap(&barra_color,0,180);
load_mouse(&mouse_g);
show_mouse(&mouse_g);
wait(50);
while(!done){
if(redraw){
wait_for_retrace();
hide_mouse(&mouse_g);
if (mouse_new!=NULL) mouse_g.bmp=mouse_new;
mouse_g.x = new_x1;
mouse_g.y=new_y1;
show_mouse(&mouse_g);
redraw=0;
mouse_new=NULL;
}
do { // check mouse status
anterior_presionado = press;
get_mouse_motion(&dx,&dy);
press = get_mouse_press(LEFT_BUTTON);
release = get_mouse_release(LEFT_BUTTON);
//Si el estado estaba presionado y no se ha soltado.. el boton esta mantenido
if(anterior_presionado==1 &&release==0){
mantenido =1;
}
} while (dx==0 && dy==0 && press==0 && release==0&&*my_clock==last_time);
if (release){
mouse_g.button1=0;
mantenido=0;
boton_soltado();
}
if (press){
mouse_g.button1=1;
boton_presionado();
}
//El boton se mantiene presionado
else if(mantenido){
boton_mantenido();
}
else{
release=1;
}
if (dx || dy) // calculate movement
{
new_x1 = mouse_g.x+dx;
new_y1 = mouse_g.y+dy; //Actualizamos posicion mouse
if (new_x1<0) new_x1=0;
if (new_y1<0) new_y1=0;
if (new_x1>319) new_x1=319;
if (new_y1>199) new_y1=199;
redraw=1;
}
if(new_x1>=287 && new_x1 <320
&& new_y1>=180 && new_y1 < 200 &&press){
set_mode(TEXT_MODE);
printf("Adios!!");
wait(25);
done=0;
return;
}
}
}
Can anyone help me solve this issue?
I don't recommend drawing a white line over the old line. Instead, you should be using a frame-buffer, which is an off-screen memory area where you re-assemble it each time. Draw the background, then the line. Once you place the line, draw it onto the background.. but until then it's drawn afterwards. Then the whole thing is flipped/copied to the screen on each loop. Make sense?

string pointers not working between functions

I am trying to assign different error messages to the error variable in each case by passing a pointer to the string between functions but for any reason is not working well.
Here is the code:
//This function takes a pointer to a char.
int lee(Fecha *f, char *error){
int checking;
printf("Introduzca una fecha compuesta por día, mes, y año\n");
checking = scanf("%i %i %i", &f->day, &f->month, &f->year);
printf("%d\n", checking);
switch (checking){
case 0:
//The message is assigned to the space where error points to.
*error = "Formato de fecha incorrecto. El día debe ser un entero.";
break;
case 1:
*error = "Formato de fecha incorrecto. El mes debe ser un entero.";
break;
case 2:
*error = "Formato de fecha incorrecto. El año debe ser un entero.";
break;
}
return (checking == 3);
}
int main(){
Fecha f;
//error is declared like a pointer
char * error;
int ret;
//The pointer is passed to the function as argument.
ret = lee(&f, error);
printf("%s", error);
return 0;
}
And the output:
user#console:~/$ ./Sesion1
Introduzca una fecha compuesta por día, mes, y año (o 0 0 0 para terminar el programa)
23 dfgadkfhgsñdgh 5
1
Segmentation fault
Since you want to make an output to the string, you need to pass pointer to your char*, so you need to pass error as char**.
So the function will be
int lee(Fecha *f, char **error){
int checking;
printf("Introduzca una fecha compuesta por día, mes, y año\n");
checking = scanf("%i %i %i", &f->day, &f->month, &f->year);
printf("%d\n", checking);
switch (checking){
case 0:
//The message is assigned to the space where error points to.
*error = "Formato de fecha incorrecto. El día debe ser un entero.";
break;
case 1:
*error = "Formato de fecha incorrecto. El mes debe ser un entero.";
break;
case 2:
*error = "Formato de fecha incorrecto. El año debe ser un entero.";
break;
}
return (checking == 3);
}
int main(){
Fecha f;
//error is declared like a pointer
char * error;
int ret;
//The pointer is passed to the function as argument.
ret = lee(&f, &error);
printf("%s", error);
return 0;
}
Also, you should consider not leaving pointers without value after declaration like you did with char* error in main().
The problem is, when you have a pointer that is not initialized and then you forget to give it a value or something like this, it can point anywhere within your application memory (or beyond it). Virtual memory protects you from reading and writing to others applications memory (generally speaking), but you can have a case where the pointer happens to point to some data inside your application. And if you then use this pointer to write or read, you will have unpredictable result, ranging from just displaying erroneous data to corrupting data or crashing application, which you obviously do not want.
You need to pass a pointer to your function output parameters. Since your output parameter is char*, the pointer to it is char**.
To fix, change the corresponding lines to:
int lee(Fecha *f, char **error);
// ...
ret = lee(&f, &error);

Crashing program when moving from computer to computer

i'm having a problem with a little C program i'm making. It's working fine when i compile it and run it on my mac (on a windows 7 partition) and on my work pc (windows 7 too) but i need it to work on a PC that is running a CNC and it is on windows XP. I can change the program location on the two computers, but it won't work anywhere on the windows XP pc It seems to be crashing when it has to write a text file but it's able to overwrite one ... Is there some functions that aren't compatible with windows XP ? I'll try some more and i'll let you know if i come across something.
P.S. can't use the debugguer on this computer because i can't install programs ... (work computer).
Here's the code that's causing a problem :
int WritePrograms(int vitesse, double hauteur, int longueur, int largeur, double localRabotage, double localGroove, int overdrive)
{
char nameOfFile[75];
char nomProgramR[75];
char nomProgramG[75];
char cheminDaccesR[100];
char cheminDaccesG[100];
char cheminDaccesGen[] = "programmes/";
char genR[] = "_Rabotage.nc";
char genG[] = "_Groove.nc";
char confirmation [3];
int i = 0;
int c = 0;
char *nomProgram = NULL;
char *caractere = NULL;
FILE* fichierRabotage = NULL;
FILE* fichierGroove = NULL;
do
{
memset(nomProgramG,0,sizeof(nomProgramG));
memset(nomProgramR,0,sizeof(nomProgramR));
memset(nameOfFile,0,sizeof(nameOfFile));
memset(cheminDaccesG,0,sizeof(cheminDaccesG));
memset(cheminDaccesR,0,sizeof(cheminDaccesR));
do
{
printf("\nVeuillez choisir un nom pour votre programme : ");
nomProgram = Saisie(nameOfFile,60);
}while (nomProgram == "0");
if (strcmp(nomProgram,"exit") == 0)
{
printf("\nVous avez decider de ne pas creer le programme\n\n");
Sleep(2000);
exit(0);
}
else if (strcmp(nomProgram,"\0") == 0)
{
printf("\nVous n'avez pas rentrer de nom pour votre programme\n");
sprintf(nomProgramR,"%dX%.0fX%dX%d_Rabotage.nc",vitesse,hauteur*10000,longueur,largeur);
sprintf(nomProgramG,"%dX%.0fX%dX%d_Groove.nc",vitesse,hauteur*10000,longueur,largeur);
printf("\nUn nom generique lui a ete attribuer\n");
sprintf(nomProgram,"%dX%.0fX%dX%d",vitesse,hauteur*10000,longueur,largeur);
printf("\n%s\n",nomProgram);
strcpy(cheminDaccesR,cheminDaccesGen);
strcpy(cheminDaccesG,cheminDaccesGen);
strcat(cheminDaccesR,nomProgramR);
strcat(cheminDaccesG,nomProgramG);
}
else
{
strcpy(nomProgramG,nomProgram);
strcpy(nomProgramR,nomProgram);
strcpy(cheminDaccesR,cheminDaccesGen);
strcpy(cheminDaccesG,cheminDaccesGen);
strcat(nomProgramR,genR);
strcat(nomProgramG,genG);
strcat(cheminDaccesR,nomProgramR);
strcat(cheminDaccesG,nomProgramG);
}
fichierRabotage = fopen(cheminDaccesR,"r");
fichierGroove = fopen(cheminDaccesG,"r");
if (fichierRabotage != NULL || fichierGroove != NULL)
{
do
{
printf("\nLe fichier existe deja, voulez-vous le remplacez (O/N)?");
caractere = Saisie(confirmation,3);
}while (strcmp(caractere,"O") != 0 && strcmp(caractere,"o") != 0 && strcmp(caractere,"n") != 0 && strcmp(caractere,"N") != 0);
if (strcmp(caractere,"O") == 0 || strcmp(caractere,"o") == 0)
{
c = 1;
}
}
else
{
c = 1;
}
}while (c != 1);
fclose(fichierGroove);
fclose(fichierRabotage);
fichierRabotage = fopen(cheminDaccesR,"w+");
fichierGroove = fopen(cheminDaccesG,"w+");
if (fichierRabotage != NULL && fichierGroove != NULL)
{
}
else
{
printf("\nLe programmme n'as pas pu etre creer\n");
Sleep(2000);
exit(0);
}
fprintf(fichierGroove, "[OUTIL 3-CARBIDE 1/8-TEMPS: 7MIN] \n");
fprintf(fichierGroove, "[MATERIEL-MOUSSE RPM 200]\n");
fprintf(fichierGroove, "G20\nG00\nG90\nT2\nG53 Z\nG53 XY\nG92 Z%.4f\nG4 [Changer l'outil]\nM03\n",localGroove);
fprintf(fichierGroove, "G0 X2.5\nG0 Y2.5\nG0 Z%.4f\n",hauteur);
return 0;
}
and here is headers.h as ask :
#ifndef HEADERS_H_INCLUDED
#define HEADERS_H_INCLUDED
char *Saisie(char *str,int n);
void viderBuffer();
int SaisieNombre(char *chaine,int a);
double SaisieNombreDecimal(char *chaine,int a);
double LireValeurCorrespondante(double thick);
int GetLocals(double *groove, double *rabotage);
int WritePrograms(int vitesse, double hauteur, int longueur, int largeur, double localRabotage, double localGroove, int overdrive);
#endif // HEADERS_H_INCLUDED
The problem here was that by opening two files to check if they existed and if one of them existed. the program would tell me if i wanted to overwrite it. I'd then close both files and overwrite them if needed but if they didn't open, it would try to close a NULL pointer. So the solution was to do this :
if (fichierGroove != NULL)
{
fclose(fichierGroove);
}
if (fichierRabotage != NULL)
{
fclose(fichierRabotage);
}
Lesson here : You can close a NULL pointer in windows 7 but not on XP
Not sure for all other OSes.

Resources