file-question. easy.

Posted on 2005-04-28
Last Modified: 2010-08-05

I made this code:
#include <iostream>
#include <windows.h>
using namespace std;
int main(){
Fp = fopen("input.dat","r");
if (Fp == NULL) {
  fprintf(stderr, "Can't open input file!\n");

I have input.dat (version 1) located at:
"C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\inlezenvanbestand\\input.dat"

and my program is here:
"C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\inlezenvanbestand\\release\\aa.exe"

I have allso a input.dat (version 2) located here:
"C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\inlezenvanbestand\\release\\input.dat"
version 1 of input.dat is different from version 2.

The strange thing is:
when I run my program it opens version 1 of input.dat  .
I want it to open the version in the directory of the exe (version 2).

so, my questions:
(1)-> How should I adapt the code so that it allways opens the file that is placed in the directory of the program itself (and,  I don't want to have the file-pad "hardcoded" in the exe)
(2)-> How comes it opens the version 1 file ?? I don't think this is very logic , since it is placed one directory up !

Any advice is welcome...

Greetz, André.
Question by:André123
    LVL 45

    Accepted Solution

    The program looks in for the file in its "current working directory". If you change your directory programatically, your code would look for files in the new directory and not in the one where it was created/launched. There apparently is no other reason why version 1 is opened instead of version 2
    LVL 5

    Assisted Solution

    Try using commandline parameters option.. here below .. argc will contain the total number of parameters specified at command line and *argv[] will contain the list of arguments.

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

    if (argc!=1)
     Fp = fopen(argv[1],"r");
    Fp = fopen("input.dat","r");

    if (Fp == NULL) {
      fprintf(stderr, "Can't open input file!\n");

    you could then run the command as...

    aa.exe ./input.dat


    Author Comment

    thanks both.

    Tried it in some other folders and it opens the correct version if I don't run it from MSVC (so, from a cmd-promt).

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

    733 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

    18 Experts available now in Live!

    Get 1:1 Help Now