Message-ID: <37B0FEBD.2D5B@homemail.com> From: Matthew Haley Organization: Home X-Mailer: Mozilla 3.04Gold (Win16; I) MIME-Version: 1.0 Newsgroups: comp.os.msdos.djgpp Subject: Re: funny errors from RHIDE References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Date: Wed, 11 Aug 1999 04:41:47 +0000 NNTP-Posting-Host: 209.181.109.144 X-Trace: news.uswest.net 934371701 209.181.109.144 (Wed, 11 Aug 1999 06:41:41 CDT) NNTP-Posting-Date: Wed, 11 Aug 1999 06:41:41 CDT Lines: 79 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Hi difman, difman wrote: > > sorry for reply to own post but the c code that caused the errors is > attached to this message. > >#include // You should #include also > >main(void) // main() should return an int int main(void) >{ > FILE *text; > char filename[10]; // This should be bigger char filename[127]; > char mytext[1000]; > int x = 0; > int y = 0; > > printf("Enter name of file: "); > gets(filename, 80); // incorrect see libc info gets(filename); > > text = fopen(filename, "r"); // Should check if this fails if (!text) { printf("Error opening %s\n", filename); return 1; } > > while(mytext[x] != EOF) { // mytext[] is unitialized... > x++; > } > > while(y != x/2) { > mytext[y] = fgetc(filename); // again mytext[] is unitialized > putchar(mytext[y]); > y++; > } > > fclose(*text); // this is wrong fclose(text); > > return 0; >} > Okay, I assume you're trying to read in a file and print it to the screen right? Since I couldn't see how hacking your code into actually working would benefit here is what you *could* have done :) #include #include int main(void) { FILE *text_fp; // this is a file pointer not the contents of a file char filename[128]; // space for a filename i.e. c:/test/file.txt printf("Enter name of file: "); gets(filename); // get the users input text_fp = fopen (filename, "r"); if (!text_fp) // check for failed fopen { printf ("Failed to open %s\n", filename); return (EXIT_FAILURE); } // This reads each char and prints them until EOF while ( putchar(fgetc(text_fp)) != EOF) ; fclose (text_fp); return (EXIT_SUCCESS); } But also, this isn't the best or only way to do it.