C++, SQLite - pointer to pointer to string - c++

I work with C++ and SQLite3 (with Microsoft Visual C++ 2008) and would like to read a value from my database and store it in a variable to work with it on.
The select statement works fine, but every time I viewed the callback function and try to read the value from char **argv, I get "only" the memory address, or the first ASCII character of the value, which is in the database. What am I doing wrong?
Here is the callback function:
static int callback(void *pArg, int argc, char **argv, char **azColName)
{
fprintf(f, "Callback aufgerufen!\n");
int i;
for(i=0; i<argc; i++)
{
fprintf(f, azColName[i]);
fprintf(f, " = ");
if(argv[i]){
fprintf(f, argv[i]);
//unsigned int x = argv[i];
}
else
fprintf(f, "NULL");
fprintf(f, "\n");
}
fprintf(f, "\n");
return 0;
}
I tried it without the callback function, but again I get the same result and I've tried different ways to store the value in a variable.
while (sqlite3_step(stmt) == SQLITE_ROW)
{
fprintf(f, "%s\n", sqlite3_column_text(stmt, 0));
//const unsigned char *c = sqlite3_column_text(stmt, 0);
fprintf(f, "%u\n", sqlite3_column_int(stmt, 0));
//unsigned int z = *sqlite3_column_int(stmt, 0);
stmt_count++;
}
Is it perhaps not possible to access the value or to store it in a variable?

I don't see why you need a callback - this should work (depending on the size of the data):
char myvalue[100];
while (sqlite3_step(stmt) == SQLITE_ROW)
{
strcpy( myvalue, (const char *) sqlite3_column_text(stmt, 0) );
// do something with myvalue
stmt_count++;
}

Related

Can't load file using fopen()

I creating a program that takes a file and ecrypts it, but now i'am with a problem opening the file to read, the fopen() always return 0.
void run(){
char buffer[260] = { '\0' };
GetWindowTextA(Path,buffer,260);
encryptFile(buffer, "C:\\Users\\DownD\\Desktop\\Some.dat");
}
I think the problem is somewhere on this function run(), because when replace the buffer array with some string for example, "C:\\Somefile.exe" replacing the function encryptFile() for:
encryptFile("C:\\Somefile.exe", "C:\\Users\\DownD\\Desktop\\Some.dat");.It reads the file nice and clean.
Here it is parts of the rest of the project.
int CCrypter::encryptFile(char* filePath, LPCSTR outFile)
{
unsigned char* data = NULL;
int cypherSize;
int fSize = readFile(data, filePath);
if (!fSize)
return 2;
unsigned char *ciphertext = new unsigned char[fSize];
cypherSize = encrypt(data, fSize, ciphertext);
if (!cypherSize)
return 3;
if (!Create_File(ciphertext, cypherSize, outFile))
return 4;
return 1;
}
int CCrypter::readFile(unsigned char *&buffer, const char* path)
{
int lenght = 0;
OutputDebugString(path);
FILE* input = fopen(path, "rb");
if (!input) // Input is always 0
return 0;
fseek(input, 0, SEEK_END);
lenght = ftell(input);
buffer = new unsigned char[lenght];
printf("%d", buffer);
ZeroMemory(buffer, lenght);
rewind(input);
if (!fread(buffer, 1, lenght, input))
return 0;
fclose(input);
return lenght;
}
Just to clarify, i'm using Multi-Byte Character Set
I solved the issue. The problem was that I had opened the file before and did not close it, that was why I was receiving permission denied.

Printing array of char pointers

I am trying to read two lines from a file using array of pointers. However, I am not getting anything on screen. I have tried searching online, but could not solve the problem. Here is my code that I have written using Netbeans on mac.
int main(int argc, char** argv) {
FILE *fp;
char *points[50];
char c;
int i=0;
fp=fopen("/Users/shubhamsharma/Desktop/data.txt","r");
if(fp==NULL)
{
printf("Reached here");
fprintf(stderr," Could not open the File!");
exit(1);
}
c=getc(fp);
while(c!=EOF)
{
*points[i]=c;
c=getc(fp);
i++;
}
for(int i=0;*points[i]!='\0';i++)
{
char d=*points[i];
printf("%c",d);
if(*(points[i+1])==',')
{
i=i+1;
}
}
return (EXIT_SUCCESS);
}
char *points[50];
Is not what you want, this is an array of 50 pointers to char.
If you want an array of pointers to char[50] you need:
char (*points)[50];
points = malloc(sizeof(*points) * 2);
Also note that fgets is prefered to get a line from a file
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char (*points)[50];
points = malloc(sizeof(*points) * 2);
if (points == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
fp = fopen("/Users/shubhamsharma/Desktop/data.txt", "r");
if (fp == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
fgets(points[0], sizeof(*points), fp);
fgets(points[1], sizeof(*points), fp);
fclose(fp);
printf("%s", points[0]);
printf("%s", points[1]);
free(points);
return 0;
}

programming c threaded io

Hi what i want to do is read from a text file do something to the text and write it back out to a file. I need it to be threaded so all 3 party can run at the same time. I am trying to use a buffer to read in to and a buffer to write out from but cant work it out.
My code :
#include <stdio.h>
#include <pthread.h>
char inBuf[1000];
//char outBuf[];
void *readerFun(void *meg){
char *inputFile;
FILE *input_ptr;
inputFile = (char *) meg;
input_ptr = fopen(inputFile, "r");
if(input_ptr == NULL){
printf("%s\n", inputFile);
printf("input file not working\n");
return;
}
while(!feof(input_ptr)){
fscanf(input_ptr, "%s", &inBuf);
sleep(1);
// printf("%s\n", &inBuf );
// printf("test _____________\n" );
}
return NULL;
}
void *modifierFun(void *meg){
//char c;
//while((c = getc(inBuf) != EOF)){
//strcat(outBuf, c);
//}
//}
return NULL;
}
void *writerFun(void *meg){
char *outFile;
FILE *output_ptr;
outFile = (char *) meg;
output_ptr = fopen(outFile, "a");
while(inBuf != NULL){
printf("%s\n", inBuf );
fprintf(output_ptr, "%s", inBuf);
}
return NULL;
}
int
main (int argc, char **argv){
pthread_t reader, modifier, writer;
char *meg = argv[1];
char *meg2 = argv[2];
int ret1, ret2, ret3;
ret1 = pthread_create(&reader, NULL, readerFun, (void *) meg);
ret2 = pthread_create(&modifier, NULL, modifierFun, (void *) meg);
ret3 = pthread_create(&writer, NULL, writerFun, (void *) meg2);
pthread_join(reader, NULL);
pthread_join(modifier, NULL);
pthread_join(writer, NULL);
return 0;
}
Would be a big help is someone could point me down the right road. I have looked all over the net and cant really find what im looking for.

stack check fail in sha-1 c++

I'm having a __stack_chk_fail in the main thread.
I have no idea why is this happening?
I got the codes from this website:
http://www.packetizer.com/security/sha1/
Im trying to add a function to compute the digest of a file using the example.
.h file
#include <stdio.h>
#include <string>
std::string digestFile( char *filename );
.cpp file
std::string SHA1::digestFile( char *filename )
{
Reset();
FILE *fp = NULL;
if (!(fp = fopen(filename, "rb")))
{
printf("sha: unable to open file %s\n", filename);
return NULL;
}
char c = fgetc(fp);
while(!feof(fp))
{
Input(c);
c = fgetc(fp);
}
fclose(fp);
unsigned message_digest[5];
if (!Result(message_digest))
{ printf("sha: could not compute message digest for %s\n", filename); }
std::string hash;
for (int i = 0; i < 5; i++)
{
char buffer[8];
int count = sprintf(buffer, "%08x", message_digest[i]);
if (count != 8)
{ printf("converting unsiged to char ERROR"); }
hash.append(buffer);
}
return hash;
}
__stack_chk_fail occurs when you write to invalid address.
It turns out you do:
char buffer[8];
int count = sprintf(buffer, "%08x", message_digest[i]);
C strings are NUL-terminated. That means that when sprintf writes 8 digits, it adds 9-th char, '\0'. But buffer only has space for 8 chars, so the 9-th goes past the end of the buffer.
You need char buffer[9]. Or do it the C++ way with std::stringstream, which does not involve any fixed sizes and thus no risk of buffer overrun.

c++ sqllite3 reading blob won't work

Can someone guide me please,
I can't seem to read my blob correctly.
I don't know what's wrong, can somebody help?
this is my function:
what i'm trying to do is:
read the bob as binary and store the bytes in a char *data;
can someone please help?
int baramdb::dbreadblob(int pid)
{
sqlite3_stmt *res;
const char *tail;
int count = 0;
this->dbopen(this->dbfile);
if (sqlite3_prepare_v2(this->db, "SELECT * FROM Packet_Send_Queue", 128, &res, &tail) != SQLITE_OK)
{
printf("[Baram] Can't retrieve data: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return(1);
}
while (sqlite3_step(res) == SQLITE_ROW)
{
int *plength = 0;
*plength = sqlite3_column_bytes(res, 2);
unsigned char **pbuffer = (unsigned char **)malloc(*plength);
memcpy(*pbuffer, sqlite3_column_blob(res, 0), *plength);
count++;
}
sqlite3_close(this->db);
this->lastresult = count;
return count;
}
It seems you don't understand what "pointer" really is and how to use it.
Then, sqlite3_column_bytes returns int not int*:
int length = sqlite3_column_bytes(res, 2);
This is absolutely incorrect in current case:
unsigned char **pbuffer = (unsigned char **)malloc(*plength);
If you're using C++ - try to not explicitly use malloc/new, use smart pointer or STL containers instead:
std::vector<char> data( length );
const char *pBuffer = reinterpret_cast<const char*>( sqlite3_column_blob(res, 2) );
std::copy( pBuffer, pBuffer + data.size(), &data[0] );
This is it.