0

I'm trying to create a log file for each program execution.

char * createLogFile(char *filename) {
  char path[100] = "logs_folder/";
  char text[100] = "";
  strcpy(text, filename);
  strcat(path, text);
  strcat(path, ".txt");
  FILE *logFile;
  logFile = fopen(path, "w");
  fclose(logFile);
  return text;
}

The problemm comes when I'm debugging this code piece, logFile is always null. And the program crashesh when it reaches fclose()

error image

it takes me to this line of invalid_parameter.cpp:

if (IsProcessorFeaturePresent(PF_FASTFAIL_AVAILABLE))
{
 __fastfail(FAST_FAIL_INVALID_ARG);
}

The path is correct, why isn't the file creating? Why does it crash?

  • @SergeBallesta The if nullcheck was a debug code that I pasted :(. The whole error is without that check and return – I've just edited the post, sorry. –  May 20 '16 at 08:27
  • Then the answer was in the debugging code! So my comment is: please test whether logFile is null, and if it is, please show the output of perror or strerror. – Serge Ballesta May 20 '16 at 08:31
  • logFile is indeed null. –  May 20 '16 at 08:32
  • @SergeBallesta logs_folder/log_2016-05-20-(10-37).txt: no such a file or directory. –  May 20 '16 at 08:38
  • 2
    Does the directory `logs_folder` exists in current directory? If it does not, it is not created and you get this error – Serge Ballesta May 20 '16 at 08:46
  • @Patxiku In your case [`fopen`](http://www.cplusplus.com/reference/cstdio/fopen/) returns NULL because the file cannot be opened for some reason. So check if `fopen` return NULL and if yes, inspect [`errno`](http://www.cplusplus.com/reference/cerrno/errno/) take some appropriate action and d'ont call `fclose`. – Jabberwocky May 20 '16 at 08:51
  • The point it that it should create that file. Isn't that how `fopen(path,"w")` work? If it exists open to write, else create the file to write. –  May 20 '16 at 08:52
  • Hmm, you should try to show what is the current directory at the moment you call fopen. Alternatively what OS and file system are you using? But I would be surprised by an underlying file system issue because even Windows accepts it... – Serge Ballesta May 20 '16 at 08:53
  • Windows 10 x64. NTFS –  May 20 '16 at 08:54
  • @Patxiku you can also step into `fopen`with the debugger, this may help you to narrow down the problem. – Jabberwocky May 20 '16 at 08:54
  • @Patxiku the current directory may not be what you expect. Use [`GetCurrentDirectory`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364934(v=vs.85).aspx) to check. – Jabberwocky May 20 '16 at 08:55
  • @Patxiku the crash occurs because you call `fclose` with a NULL parameter. That crash is expected. The actual question is **Why is fopen unable to open my file**. – Jabberwocky May 20 '16 at 08:57
  • I'm working on it. –  May 20 '16 at 09:07
  • @MichaelWalz can't get `GetCurrentDirectoy`working. –  May 20 '16 at 09:17
  • 1
    For test purposes, temporarily put in a complete, absolute path. The 'current directory' is an antiquated annoyance. – Martin James May 20 '16 at 09:19
  • You mean, "C:\path\path\path\path\log_files\lalalal.txt" or just "log_files\lalala.txt" ? –  May 20 '16 at 09:20
  • @Patxiku _can't get `GetCurrentDirectoy` working_: be more explicit. – Jabberwocky May 20 '16 at 09:25
  • it saves me numbers, not path: `TCHAR Buffer[BUFSIZE]; DWORD dwRet; dwRet = GetCurrentDirectory(BUFSIZE, Buffer);` –  May 20 '16 at 09:25
  • Ok, I might find the path error. using VIsual studio debug tools doesn't launch the compiled exe in the same directory as other ones. –  May 20 '16 at 09:31

2 Answers2

1

The code sequence

strcpy(text, filename);
strcat(path, text);
strcat(path, ".txt");

is simply dangerous, since you append to a stack based buffer (path) unverified input string (filename)... Possibly the result is a stack overwrite which destroys your logFile variable...

If you stick to MS paltform, use strcat_s and strcpy_s(https://msdn.microsoft.com/en-us/library/d45bbxx4.aspx, https://msdn.microsoft.com/en-us/library/td1esda9.aspx) or at least do some length checking.

Also, you return the address of a local stack based variable which gets destroyed after the function exits.

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
  • The `if null`check was a debug code that I pasted :(. The whole error is without that check and `return` –  May 20 '16 at 08:27
1

your program should not go to fclose if it is always null.

Are you sure that the string text is long enough? Why don't you use a strlen() of path and filename to get the minimum char you need?

int len = strlen(path) + strlen(filename) + 5; //5 == ".txt" & '\0'
char text[len] = "";

And are you sure the path you set is right?

zondo
  • 19,901
  • 8
  • 44
  • 83
Rizz
  • 73
  • 7