Xref: news2.mv.net comp.os.msdos.djgpp:6355 Newsgroups: comp.os.msdos.djgpp From: Robert Jaycocks Subject: Re: C Program crashes in Djgpp Sender: usenet AT lboro DOT ac DOT uk (Usenet-News) Message-ID: <31F619EE.424A@lut.ac.uk> Date: Wed, 24 Jul 1996 12:41:18 GMT To: PENG ZHOU Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii References: <4t3hfa$p5n AT sjx-ixn6 DOT ix DOT netcom DOT com> Mime-Version: 1.0 Organization: LUT Lines: 66 DJ-Gateway: from newsgroup comp.os.msdos.djgpp PENG ZHOU wrote: > > Hi, > My program fileread.c which works ok with my MSC but crashes with > some general protection faults in DJGPP. Don't know why. > > --------------------FileRead.c---------------------- > #include > #include > #include > > int main(int argc, char **argv) > { > FILE *input;Change this to FILE *input = NULL; (See below for explanation. ) > char ch; > int arr; > > for ( arr = 0; arr < argc; arr++ ) This should be for (arr = 1;arr < argc;arr++). argv[0] is the program name. > { > if ( (strcmp(argv[arr],"-help)==0) || (strcmp(argv[arr],"-h")==0) ) > { > puts("FileRead.EXE v.1\n"); > puts("Usage: C>FileRead -file filename.xxx\n"); > exit(0); > } > > if ( (strcmp(argv[arr],"-file)==0) || (strcmp(argv[arr],"-f")==0) ) > { > input = fopen(argv[arr+1], "r"); > if ( input == NULL ) > { > puts("Cannot open file!\n"); > exit(1); > } > } > } > What happens when there are no arguments. input is not set correctly. Initially set input to NULL, then add an if statement before this: if(input != NULL) { > while (!feof(input)) > { > ch = getc(input); > printf("%c", ch); > } > fclose(input); } > return 0; > } > > This program might have a few bugs but it runs ok in MSC, compiles ok > in DJGPP but crashes when runned. Any Suggestions? > > ---- > pengzh AT ix DOT netcom DOT com > > > Apart from the points I mentioned above, I cannot see any other errors. Robert