printf("%s",MemBlock->Data[0]);So Playing Around with structures :
Code:
#include <stdio.h>
#include <stdlib.h>
struct MemoryBlock
{
char *Data;
size_t Size; //size_t is a unsigned integer
};
int LoadFromFile(char *FileName,void *GenericPointer)
{
FILE *Source;
size_t Size = 0;
Source = fopen(FileName,"r");
struct MemoryBlock *MemBlock;
MemBlock = GenericPointer;
if (Source!=NULL)
{
char ch;
while((ch = fgetc(Source))!=EOF)
{
Size++;
MemBlock->Data=MemBlock->Data+ch; //Fills my structure with data
};
MemBlock->Size=Size;
}
return 1; //This line will only be reached if the file could not be found.
}
int PrintMemBlock(void *GenericPointer)
{
struct MemoryBlock *MemBlock;
MemBlock=GenericPointer;
printf("%s\n","Memory Block:");
printf("\nSize :%i\n",MemBlock->Size);
printf("%s",MemBlock->Data[0]);
return 0;
}
int main()
{
struct MemoryBlock MemBlock;
MemBlock.Size = 3;
MemBlock.Data="a";
LoadFromFile("BlobFile.txt",&MemBlock);
PrintMemBlock(&MemBlock);
return 0;
}
This is bugging me - printf("%s",MemBlock->Data[0]);
1. ) How do I work with the first element of the Data variable?
On to callback functions :
Code:
#include <stdio.h>
#include <stdlib.h>
struct Rectangle
{
int Width,Length;
float Area;
char *Name;
};
void Construct_Rectangle(struct Rectangle *StructPnt,char *AName,int ALength,int AWidth)
{
StructPnt->Name = AName; //-> is basically just dereferancing the struct like &StructPnt....
StructPnt->Width = AWidth;
StructPnt->Length = ALength;
StructPnt->Area = AWidth * ALength;
}
void Print_Rectangle(struct Rectangle *StructPnt)
{
printf("Name : %s\n",StructPnt->Name);
printf("Width : %i\n",StructPnt->Width);
printf("Length : %i\n",StructPnt->Length);
printf("Area : %f\n",StructPnt->Area);
}
void Count(void(*Referance)())
{
int i = 1 ;
for(i;i!=10;i++)
{
printf("%i\n",i);
Referance(i);
}
}
void CallBack(int a)
{
if (a%2==0) printf("%s\n","Called A");
else printf("%s\n","Called B");
}
int main()
{
//Void pointer
int AString = 5;
void *Pntr; //Generic Pointer
Pntr = AString;
printf("%i\n",Pntr);
//Structures
struct Rectangle Test;
Construct_Rectangle(&Test,"Bobby",5,10);
Print_Rectangle(&Test);
Count(CallBack);
return 0;
//Callbacks
}
Just any help with this , if I'm going wrong somewhere. General Tips!
And then libcurl :O
Code:
#include <stdio.h>
#include <stdlib.h>
#include <include/curl/curl.h>
#include <include/curl/easy.h> //#includes are funny because i had no idea what i was doing setting up curl
static int CBFWrite(void *Contents,size_t Size,size_t NumberOfUnits,void *UserPnter);
typedef struct Memblock_t;
struct Memblock_t
{
char *Data;
size_t Size;
};
static int CBFWrite(void *Contents,size_t Size,size_t NumberOfUnits,void *UserPnter)
//Callback structure : Void Pointer , size_t , size_t , void Pointer
{
size_t RawSize = Size * NumberOfUnits;
struct MemBlock_t *Chunk;
Chunk = (struct Memblock_t *)UserPnter;
return 0;
}
int main()
{
CURL *CurlHandle;
CURLcode CurlResult;
CurlHandle = curl_easy_init();
struct MemBlock_t DataChunk;
curl_easy_setopt(CurlHandle,CURLOPT_URL,"http://www.youtube.com");
curl_easy_setopt(CurlHandle,CURLOPT_WRITEFUNCTION,CBFWrite);
curl_easy_setopt(CurlHandle,CURLOPT_WRITEDATA,(void *)&DataChunk);
CurlResult = curl_easy_perform(CurlHandle);
if (CurlResult == CURLE_OK)
{
printf("%s\n","Executed With No Errors");
}
else
{
printf("%s\n","Error");
}
curl_global_cleanup();
return 0;
}
I get an error on this line
Chunk = (struct Memblock_t *)UserPnter;
Error : dereferencing pointer to incomplete type
Aaaaaaand this line
struct MemBlock_t DataChunk;
Error : Size of datachunk is unknown.
As I understand it instead of pushing everything to the stdout by using curl_easy_opt(...CURLOPT_WRITEFUNCTION,CallbackFun ction)
curl will now send it's output to that particular function.
Now this
Code:
static int CBFWrite(void *Contents,size_t Size,size_t NumberOfUnits,void *UserPnter)
is the function Curl is expecting ; That being said if I were to lets say remove a parameter - would my program simply output garbash or just skip that variable (like if what curl is expecting in the callback function was set up in a parallel array ) Eg:
My main function expects int a , char b , int c
but my callback function only provides int a and char b
what happens to int c?
As of now i feel the structures and callback functions are much more important than learning to use libcurl. Please help!
Saw Someting else:
I guess this comes from delphi XD
MemBlock->Data=MemBlock->Data+ch; //Fills my structure with data
Why does this line work? Isnt it the same to lets say AString = AString + 'c'
Which again in delphi is fine but in c shouldnt i use strcat()
Also. Shouldnt i be allocating memory somewhere . im confused.