basic file code

Posted on 2005-04-26
Last Modified: 2010-04-17
I need to declare a file "aFile" and attach it to "theData.txt"

and verify that "aFile" exists and print out "No FIle" if it doesn't

and then open "aFile" for input as "InputFile" (without try block)

and then Close "aFile"
Question by:usumle
    LVL 23

    Expert Comment

    In which programming language and which platform does this need to be done?
    LVL 3

    Expert Comment

    For VB would be something like this

    Dim aFile as string

    aFile = "theData.txt"

    If Dir(aFile) <> "" then
      Open aFile for Output as #1

      ' Do whatever
      Close #1
      MsgBox "No File exists."
    End if
    LVL 23

    Accepted Solution

    in C:

    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    char * aFile = "theData.txt";            /* this should really be a full pathname */
    struct stat sb;

    if (stat(aFile, &sb) < 0) {
          if  ((errno == ENOTDIR) || (errno == ENOENT))
                /* Exactly what you asked for ...*/
                fputs("No File", stderr);
                /* however I would report other errors as well */
                perror("Unable to test file exists");
    } else {
          FILE * inputFile = fopen(aFile, "r+");
          if (inputFile == null)
                perror("Unable to open file");
          else {
                /* read from file */
                /* close file */
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Based on the authors question history, usumle probably wants a Java solution.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    This is about my first experience with programming Arduino.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now