Click here to Skip to main content
15,894,740 members
Home / Discussions / C#
   

C#

 
GeneralRe: why cant I run powershell script from current directory Pin
trønderen8-Feb-24 6:21
trønderen8-Feb-24 6:21 
GeneralRe: why cant I run powershell script from current directory Pin
jschell7-Feb-24 5:31
jschell7-Feb-24 5:31 
QuestionShould 0.5 round up or down? Pin
Gerry Schmitz26-Jan-24 8:33
mveGerry Schmitz26-Jan-24 8:33 
AnswerRe: Should 0.5 round up or down? Pin
trønderen26-Jan-24 9:14
trønderen26-Jan-24 9:14 
AnswerRe: Should 0.5 round up or down? Pin
Dave Kreskowiak26-Jan-24 10:59
mveDave Kreskowiak26-Jan-24 10:59 
AnswerRe: Should 0.5 round up or down? Pin
Kenneth Haugland27-Jan-24 2:30
mvaKenneth Haugland27-Jan-24 2:30 
AnswerRe: Should 0.5 round up or down? Pin
jschell29-Jan-24 4:41
jschell29-Jan-24 4:41 
GeneralRe: Should 0.5 round up or down? Pin
Gerry Schmitz29-Jan-24 8:51
mveGerry Schmitz29-Jan-24 8:51 
GeneralRe: Should 0.5 round up or down? Pin
jschell30-Jan-24 4:49
jschell30-Jan-24 4:49 
GeneralRe: Should 0.5 round up or down? Pin
Gerry Schmitz30-Jan-24 8:03
mveGerry Schmitz30-Jan-24 8:03 
AnswerRe: Should 0.5 round up or down? Pin
Richard MacCutchan30-Jan-24 5:23
mveRichard MacCutchan30-Jan-24 5:23 
GeneralRe: Should 0.5 round up or down? Pin
Gerry Schmitz30-Jan-24 8:09
mveGerry Schmitz30-Jan-24 8:09 
GeneralRe: Should 0.5 round up or down? Pin
Richard MacCutchan30-Jan-24 8:20
mveRichard MacCutchan30-Jan-24 8:20 
GeneralRe: Should 0.5 round up or down? Pin
jschell31-Jan-24 4:51
jschell31-Jan-24 4:51 
GeneralRe: Should 0.5 round up or down? Pin
Richard MacCutchan31-Jan-24 6:43
mveRichard MacCutchan31-Jan-24 6:43 
GeneralRe: Should 0.5 round up or down? Pin
trønderen31-Jan-24 10:43
trønderen31-Jan-24 10:43 
GeneralRe: Should 0.5 round up or down? Pin
Richard Deeming31-Jan-24 22:35
mveRichard Deeming31-Jan-24 22:35 
GeneralRe: Should 0.5 round up or down? Pin
jschell1-Feb-24 6:00
jschell1-Feb-24 6:00 
GeneralRe: Should 0.5 round up or down? Pin
Richard MacCutchan31-Jan-24 22:42
mveRichard MacCutchan31-Jan-24 22:42 
GeneralRe: Should 0.5 round up or down? Pin
jschell1-Feb-24 5:42
jschell1-Feb-24 5:42 
GeneralRe: Should 0.5 round up or down? Pin
Richard MacCutchan1-Feb-24 5:55
mveRichard MacCutchan1-Feb-24 5:55 
QuestionQuestion about (char* from string) ,(string from char*) and (char* to char) Pin
Jens Eckervogt19-Jan-24 8:24
Jens Eckervogt19-Jan-24 8:24 
Hello everyone,

I have problem with char* from string (CharPointerFromString):
static int LengthSize(string str)
{
    if (str == null)
    {
        return 0;
    }
    return (str.Length * 4) + 3;
}

static char* CharPointerFromString(string str)
{
    if (str == null)
    {
        return null;
    }

    char[] charArr = new char[str.Length];
    for (int i = 0; i < str.Length; i++)
    {
        charArr[i] = str.ToCharArray()[i];
    }

    fixed (char* charPtr = charArr)
    {
        return charPtr;
    }
}

static int Main(string[] args)
{
    printf(CharPointerFromString($"Hello {"DeafMan1983"}!\n"));
}

Result:
Hello DeafMan1983!

And I add string from char* (StringFromCharPointer)
static string StringFromCharPointer(char* s, bool freePtr = false)
{
    if (s == null)
    {
        return string.Empty;
    }

    char* ptr = s;
    while (*ptr != 0)
    {
        ptr++;
    }

    string result = Encoding.UTF8.GetString((byte*)s, (int) (ptr - s));

    if (freePtr)
    {
        NativeMemory.Free(s);
    }
    return result;
}

But I have problem with extension like text".txt" -> if I don't have text.txt than I write simple with FILE and fopen(), ferror() and fclose();
string text_txt = Path.GetFileName("text.txt");
char* path = CharPointerFromString(text_txt);
FILE* file = fopen(path, CharPointerFromString("r"));
if (file == null)
{
    printf(CharPointerFromString($"Error: {StringFromCharPointer(path)} not found.\n"));
    return ferror(file);
}

printf(CharPointerFromString($"Success: {StringFromCharPointer(path)} found.\n"));
fclose(file);

Result: It seems forgetting extension?
Success: text found.

How do I fix it?
AnswerRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Richard Andrew x6419-Jan-24 12:03
professionalRichard Andrew x6419-Jan-24 12:03 
AnswerRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Dave Kreskowiak19-Jan-24 12:35
mveDave Kreskowiak19-Jan-24 12:35 
GeneralRe: Question about (char* from string) ,(string from char*) and (char* to char) Pin
Jens Eckervogt19-Jan-24 15:15
Jens Eckervogt19-Jan-24 15:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.