basic file code

Posted on 2005-04-26
Medium Priority
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
  • 2
LVL 23

Expert Comment

ID: 13872843
In which programming language and which platform does this need to be done?

Expert Comment

ID: 13876348
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

brettmjohnson earned 200 total points
ID: 13876831
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 86

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

840 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