Reputation: 777
Hello there I am trying to execute the .exe file generated by the below code in Microsoft Visual Studio Ultimate 2010 and I don't see the file being created.
This code when compiled and executed in Linux with GCC works absolutely fine.
To repeat I am able to use the file created in Linux !! but in Windows the .exe program fails to create the file for the name entered by user at command prompt.
Can someone please let me know as to where I am going wrong with respect to the compilers?
Sincere Thanks
// filename.cpp : Defines the entry point for the console application.
#include "stdafx.h" //Please comment if code is to be executed in GCC
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int main()
{
FILE *pFile;
char cNotes_buffer[100];
memset(cNotes_buffer,0,100);
printf("Please enter name of the LOG file - with tag MAX 100 characters!! \n");
if( fgets (cNotes_buffer , 100 , stdin) != NULL )
{
fclose (stdin);
}
printf("name of file %s \n", cNotes_buffer);
if ( ( pFile = fopen(cNotes_buffer,"a+") ) == NULL )
{
printf("ERROR OPENING FILE FOR LOGGING \n");
exit(0);
}
return 0;
}
Upvotes: 1
Views: 395
Reputation: 28565
In all probability you will press the ENTER after entering your file name. So \n
also gets into cNotes_buffer
. Now you try to create a file with this buffer as the file name. And I don't think you can create file names with \n
in it.
Do something like if you've pressed the return key
cNotes_buffer[strlen(cNotes_buffer)-1]=0;
EDIT: Newlines can be present in filenames in linux but not so on Windows This explains why your file got created in Linux but not on Windows.
Upvotes: 3
Reputation: 13520
Try to fclose(pFile)
. Failing to close a file causes strange behaviour.
Upvotes: 0