Hi, can anybody help me? i am relatively new to C Programming. My question is : How do you create a program which will take a filename as an argument, check whether the file is present and open it (if the file is not present create that file). Thanks

-Kartik

Recommended Answers

All 6 Replies

Smells like homework so post your best effort and we'll help ya fix it.

My question is : How do you create a program which will take a filename as an argument, check whether the file is present and open it (if the file is not present create that file).

Use the form:

int main(int argc, char *argv[])

Check for argc being greater that 1. Try to fopen argv[1]. If it doesn't open, ...

Ok, this is not the biggest dificulty in the world:

#include <stdio.h>
#include <conio.h>
#include <dir.h>
void main(int argc, char **argv)
{
   FILE *in;
   if (searchpath(argv[1]))
          { 
           printf("File exists");
           in=fopen(argv[1],"r");
           ...
          }
   else {
           printf("File does not exist\nCreate?y/n") ;
           char c=getch();
           if (c=='n'||c=='N') return;
           else 
                { 
                 in=fopen(argv[1],"r");
                 ...
                }
         } 
}

The point is, you must run it from the DOS shell and type:
exist.exe (exist is the name of this program) file.dat

Hope it helps :)

>this is not the biggest dificulty in the world
Judging from your code, apparently it is.

>Hope it helps
No, as a matter of fact, it doesn't. Your code is non-portable, and very poorly written. If giving the OP a freebie doesn't get him expelled, you'll have taught him bad habits. So no, you've hurt more than helped. Shall we go over the bad parts of your code?

>#include <conio.h>
Not portable, this will only work on Windows compilers that support Borland's conio.h header. Most of the time, conio.h is unnecessary and ignorant people include it so that they can use getch() for no good reason.

>#include <dir.h>
Also not portable. I applaud your desire to check if the file exists properly, but that doesn't mean your solution is what was desired, and it certainly doesn't give you free rights to post any platform or compiler specific code that pops into your head.

>void main(int argc, char **argv)
void main is wrong. It always has been, it always will be. I can't stress enough that if you use void main, you will be tagged as a retard, and nobody who knows anything will trust you.

>if (searchpath(argv[1]))
searchpath() is not portable. In fact, it's not even well known, so this particular solution is very poor on a forum that stresses portable solutions. Even worse, argv[1] is not guaranteed to be meaningful. It may be a null pointer, and I'd be willing to bet that searchpath() will choke on a null argument.

>char c=getch();
There's no need for getch() here. getchar() will work just fine.

>in=fopen(argv[1],"r");
Who said the file was for reading? I'm more likely to assume that it's for writing since it is being created if it doesn't already exist. In that case the solution can be written portably, and easily:

#include <stdio.h>
#include <stdlib.h>

int main ( int argc, char *argv[] )
{
  FILE *in;

  if ( argc < 2 ) {
    fprintf ( stderr, "usage: %s <filename>\n", __FILE__ );
    return EXIT_FAILURE;
  }

  in = fopen ( argv[1], "w" );

  if ( in == NULL ) {
    fprintf ( stderr, "Failed to open file\n" );
    return EXIT_SUCCESS;
  }

  /* ... */

  return EXIT_SUCCESS;
}

Since the "w" and "a" modes create the file if it doesn't exist, there's really no point in explicitly searching for the file before trying to open it. If the OP needs such functionality, the file can be opened for reading, where if it doesn't exist, the call to fopen will fail and return a null pointer. It's usually safe to assume that failure to open a file for reading means that the file is not located at that path, and thus does not exist.

There. Not only is my program completely correct and portable, it's also shorter and easier to understand than yours.

Since WHEN is void main() wrong exactly??
A few thing to say:
1. a program is better than saying-search google
2. it was written in Borland C++ 3.1
3. Oh, I'm sorry, I should have made it work on every system in this world... How bad of me... :(
4. I will consider your advice, as I am sure it comes from a great expert, thank you. :mrgreen:

>Since WHEN is void main() wrong exactly??
Hmm, how about always? main has always returned int. Now, it may be a supported definition for your compiler, but that doesn't make it any less wrong, it only means that your compiler vendor caters to the stupid.

>1. a program is better than saying-search google
I beg to differ. By posting the entire program, you're only teaching the OP to rely on other people to do his work for him.

>2. it was written in Borland C++ 3.1
I bet you use Windows XP too. Get a newer compiler, already! But that's not really an excuse to write sloppy code. Borland C++ 3.1 can handle the program I wrote.

>3. Oh, I'm sorry, I should have made it work on every system in this world...
Yes, you should have. Yes, it's possible. Your sarcasm is not lost on me, but I do think you're stupid for eschewing portability in favor of a Borland specific solution just because you're too lazy to write portable code or ask the OP for system specifics so that you can give him a program tailored to his system, and not to yours.

>4. I will consider your advice
Please do. In the end I only want to help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.