String-to-textfile programs will not run in DOS window...

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;
}
 
M

Malcolm

Blankdraw said:
/* 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 );
For some reason the program cannot open the file for writing. This could be
an OS issue - do you have write permission in the directory you told it to
create the file in? Did you pass it a valid path? Are you sure that you
entered fewer than 84 characters?
 
M

MG

if ((fileptr = fopen(filename, "w")) == NULL );

Use if ((fileptr = fopen(filename, "w+")) == NULL );

when you mention "w" as the option for the opening the file, you assume that
the file existed..

HTH
MG
 
I

Irrwahn Grausewitz

MG said:
Use if ((fileptr = fopen(filename, "w+")) == NULL );

when you mention "w" as the option for the opening the file, you assume that
the file existed..

Sorry, but I definitly have to disagree on that:

r open text file for reading
w truncate to zero length or create text file for writing
a append; open or create text file for writing at end-of-file

r+ open text file for update (reading and writing)
w+ truncate to zero length or create text file for update
a+ append; open or create text file for update, writing at
end-of-file

[quoted from N843-draft]

Irrwahn.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top