Hello ,
I'm new here and new to c/c++ programming.
Can you guide me how to extract user from path like : /home/user/public_html

Please help me , thanks a lot

Dani AI

Generated

Thread asked for extracting the "user" component from a path like /home/user/public_html. pointed at tokenizing and reminded the OP to show effort. Below are corrected, practical examples that extract the second path component (the username), plus notes on common pitfalls.

A minimal, non-destructive C approach that skips repeated slashes and handles absolute or relative paths:

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

int main(void) {
    const char *path = "/home/user/public_html";
    const char *p = path;

    while (*p == '/') ++p;             /* skip any leading slashes */
    while (*p && *p != '/') ++p;       /* skip first component */
    while (*p == '/') ++p;             /* skip separators before second */
    const char *start = p;
    while (*p && *p != '/') ++p;
    size_t len = p - start;
    if (len == 0) { printf("(no second component)\n"); return 0; }
    char *user = malloc(len + 1);
    if (!user) return 1;
    memcpy(user, start, len);
    user[len] = '\0';
    printf("%s\n", user);
    free(user);
    return 0;
}

A compact pre-C++17 std::string solution that is robust against repeated slashes:

#include <string>
#include <iostream>

int main() {
    std::string path = "/home/user/public_html";
    std::size_t pos = path.find_first_not_of('/', 0);        // start of first component
    if (pos == std::string::npos) { std::cout << "(no components)\n"; return 0; }
    std::size_t a = path.find('/', pos);                    // end of first component
    if (a == std::string::npos) { std::cout << "(no second component)\n"; return 0; }
    std::size_t start = path.find_first_not_of('/', a);     // start of second component
    if (start == std::string::npos) { std::cout << "(no second component)\n"; return 0; }
    std::size_t b = path.find('/', start);
    std::size_t len = (b == std::string::npos) ? path.size() - start : b - start;
    std::cout << path.substr(start, len) << '\n';
}

For modern C++ prefer std::filesystem::path (C++17+) which understands root names, separators and platform details — iterate its elements and pick the desired non-root component. See std::filesystem::path. Avoid strtok() unless you intentionally want to modify the input buffer; it changes the string and can complicate multi-threaded code. Also watch for Windows backslashes and drive letters if your code must be portable — either normalize separators first or use std::filesystem for platform-aware parsing.

Recommended Answers

All 5 Replies

It really depends on your extraction criteria but I would start investigating functions like strtok().

Thanks For your replay
but i do not know any thing in c
can you write the code please.

Thanks For your replay
but i do not know any thing in c
can you write the code please.

No, absolutely not.

anybody can help me please

okay before we give more help what have you researched so far did you try to google search about the topic?

but i do not know any thing in c
can you write the code please.

Sorry but no ones gonna serve you code here without doing your part
forum rule:
Keep It Organized:
Do provide evidence of having done some work yourself if posting questions from school or work assignments

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.