Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String-to-textfile programs will not run in DOS window...

Reply
Thread Tools

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

 
 
Blankdraw
Guest
Posts: n/a
 
      08-16-2003
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;
}
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      08-16-2003

"Blankdraw" <(E-Mail Removed)> wrote in message
>
> /* 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?
>
>


> printf("Error: Cannot open input file\n");
> exit(0);
> }
>
> while ((ch=getchar()) != '#')
>


> putc(ch, fileptr);
> }
>
> fclose(fileptr);
> return 0;
> }
>
>




 
Reply With Quote
 
 
 
 
MG
Guest
Posts: n/a
 
      08-17-2003

> 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


 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      08-17-2003
"MG" <(E-Mail Removed)> wrote in
<smH%a.48$(E-Mail Removed)>:
>
>> 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..


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.

--
Proofread carefully to see if you any words out.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IOS DoS defense causes DoS to itself:) Igor MamuziŠ Cisco 2 05-19-2006 11:59 PM
Outsmarting DOS C compiler to print to USB printer -- use DOS interrupt? tomhr C Programming 27 01-12-2006 04:09 PM
[newbie]How to install python under DOS and is there any Wxpython can be installed under dos? john san Python 19 02-18-2005 12:05 PM
Unable to run MS-DOS in Windows and DOS properties tabs missing Don Computer Support 5 02-11-2004 07:20 PM
Executing DOS (yes, DOS) program from within Python? Ben Fairbank Python 2 10-07-2003 08:51 AM



Advertisments