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 :
Lesson here : You can close a NULL pointer in windows 7 but not on XP
Not sure for all other OSes.