Home > Software engineering >  How can i pointer char variable add to list char variable in C Programming
How can i pointer char variable add to list char variable in C Programming

Time:11-26

i stucked, need to add pointer char value to list char value. pointer char value includes username (windows machine) and list char value includes startup path. I tried to strcat but it breaks the first ";" part.

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


int main(void)
{
    char *username = getenv("USERNAME");
    //printf("%s\n", username);  

    char shortcutpath[100] = "C:\\Users\\";"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup";  //2 ; is weird, i just tried

    strcat(shortcutpath, username);
    printf("%s\n",shortcutpath);
    return 0;
}

Output = C:\Users%username% ,needs to continue

Need to assign a variable "C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup" this path

Is there any easy way to do that? Or something other usefull things?

CodePudding user response:

You probably want this:

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(void)
{
  char* username = getenv("USERNAME");
  char shortcutpath[100] = "C:\\Users\\"; 
  strcat(shortcutpath, username);
  strcat(shortcutpath, "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup");

  printf("%s\n", shortcutpath);
  return 0;
}

But the more correct way would probably be to get directly the APPDATA environnement variable:

int main(void)
  {
    char shortcutpath[100];
    char* appdata = getenv("APPDATA");  // C:\Users\<yourusername>\AppData\Roaming
    strcpy(shortcutpath, appdata);
    strcat(shortcutpath, "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup");
    ...

CodePudding user response:

char shortcutpath[255] = "C:\\Users\\";
strcat(shortcutpath, username);
strcat(shortcutpath, "\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup");
  •  Tags:  
  • c
  • Related