B
Blankdraw
Both the programs below fail to run on a Windows DOS window, after
build by MVC/C++ 4.2. No compiler problems, they just refuse to give
me anything but:
"Cannot open input file". Can someone run this and tell me what you
did to make it go? They are from QUE's Crash Course in C, and should
work as flawlessly as the source compiles.
/* WRITER.C - sends characters to text file */
/* as copyrighted - not debugged yet */
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
int main()
{
char ch, filename[85];
FILE *fileptr;
printf("\nEnter filename: ");
gets(filename);
printf("\n\nType # on a blank line to end\n\n");
if ((fileptr = fopen(filename, "w")) == NULL );
{
printf("Error: Cannot open input file\n");
exit(0);
}
while ((ch=getchar()) != '#')
{
putc(ch, fileptr);
}
fclose(fileptr);
return 0;
}
Supposedly maintains a loop which fills up a default keyboard buffer
with every keystroke entered by user, until it is full or until user
termination, when fclose() terminates the program while dumping the
buffer into the disk file.
These programs use functions that read or write an entire line of text
at a time, so that file I/O for strings is as for single characters.
STRTYPE.C reads lines of text (=records???) with the same algorithm as
TYPE.C and displays them. It tracks the lines read and tallies their
number in the data file.
/* STRTYPE.C - Sends characters read from the file to the display
*/
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#define MAXLINELEN 135
int main()
{
char ch, filename[85], strline[MAXLINELEN];
int line = 0;
FILE *fileptr;
printf("\nEnter filename: ");
gets(filename);
if ((fileptr = fopen(filename, "r")) == NULL)
{
printf("Error: cannot open input file\n");
exit(0);
}
while (!feof(fileptr))
{
fgets(strline, MAXLINELEN, fileptr); /* get next char from
file */
printf("%s",strline); /* display char on screen */
line++;
}
fclose(fileptr);
printf("\n***There were %d lines "
"in that file\n", line);
return 0;
}
build by MVC/C++ 4.2. No compiler problems, they just refuse to give
me anything but:
"Cannot open input file". Can someone run this and tell me what you
did to make it go? They are from QUE's Crash Course in C, and should
work as flawlessly as the source compiles.
/* WRITER.C - sends characters to text file */
/* as copyrighted - not debugged yet */
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
int main()
{
char ch, filename[85];
FILE *fileptr;
printf("\nEnter filename: ");
gets(filename);
printf("\n\nType # on a blank line to end\n\n");
if ((fileptr = fopen(filename, "w")) == NULL );
{
printf("Error: Cannot open input file\n");
exit(0);
}
while ((ch=getchar()) != '#')
{
putc(ch, fileptr);
}
fclose(fileptr);
return 0;
}
Supposedly maintains a loop which fills up a default keyboard buffer
with every keystroke entered by user, until it is full or until user
termination, when fclose() terminates the program while dumping the
buffer into the disk file.
These programs use functions that read or write an entire line of text
at a time, so that file I/O for strings is as for single characters.
STRTYPE.C reads lines of text (=records???) with the same algorithm as
TYPE.C and displays them. It tracks the lines read and tallies their
number in the data file.
/* STRTYPE.C - Sends characters read from the file to the display
*/
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#define MAXLINELEN 135
int main()
{
char ch, filename[85], strline[MAXLINELEN];
int line = 0;
FILE *fileptr;
printf("\nEnter filename: ");
gets(filename);
if ((fileptr = fopen(filename, "r")) == NULL)
{
printf("Error: cannot open input file\n");
exit(0);
}
while (!feof(fileptr))
{
fgets(strline, MAXLINELEN, fileptr); /* get next char from
file */
printf("%s",strline); /* display char on screen */
line++;
}
fclose(fileptr);
printf("\n***There were %d lines "
"in that file\n", line);
return 0;
}