1

I am trying to get fingerprint data (which I created and stored as a text file in another code) which I have to compare with a new fingerprint in this code. The problem is that the fingerprint API requires the fingerprint data to be passed as char pointer. I'm using the following code :

std::ifstream infile("timestamp.txt");

char* text_stream;
std::string line;

  if (infile.is_open()){
    while ( getline (infile,line)){
        if(text_stream){
            *text_stream = malloc (1 + strlen (line));
            strcpy(text_stream,line);
        }
        else{
            fprintf (stderr, "malloc failure!");
        }
    }
    infile.close();
  }

I have also tried using other codes for the same purpose but I'm getting this kind of compilation error everytime:

verifiy.cpp: In function ‘int main()’:
verifiy.cpp:29:47: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’
    *text_stream = malloc (1 + strlen (line));
                                           ^
verifiy.cpp:30:31: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘2’ to ‘char* strcpy(char*, const char*)’
    strcpy(text_stream,line);
                           ^
ayuhsya
  • 63
  • 9
  • Unless the fingerprint API specifically wants a `malloc`-allocated buffer, don't use `malloc`. Unless the fingerprint api specifically wants a persistent char buffer, just use `std::string::c_str()` to get the char pointer (and even if it does want a persistent buffer, then just use a `std::string` which you won't modify, so it won't have the buffer moved). – hyde Jun 11 '15 at 06:22

2 Answers2

2

BTW, if you are just reading the whole file, you can write something like this:

#include <string>
#include <fstream>
#include <streambuf>

std::ifstream t("timestamp.txt");
std::string str((std::istreambuf_iterator<char>(t)),
                 std::istreambuf_iterator<char>());

(If you care about the efficiency of the code above, there is a more efficient version here: Read whole ASCII file into C++ std::string)

That way, you don't need to create an explicit string buffer yourself. After that, you can just use str.c_str() to get a "const char*".

While using that, please beware that the resulting pointer can be invalid if you keep editing the string. In that case, you might need to make another copy of the string. If your function needs a "char*" instead of a "const char*", you might need to do that too. More information can be found here: How to convert string to char array in C++?

Community
  • 1
  • 1
Henry Tsai
  • 116
  • 4
1

You actually have two errors in the code, first the one you show in your question (it's the same error in two different places), the second error is worse and will lead to undefined behavior.

The first error is that std::string is not a char*, so you can't use the old C functions with it. Reading this reference should help you a little.

The second problem is that text_stream is a pointer to char, meaning *text_stream will give you what text_stream points to, i.e. a single char, or text_stream[0]. This is problematic because of two reasons: The first because text_stream is uninitialized, its contents is indeterminate and dereferencing will not give you a valid pointer leading to undefined behavior. The second problem with that is that *text_stream is not a pointer, assigning to *text_stream will not change text_stream so text_stream will still be uninitialized while you overwrite some random memory. If the compiler doesn't shout warnings at you for this assignment, then you need to enable more warnings. Warnings from the compiler are just as important as compiler errors, and they often indicate places you do something which is not technically wrong but will lead to other problems when running the program.

The solution to both these problems is to stop using C function in C++. Use std::string exclusively, and when you need to call a function which needs a const char * argument, just use std::string::c_str to get such a pointer.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550