basic file code

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"
usumleAsked:
Who is Participating?
 
brettmjohnsonConnect With a Mentor Commented:
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);
      else
            /* 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 */
            fclose(inputFile);
      }
}
            
0
 
brettmjohnsonCommented:
In which programming language and which platform does this need to be done?
0
 
Sparky568223Commented:
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
Else
  MsgBox "No File exists."
End if
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Based on the authors question history, usumle probably wants a Java solution.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.