there is a code:
void sdl() {
char text[] = "texttext";
if(SDL_Init(SDL_INIT_VIDEO) == 0) {
if(SDL_SetClipboardText(text) == 0) {
printf("work");
}
}
SDL_Quit();
}
I looked at different implementations and did not find what I did not do what others did. I compile on linux. the code completes without errors, SDL_Init works.
Related
Please I am finding it hard to recursively get all files in a directory. My code works fine, just that it only get files in current directory. Please anyone should help me view my code to check what i have done wrong.
Here is my code:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int list2() {
TCHAR currentdir[MAX_PATH];
WIN32_FIND_DATA findDat2;
GetCurrentDirectory(MAX_PATH, currentdir);
strcat_s(currentdir, strlen(currentdir)+strlen("\\*")+1, "\\*");
HANDLE hFind2 = FindFirstFile(currentdir, &findDat2);
if (hFind2 == INVALID_HANDLE_VALUE) {
printf("INVALID HANDLE VALUE");
}
else {
INT dot1 = strncmp(".", findDat2.cFileName, strlen("."));
INT dot2 = strncmp("..", findDat2.cFileName, strlen(".."));
do {
if (dot1 !=0 && dot2 != 0) {
if (findDat2.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
SetCurrentDirectory(findDat2.cFileName);
list2();
}
else {
printf("%s\n", findDat2.cFileName);
}
}
} while (FindNextFile(hFind2, &findDat2));
}
}
int list(CHAR *startDir) {
WIN32_FIND_DATA findDat;
TCHAR path[MAX_PATH];
TCHAR cpstartDir[MAX_PATH];
strcpy_s(cpstartDir, sizeof(cpstartDir), startDir);
HANDLE hFind = FindFirstFile(startDir, &findDat);
if (hFind == INVALID_HANDLE_VALUE) {
printf("INVALID_HANDLE_VALUE with finding file\n");
return 0;
}
else {
INT dot1 = strncmp(".", findDat.cFileName, strlen("."));
INT dot2 = strncmp("..", findDat.cFileName, strlen(".."));
do {
if (dot1 !=0 && dot2 != 0) {
if (findDat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
SetCurrentDirectory(findDat.cFileName);
list2();
}
else {
printf("%s\n", findDat.cFileName);
}
}
} while (FindNextFile(hFind, &findDat));
}
}
int main() {
list("C:\\*.*");
return 0;
}
This only get files in a current directory
What you need is a recursive function that recurses into each directory found (except for . and ..).
I don't see the point in using SetCurrentDirectory. You don't want to change your working directory, but instead you just want the contents of the lower level path.
So list should call itself for each subdirectory with the argument being: the parent path + \ + the name of the subfolder.
I created a cross platform open source library to traverse an entire tree: https://github.com/brechtsanders/libdirtrav
Maybe this is useful for you so that you wouldn't have to reinvent everything.
This is just suppose to display a bmp image to the SDL window front buffer. I played around with the code. And I think there is something wrong with my init() function. I'm new to SDL. But there must be a problem with my pointers or something I'm missing about SDL's fucntions
EDIT: I used GDB and it turned out my close() function was the problem. I believe it was because I was freeing memory that was set to NULL? I got rid of the close fucntion and just freed mem after my delay function.
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#define SCREENWIDTH 640
#define SCREENHEIGHT 480
SDL_Window *win = NULL;
SDL_Surface *scrn = NULL;
SDL_Surface *mscrn = NULL;
bool init()
{
bool suc = true;
char name[11] = "Hello SDL";
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("%s", SDL_GetError());
suc = false;
}
win = SDL_CreateWindow(name, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREENWIDTH, SCREENHEIGHT, SDL_WINDOW_SHOWN);
if (win == NULL) {
printf("%s", SDL_GetError());
suc = false;
}
scrn = SDL_GetWindowSurface(win);
return suc;
}
bool loadmedia()
{
bool suc = true;
mscrn = SDL_LoadBMP("hello_world.bmp");
if (mscrn == NULL) {
printf("%s", SDL_GetError());
suc = false;
}
return suc;
}
void close()
{
SDL_FreeSurface(mscrn);
SDL_DestroyWindow(win);
SDL_Quit();
}
int main(int argc, char* args[])
{
if (!init()) {
close();
return 1;
}
if (!loadmedia()) {
close();
return 1;
}
SDL_BlitSurface(mscrn, NULL, scrn, NULL);
SDL_UpdateWindowSurface(win);
SDL_Delay(3000);
close();
return 0;
}
You should find a reasonable debugger and other tools to to find out which line is causing the error and why. Basically it boils down to using a debugger which usually comes with your IDE if you're using one, or using the very good code analysis tool, Valgrind.
If you're using gcc you can likely use gdb to debug your program easily. Here are some resources on how to help you diagnose segmentation faults:
Determine the line of C code that causes a segmentation fault?
http://www.cprogramming.com/debugging/segfaults.html
Get familiar with these tools, as they will save you countless hours in the future when you face new problems.
i have a pointer problem:
SearchResults* pointy;
pointy = returnResults();
if(pointy != NULL && pointy->results[0] != NULL)
{
HandleResponse();
printf("sharp");
}else{
//do other things
}
if(pointy == NULL){
printf("blunt");
}
if(pointy->results[0] == NULL){
printf("wah!!!");
}
in the debugger the code correctly works and i get "sharp" but under the same conditions in the bash terminal i get "wah!!!"
typedef struct SearchResults
{
TreeNode* results[40];
int searchIndex;
} SearchResults;
SearchResults* lostAndFound;
SearchResults* returnResults()
{
return lostAndFound;
}
Found a Problem in both the debug and release variations there is a .csv file.. the debug reads and writes to it perfectly fine whereas the release seems to steamroll it into nothigness.
Based on this example, I try to implement font rendering in my SDL application. When calling hb_shape(), the application is halted because of an access-violation.
DLL-download-link (win32): here {harfbuzz-0.9.26-win32.zip}
ErrorMsg (VC2012): Unhandled exception at 0x6160B7F0 (libharfbuzz-0.dll)in ConsoleApplication2.exe: 0xC0000005: Access violation while reading at position 0x00000068
EDIT: I changed the example to a console application, for simplicity.
Edit2: Now linking statically, .lib-file created with VC++'s LIB.exe.
#include <Windows.h>
#include <iostream>
#include <hb.h>
#include <hb-ft.h>
#pragma comment(lib,"lib/x86/freetype253ST.lib") // freetype single-thread
#pragma comment (lib,"libharfbuzz-0.lib") // linked to libharfbuzz.dll, created by LIB.exe
int main()
{
hb_font_t* font = NULL;
hb_buffer_t* buffer = NULL;
FT_Library flib;
FT_Face face;
bool found = false;
const char text[] = {"Write something...."};
if (FT_Init_FreeType(&flib) == 0)
{
if (FT_New_Face(flib,"arial.ttf",0,&face) == 0)
{
for (int i = 0; i < face->num_charmaps; i++)
{ // search for UNICODE 2.0 charmap
if ((face->charmaps[i]->encoding_id == 3 && face->charmaps[i]->platform_id == 0) ||
(face->charmaps[i]->encoding_id == 1 && face->charmaps[i]->platform_id == 3) )
{
FT_Set_Charmap(face,face->charmaps[i]);
found = true;
break;
}
}
if (found && FT_Set_Char_Size(face,0,50*64,72,72) == 0)
{
font = hb_ft_font_create(face,NULL);
buffer = hb_buffer_create();
if (hb_buffer_allocation_successful(buffer))
{
hb_buffer_set_script(buffer,HB_SCRIPT_LATIN);
hb_buffer_set_language(buffer, hb_language_from_string("en",strlen("en"))); // strlen("en") is awful but good enough here
hb_buffer_set_direction(buffer,HB_DIRECTION_LTR);
hb_buffer_add_utf8(buffer,text,strlen(text),0,strlen(text));
hb_shape(font,buffer,NULL,0); // Access violation
std::cout << "There\n";
hb_buffer_destroy(buffer);
}
hb_font_destroy(font);
}
FT_Done_Face(face);
}
FT_Done_FreeType(flib);
}
Sleep(3000);
return 0;
}
Trying to write my programme into the windows registry but my code tells me there is an error when doing the RegSetValueEX(). I have administrative access. I can't see whats wrong at all and I've been staring at MSDN pages on REG all day.
int StartupKey()
{
int StartupKey;
long RegOpenResult, result_write;
const char *FilePath[]= "C:\\Windows\\security\\BensKlog.exe";
LPCSTR Klog = "BensKLOG";
HKEY hkey;
printf("Opening Key...\n");
RegOpenResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hkey);
if(RegOpenResult != ERROR_SUCCESS) {
if(RegOpenResult == ERROR_FILE_NOT_FOUND) {
printf("Not found\n");
} else {
printf("Error Opening Key\n");
}
} else {
printf("SUCCESS!!!\n");
}
StartupKey=RegCreateKey(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",&hkey);
printf("Writing Value named Klog\n");
result_write = RegSetValueEx((HKEY)hkey,Klog,0,REG_SZ,(BYTE *)FilePath,strlen(FilePath));
if(result_write != ERROR_SUCCESS) {
printf("Error Writing Value\n");
} else {
printf("SUCCESS!!!\n");
}
RegCloseKey(hkey);
}
const char *FilePath[]= "C:\\Windows\\security\\BensKlog.exe";
Use either:
const char FilePath[] = "C:\\Windows\\security\\BensKlog.exe";
or
const char *FilePath = "C:\\Windows\\security\\BensKlog.exe";
but don't mix them (your code defines an array of const char pointers instead of one pointer).
(Might not be the only error though)
Spoke to a lecturer at my university, showed him
My code he claims I need to run on an Administrator account, turns out my
User on my laptop wasn't the admin(which I thought it was) will check if it works when I'm home and update