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

Discussion in 'C Programming' started by Blankdraw, Aug 16, 2003.

  1. Blankdraw

    Blankdraw Guest

    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;
    }
    Blankdraw, Aug 16, 2003
    #1
    1. Advertising

  2. Blankdraw

    Malcolm Guest

    "Blankdraw" <> 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;
    > }
    >
    >
    Malcolm, Aug 16, 2003
    #2
    1. Advertising

  3. Blankdraw

    MG Guest


    > 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
    MG, Aug 17, 2003
    #3
  4. "MG" <> wrote in
    <smH%a.48$>:
    >
    >> 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.
    Irrwahn Grausewitz, Aug 17, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TC
    Replies:
    2
    Views:
    370
  2. Ben Fairbank
    Replies:
    2
    Views:
    5,402
  3. john san
    Replies:
    19
    Views:
    728
    Diez B. Roggisch
    Feb 18, 2005
  4. tomhr
    Replies:
    27
    Views:
    1,400
    Mike Wahler
    Jan 12, 2006
  5. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    265
    Michele Dondi
    Jan 22, 2004
Loading...

Share This Page