Xcode does not support printf_s ? - xcode

Not sure why Xcode complains "Use of undeclared identifier 'printf_s' did you mean 'printf'? Try to find help from Xcode manual but not useful.
#include <iostream>
#include <stdio.h>
int main(int argc, const char * argv[]) {
printf_s("Line one\n\t\tLine two\n");
return 0;
}

Related

Open /dev/rdisk0 gives “Operation not permitted” error despite using sudo

On Mac OSX Mojave 10.14.6, the following simple code does not work anymore :
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
int main(int argc, char** argv)
{
int fd = open ("/dev/rdisk0", O_RDONLY);
if (fd == -1)
{
fprintf(stdout, "open(%s) error = %s\n", "/dev/rdisk0", strerror(errno));
fflush(stdout);
return 1;
}
return 0;
}
It gives :
open(/dev/rdisk0) error = Operation not permitted
This happens even when running the executable using sudo.
This code used to work under 10.13 and earlier versions.
Thinking this might be due to SIP, I gave the Terminal and the executable Full Disk Access but it didn't help.
Is there another way to get around this issue? How do I open /dev/rdisk0 now ?
Thanks in advance

getting product name from global unique identifier

I'm trying to get the list of all installed softwares, and i want to do it using the Windows APIs only.
This is my code..
#include "stdafx.h"
#pragma comment(lib,"msi.lib")
#include <Windows.h>
#include<iostream>
#include "Msi.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR *liProBuf = new TCHAR[39];
UINT liVal = MsiEnumProducts(0,liProBuf);
cout << liProBuf;
getchar();
return 0;
}
The above code uses the function MsiEnumProducts which returns the Global Unique Identifier(GUID) ,which is the unique identifier for the product.But after this i don't know how to to get the name of the product using this GUID.
How can this be done.

"struct has no member named" error with gcc on dev machine

#include <stdio.h>
#include <stdlib.h>
#include "ReadMethods.h"
int main(int argc,char * argv[])
{
DPDA WordChecker;
DPDA * WordCheckerPointer=&WordChecker;
WordChecker.DPDAFilename=(char*)malloc(25*sizeof(char));
WordChecker.DPDAInputFilename=(char*)malloc(25*sizeof(char));
WordChecker.DPDAOutputFilename=(char*)malloc(25*sizeof(char));
strcpy( WordChecker.DPDAFilename,argv[1]);
strcpy( WordChecker.DPDAInputFilename,argv[2]);
strcpy( WordChecker.DPDAOutputFilename,argv[3]);
readDPDA(argv[1],WordCheckerPointer);
readInputLines(argv[2],WordCheckerPointer,argv[3]);
return 0;
}
This is my code that gives error from mallocs until last strcpy() ,total 6 lines.The error is "DPDA has no member named DPDAFilename" and same for other fields for every malloc and strcpy linesthat i work on.Here is the part of header file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct tagRule{
char *startingState;
char symbolToPop;
char expectedInput;
char *endingState;
char symbolToPush;
}Rule;
typedef struct tagStackDPDA{
char * arrayOfSymbols;
int stackElementCount;
char * currentState;
}stackDPDA;
typedef struct tagDPDA{
char * alphabet;
char * stackSymbols;
char ** states;
char *startingState;
char **finalStates;
int finalStatesAmount;
Rule * ruleList;
stackDPDA stackOfDPDA;
int sizeArray[4];//This array holds amount values of states,alphabet symbols,stack symbols and transition rules
char *DPDAFilename;
char *DPDAInputFilename;
char *DPDAOutputFilename;
}DPDA;
The code works fine in codeblocks environment but in gcc (-Wall -ansi).Those filenames come from input text files yet i am not sure it can cause this error.
Edit:By the way I am using this command line to compile;
gcc -Wall -ansi main.c ReadMethods.h -o WordChecker
May be if you compile in C mode, you have to use C-style comments in header?
/**/ instead of //

Xcode, Instruments, Allocations, Command Line App, not registering free()

I have a very simple Command Line Tool in Xcode:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(int argc, const char * argv[])
{
void *p = calloc(32, 1);
assert(p);
free(p);
return 0;
}
When I run Instruments->Allocations it shows one living block. The free seems to be ignored.
In the olden days, I remember that you could actually still use the last free'ed block. So I tried this:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(int argc, const char * argv[])
{
void *p = calloc(32, 1);
assert(p);
free(p);
void *q = calloc(32, 1);
assert(q);
free(q);
return 0;
}
Now, Instruments->Allocations shows no living blocks. This seems correct.
Can anyone explain or reproduce the problem I am seeing in the first program?
I'm using Xcode 4.1.1
Thanks.
Let me rephrase the comments above.
Apple LLVM in Xcode 5 resolved the alloc / free behavior so that no blocks allocated now, thus the free() method runs as expected.

Problems with eval function skipping newlines in $(eval echo $myvar)

I am currently working on a script meant to add a new project generation for any basic editor.
I am using the following structure in order to generate the right basic program (hello, world) according to the language selected by the user :
#!/bin/sh
#this is a short example in the case the user selected C as the language
TXTMAIN="\$TXTMAIN_C"
$TXTMAIN_C="#include <stdlib.h>
#include <stdio.h>
int main(int argc, char const* argv[])
{
printf(\"hello, world\n\");
return EXIT_SUCCESS;
}"
MAIN="./main.c"
touch MAIN
echo -n "$(eval echo $TXTMAIN)" >> "$MAIN"
gedit MAIN
This piece of code gives the following output when you edit main.c :
#include <stdlib.h> #include <stdio.h> int main(int argc, char const* argv[]) { printf("hello, world\n"); return EXIT_SUCCESS; }
However, when replacing line 13 by echo -n "$TXTMAIN_C" >> "$MAIN", it gives the right output :
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char const* argv[])
{
printf("hello, world\n");
return EXIT_SUCCESS;
}
I still don't know wether that's an echo or eval issue, or if there is a way around for my pointer-like problem.
Any advice is very welcome !
There are a few errors in your script, and it's more complicated than it should be.
If you want to use an indirect variable like that, use the ${!FOO} syntax, and put quotes where appropriate:
#!/bin/sh
#this is a short example in the case the user selected C as the language
TXTMAIN=TXTMAIN_C # don't force a $ here
TXTMAIN_C="#include <stdlib.h>
#include <stdio.h>
int main(int argc, char const* argv[])
{
printf(\"hello, world\n\");
return EXIT_SUCCESS;
}"
MAIN="./main.c"
echo "${!TXTMAIN}" > "$MAIN" # overwrite here, if you want to
# append, use >>. `touch` is useless

Resources