convert a char* to string

Hello,

first of all, I apologize for speaking so bad english, i am french and I try to improve english and C++ so... ;-)

Then, I used the getline function to read a file.
I would like to sort "line" (which are char*) by alphabetical order.

Is it possible to convert char* to string? I found some functions like c_str but it doesn't work. Could you help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

void recupEntrees(){             //read the file and take lines
  FILE * fp;
  char * line = NULL;
  size_t len = 0;
  ssize_t read;
  index Id;                     //this is a BST

  fp = fopen("./test.txt", "r");
  if (fp == NULL){
    exit(EXIT_FAILURE);
  }else{
  
  while ((read = getline(&line, &len, fp)) != -1) {
      ajouter(Id, line);     //add line in a BST
  }
  }
  //if (line) delete(line);
  fclose(fp);
}


void ajouter(index &ind, char* mot){

//convert char* mot to string


  if (index==0){
  index aux;
  aux=new boite;

  }

Thanks a lot
 





std::string has a constructor that takes a const char*

1
2
3
4
5
6
7
8
9
10
11
#include<string>
#include<iostream>

int main()
{
const char* charString= "Eggs on toast.";
 std::string someString(charString);

 std::cout << someString;
 return 0;
}

You can also just cast a char* to a string:
1
2
char *cStr = "C++";
std::string Str = std::string(cStr);
Thank you very much...
by the way: there is a standard getline function which reads directly into a std::string. So you don't need your self-made getline (which, I bet, is leaking memory ;) )
Topic archived. No new replies allowed.