0

I am trying to write into a file using process 0 only but the file is not even created in the directory, if I run it without mpich it runs fine (and the file is created in the the project directory as it should be), but not when I run with mpich.

I don't need parallel file io but I do need to parallel some other code and I do need to use files , so I am using the process 0 for this.

this is a test code for this:

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

  MPI_Init(&argc, &argv);
  int  numOfProcs, myid, j;
  MPI_Comm_rank(MPI_COMM_WORLD, &myid);
  MPI_Comm_size(MPI_COMM_WORLD, &numOfProcs);
  MPI_Status status;

  if (myid==0)
  {
      FILE* f = fopen("wee.txt","w");

      printf("address = %p",f);

      fprintf(f, "ee");
      fclose(f);
      printf("file creation done\n");
  }
  MPI_Finalize();

}//END of main

The file does open and returns an address, I searched maybe it creates it in a different directory but couldn't find it.

exmple for an adress returned by the code: 00007FFB40537500

And I do get the msg: "file creation done"

The file does not exist int he folder though

edit: I am running on admin account and the project is located in system disk (not in program files , just c and project folders)

user2893825
  • 95
  • 2
  • 9
  • So you are entering the if's body, right? Check for null pointer in `f`. The file should be created in the directory that the code lies too, if everything works fine. – gsamaras Mar 31 '16 at 09:59
  • I added the code: printf("address = %p",f); and got the address back:00007FFB40537500 – user2893825 Mar 31 '16 at 10:01
  • I made my comment as I was seeing your code. – gsamaras Mar 31 '16 at 10:01
  • `void main` --> `int main`, check the result of `fopen` instead of printing the address of `f` – David Ranieri Mar 31 '16 at 10:09
  • could you elaborate? how can I get the result of int main? , got the file to open though, I provided the full path where I want it to be created and it worked just fine....for some reason mpich wouldn;t do it itself.. – user2893825 Mar 31 '16 at 10:19
  • To get the current directory, you can add `system("pwd");` just before file creation. Or something approching, depending on your system... – Mathieu Mar 31 '16 at 10:29

1 Answers1

0

You will get that message independently of whether the file gets created or not. Do this:

  if (myid==0) {
      FILE* f = fopen("wee.txt","w");
      if(f == NULL) {
        printf("File did NOT open\n");
        return -1;
      }
      fprintf(f, "ee");
      fclose(f);
      printf("file creation done\n");
  }

Please read this too: What should main() return in C and C++?

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I can;t seem to get the returned value, I am using windows so I tried without the '.' in "$ ./a.out" (specified in the link u provided) but it doesn;t work, I am using visual studio 2013 with c++ compiler although I write in c. Is there away to read this value from 1 of the vs stored files? – user2893825 Mar 31 '16 at 11:21
  • Did you try my code? @user2893825 What does it print? – gsamaras Mar 31 '16 at 13:27