I'm trying to make a little programme in C using SDL, displaying a robot moving on a grid. This grid is represented by a txt file of 0s and 1s.
Here is the fonction creating an array from the txt file, which works.
// create a map(array) from a text file
int (*newMap())[SIZE_HEIGHT][SIZE_WIDTH]
{
static const char filename[] = "input.txt"; /* the name of a file to open */
FILE *file = fopen(filename, "r"); /* try to open the file */
int map[SIZE_HEIGHT][SIZE_WIDTH];
char line[BUFSIZ]; /* space to read a line into */
int k = 0;
while ( fgets(line, sizeof line, file)!=NULL && k<SIZE_HEIGHT) /* read each line */
{
int i;
char *token = line; /* point to the beginning of the line */
for ( i = 0; i<SIZE_WIDTH; i++ )
{
map[k][i]=((int)*token)-48;
token+=sizeof(char);
printf("map[%d][%d]=%d\n", (int)k,(int)i,map[k][i]);
}
puts("----\n");
k++;
}
fclose(file);
int (*p)[SIZE_HEIGHT][SIZE_WIDTH];
p=↦
return p;
}
Then I try to put the grid on the sreen (not the whole fonction):
#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <time.h>
#include <string.h>
#include "Parameters.h"
#include "simulation.h"
#include "editor.h"
void simulate(SDL_Surface *ecran)
{
SDL_Surface *carreVert = SDL_LoadBMP("carreVert.bmp");
SDL_Surface *carreRouge = SDL_LoadBMP("carreRouge.bmp");
SDL_Surface *robot = SDL_LoadBMP("robotRouge.bmp");
SDL_SetColorKey(robot, SDL_SRCCOLORKEY, SDL_MapRGB(robot->format, 255, 255, 255));
int (*map)[SIZE_HEIGHT][SIZE_WIDTH];
map=newMap();
SDL_Rect positionFond;
int i;
int j;
for(j=0; j<SIZE_HEIGHT; j++)
{
for(i=0; i<SIZE_WIDTH; i++)
{
positionFond.x = 100*i;
positionFond.y = 100*j;
if((*map)[j][i]+1)
{
SDL_BlitSurface(carreVert, NULL, ecran, &positionFond);
}else
{
SDL_BlitSurface(carreRouge, NULL, ecran, &positionFond);
}
}
}
And then something strange happens: when I observe the array *map with the debugger, I see that the values are changing when I go through the test. So the grid does appear, but not with the right pattern. Why does that happen?
Edit:no error an compiler.
Edit: Any guess of what might do that would be gladly accepted.
The array map is local to the function. It ceases to exist when the function finishes. You pass its address to the caller, but when the caller tries to use it ... BANG!
Fast solution: make the array map a static one: make sure you never call newMap more than once per run.
Other solutions:
move the creation of the array outwards and pass the address around
use malloc and friends to manage the array
You are returning a pointer to an object with automatic storage duration (an array that gets "deallocated" on return from the function where it is declared), so you end up seeing random garbage.
Related
I am fairly new to C and have been trying my hand with some arduino projects on Proteus. I recently tried implementing a keypad and LCD interface with Peter Fleury's libraries, so far the characters I input are displayed fine, but I run into a problem when trying to print to the serial port. It's like the value of the keys keeps on being concatenated with every iteration so the ouput has extra characters like this:
The value before the comma is from the 'key' variable, the value after it the 'buf' variable:
151
(The 5 I input in the second iteration was added to the 1 from the first iteration and then put into the variable I print)
I figure it may be due to my lack/incorrect use of pointers, heres is my code:
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd.h"
#include "mat_kbrd.h"
#include "funciones.h"
#include "menu.h"
char buf[256];
char* coma = ",";
int main(void)
{
pin_init();
serial_begin();
lcd_init(LCD_DISP_ON);
kbrd_init();
bienvenida();
while (1) {
int i = 0;
char key = 0;
//char *peso;
//int pesoSize = 1;
char peso[100];
//peso = calloc(pesoSize,sizeof(char));
int salida = 0;
lcd_clrscr();
desechos();
key = kbrd_read();
if (key != 0) {
lcd_gotoxy(0,3);
lcd_putc(key);
_delay_ms(2000);
lcd_clrscr();
cantidad();
while (salida != 1) {
char keypeso = 0;
keypeso = kbrd_read();
//pesoSize = i;
//peso = realloc(peso,pesoSize*sizeof(char));
if (keypeso != 0) {
if (keypeso == '+') {
salida = 1;
keypeso = *("");
lcd_clrscr();
calcularTotal(key,peso);
_delay_ms(2000);
} else {
lcd_gotoxy(i,1);
lcd_putc(keypeso);
snprintf(peso, sizeof peso, "%s%s",peso, &keypeso);
//strcat(peso,&keypeso);
i++;
_delay_ms(2000);
}
}
}
snprintf(buf, sizeof buf, "%s%s%s", &key,coma,peso);
serial_println_str(buf);
}
}
}
&key and &keypeso point to a single char, but you are using the %s format specifier, so trying to read a string into a single char. Use %c rather then %s for single characters, and pass the char not the address-of-char..
I have a task to rewrite the Unix system function tail. My algorithm works in the following way:
I lseek to the end of the file, enter a loop, move the file descriptor a bit back, read a couple of bytes, and if I find any new lines, I increase the counter. Once, I get 10 new lines ( I think it should be 11, but the thing is not finished yet and this works for me for now ), I leave the loop.
If the file has less than 10 new lines, I get into an infinite loop, because of the way I wrote it.
Is there a way to see if I have reached the beginning of the file, so I can leave the loop then?
Code :
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int main (int argc, char *argv[])
{
int fd;
off_t offset = 10;
size_t size = 10;
unsigned char buff[10];
int new_line_counter = 0;
off_t total_offset = 0;//dont mind this
fd = open("a.txt", O_RDONLY);
if (fd == -1)
{
perror("open");
return 1;
}
lseek(fd,0,SEEK_END);
while (new_line_counter < 10)
{
lseek(fd,-offset,SEEK_CUR);
total_offset+=10;//dont mind this
for(int i = 0; i<10;i++)
{
if(buff[i]=='\n')
{
if(new_line_counter==10)break;
new_line_counter++;
//printf("%d",new_line_counter);
}
}
read(fd,buff,size);
lseek(fd,-offset,SEEK_CUR);
}
}
Use the return value of lseek. It will return the current file position, or, -1 on error.
You first lseek(fd,0,SEEK_END) will give you the file size.
Use that to keep track the current file position (and make sure that to adjust 'offset' so that lseek(fd,-offset,SEEK_CUR); will never seek before file begin.
This is necessary, otherwise you might miss the first line.
lseek() returns a value, it's either the new offset or -1. Check for that value like this:
while (new_line_counter < 10)
{
if (-1==lseek(fd,-offset,SEEK_CUR)) break;
I am trying to find the file(say marks.txt) in the particular path passed as argument to a function. Is it possible to give the filename and path as arguments to a function which checks if the file exists and prints out the path?
The below function only takes path as argument.
int fileexists(const char *path){
File *ptr = fopen(path, "r");
if (fptr == NULL)
return 0;
fclose(fptr);
return 1;
}
The required function prototype :
int fileexists(const char *path, const char *filename)
There are two parts to this question, and the right answers to them depend on what you're trying to do.
Concatenate a directory name and a file name to form a full path name.
Determine whether a file (referred to by a full path name) exists or not.
Concatenating a directory name and a file name is straightforward. Your friendsstrcpy and strcat will do most of the work. There are a few minor details to be careful of: (a) You'll need a big enough buffer for the full pathname, and you'll need to decide whether to use a fixed-size array (perhaps of size MAX_PATH), or a malloc'ed buffer; (b) you might need to insert an explicit '/' character (and it usually doesn't hurt to stick one in even if the directory string already ends in one); (c) under Windows you might want to use '\\' instead of '/'.
And then determining whether a file named by a full pathname exists is already well answered over at What's the best way to check if a file exists in C?. The big question to ask here is, are you asking whether the file exists in preparation to doing something with the file? If so, you have a serious vulnerability if you check for the file's existence, but then before you do the other thing, something else happens to cause the file to appear or disappear. So rather than checking-and-then-doing, it's usually better to just try doing the other thing, and deal gracefully with any errors.
The function you have checks if the file can be opened, but it will fail for some files that exist but you have no rights to open. I'd use stat instead. To concatenate the path and filename you can use string functions.
The usual Unix C APIs are dismal. It takes lots of effort to do the simplest of things correctly - and even then I'm not sure that I didn't forget some Unix-ism like signal handling or some obscure error cases. I.e. stuff that's rather trivial to get right in modern C++.
I wish someone designed a modern C system API and implemented it for at least Linux, so that our suffering would end...
Usually, string concatenation requires some higher level API to be done while maintaining a modicum of sanity. Thus, the example below uses a strbuilder class to build the string. This makes things vaguely readable and avoids most common mistakes.
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
struct strbuilder {
unsigned items, item;
size_t length, *lengths;
char *str, *dst;
};
bool strbuilder_pass(struct strbuilder *builder, int *rc);
void strcat_str(struct strbuilder *builder, const char *src);
void strcat_c_ifnone(struct strbuilder *builder, char c);
bool strbuilder_is_freed(const struct strbuilder *builder);
int fileExists(const char *path, const char *filename)
{
const char pathSep = '/';
int rc;
struct strbuilder bld = {0};
while (strbuilder_pass(&bld, &rc))
{
strcat_str(&bld, path);
strcat_c_ifnone(&bld, pathSep);
strcat_str(&bld, filename);
if (!rc)
{
struct stat statbuf;
printf("path = %s\n", bld.str);
rc = stat(bld.str, &statbuf);
}
}
assert(strbuilder_is_freed(&bld));
return rc;
}
int main()
{
int rc = fileExists("/", "dev");
assert(rc == 0);
return 0;
}
The string building is controlled by a strbuilder_pass function, which advances the string builder's state through five passes of operation:
Determine the number of items whose width has to be stored (avoids the need to call strlen twice).
Prepare the length storage vector. Determine the length of the buffer needed.
Prepare the output string buffer. Concatenate the elements into the buffer.
Use the output string buffer.
Free the output string buffer.
This API is not particularly special, but fits this use case. Some other ad-hoc approach would work too, but this is IMHO a bit more elegant.
void strbuilder_free(struct strbuilder *builder)
{
free(builder->lengths);
free(builder->str);
memset(builder, 0, sizeof(*builder));
}
bool strbuilder_pass(struct strbuilder *builder, int *rc)
{
if (!builder->length) {// start of pass 1
builder->length = 1; /*term*/
*rc = EAGAIN;
return true;
}
else if (!builder->lengths) // end of pass 1
{
builder->lengths = malloc(sizeof(*builder->lengths) * builder->items);
if (builder->lengths)
return true;
*rc = ENOMEM;
}
else if (!builder->str) // end of pass 2
{
builder->dst = (builder->str = malloc(builder->length));
builder->item = 0;
builder->length = 0;
if (builder->dst) {
*builder->dst = '\0';
return true;
}
*rc = ENOMEM;
}
else if (builder->dst) // end of pass 3
{
while (*builder->dst) { // include optional content
builder->dst++; // skip
builder->length++;
}
builder->dst = NULL;
*rc = 0;
return true;
}
else if (!builder->dst) // end of pass 4 (if any)
{}
else {
*rc = EINVAL;
}
strbuilder_free(builder);
return false;
}
void strcat_str(struct strbuilder *builder, const char *src)
{
if (!src)
return;
if (!builder->lengths) // pass 1
builder->items ++;
else if (!builder->str) // pass 2
{
size_t len = strlen(src);
builder->lengths[builder->item++] = len;
builder->length += len;
}
else if (builder->dst) // pass 3
{
size_t len = builder->lengths[builder->item++];
if (*builder->dst && (!len || *builder->dst != *src))
{
builder->dst++;
builder->length++;
}
memcpy(builder->dst, src, len);
builder->dst += len;
builder->length += len;
*builder->dst = '\0';
}
}
void strcat_c_ifnone(struct strbuilder *builder, char c)
{
if (!builder->lengths) {} // pass 1
else if (!builder->str) // pass 2
{
if (c) builder->length ++;
}
else if (builder->dst) // pass 3
{
if (!builder->length || builder->dst[-1] != c)
*(builder->dst) = c;
}
}
bool strbuilder_is_freed(const struct strbuilder *builder)
{
return !builder || (!builder->lengths && !builder->str);
}
You probably want something like this (no error checking for brevity):
...
#include <string.h> // for str* functions
#include <unistd.h> // for access
#include <stdlib.h> // for malloc
...
int fileexists(const char *path, const char *filename)
{
char *name= malloc(strlen(path) + strlen(filename) + 1);
strcpy(name, path);
strcat(name, filename);
int retval = access(name, F_OK) == 0;
free(name);
return retval;
}
Call like this:
if (fileexists("/some/path/", "somefilename.txt")) ...
I'm trying to use OpenCV to load a jpg image from file and pass it to zbar library to decode a barcode. However, no barcodes are decoded properly, even though the code below works when I use functions from libpng to load the image. I have no errors, and I have no idea where the problem is, as I have already checked all posts I could find and nothing worked.
Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <zbar.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
zbar_image_scanner_t *scanner = NULL;
IplImage* cvLoadImage(const char* filename, int iscolor);
int main (int argc, char **argv)
{
// create a reader
scanner = zbar_image_scanner_create();
// configure the reader
zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);
// obtain image data with opencv
IplImage* img = 0;
int height,width,step,channels;
img = cvLoadImage(argv[1], 1);
height = img->height;
width = img->width;
step = img->widthStep;
channels = img->nChannels;
void *raw = (void *)(img->imageData);
printf("Processing a %dx%d image \n",height,width);
// wrap image data
zbar_image_t *image = zbar_image_create();
zbar_image_set_format(image, *(int*)"Y800");
zbar_image_set_size(image, width, height);
zbar_image_set_data(image, raw, width * height, zbar_image_free_data);
// scan the image for barcodes
int n = zbar_scan_image(scanner, image);
if (n==0){
printf("No barcode detected for image %s\n", argv[1]);
return 1;
}
// extract results
if (n!=0) {
const zbar_symbol_t *symbol = zbar_image_first_symbol(image);
printf("symbol extracted \n");
for(; symbol; symbol = zbar_symbol_next(symbol)) {
// do something useful with results
zbar_symbol_type_t typ = zbar_symbol_get_type(symbol);
const char *dataZ = zbar_symbol_get_data(symbol);
printf("decoded %s symbol \"%s\" of image %s \n", zbar_get_symbol_name(typ), dataZ, argv[1]);
}
}
// clean up
zbar_image_destroy(image);
zbar_image_scanner_destroy(scanner);
return 0;
}
That code worked perfectly for me. I used it in my program with small changes:
I don't show how I get the 'struct _IplImage *' {aka 'IplImage *'} because it is done in another file, and get it as param, but it is of course done with cvLoadImage().
I used "GREY" instead of "Y800", but I tried "Y800" and also worked, as they are fundamentally the same.
This works (at least with openCV 2.4.9; openCV is deprecating its C API, and its C++ API should be used instead (I'm against this, but nothing can be done :( )):
/******************************************************************************
******* headers **************************************************************
******************************************************************************/
/* Standard C ----------------------------------------------------------------*/
/* snprintf() */
#include <stdio.h>
/* Packages ------------------------------------------------------------------*/
/* opencv */
#include <cv.h>
/* zbar */
#include <zbar.h>
/* Module --------------------------------------------------------------------*/
#include "this_file.h"
/******************************************************************************
******* macros ***************************************************************
******************************************************************************/
# define ZB_CODES_MAX (10)
# define ZBAR_LEN_MAX (1048576)
/******************************************************************************
******* structs **************************************************************
******************************************************************************/
struct ZB_Codes {
int n;
struct {
int type;
char sym_name [80];
char data [ZBAR_LEN_MAX];
} arr [ZB_CODES_MAX];
};
/******************************************************************************
******* variables ************************************************************
******************************************************************************/
struct ZB_Codes zb_codes;
/******************************************************************************
******* functions ************************************************************
******************************************************************************/
void img_zb_decode (struct _IplImage *imgptr)
{
struct zbar_image_scanner_s *scanner;
struct zbar_image_s *image_zb;
const struct zbar_symbol_s *symbol;
/* Type of code to scan */
/* 0 for all; set to another if used only for a specific barcode */
int code_type;
code_type = 0;
/* create & configure a reader */
scanner = zbar_image_scanner_create();
zbar_image_scanner_set_config(scanner, code_type, ZBAR_CFG_ENABLE, 1);
/* wrap image data */
image_zb = zbar_image_create();
zbar_image_set_format(image_zb, *(int *)"GREY");
zbar_image_set_size(image_zb, imgptr->width, imgptr->height);
zbar_image_set_data(image_zb, (void *)(imgptr->imageData),
(imgptr->width * imgptr->height), NULL);
/* scan the image for barcodes */
int i;
zb_codes.n = zbar_scan_image(scanner, image_zb);
if (zb_codes.n) {
/* extract results */
symbol = zbar_image_first_symbol(image_zb);
for (i = 0; i < ZB_CODES_MAX && symbol; i++) {
/* Write results into array */
zb_codes.arr[i].type = zbar_symbol_get_type(symbol);
snprintf(zb_codes.arr[i].sym_name, 80, "%s",
zbar_get_symbol_name(
zb_codes.arr[i].type));
snprintf(zb_codes.arr[i].data, ZBAR_LEN_MAX, "%s",
zbar_symbol_get_data(symbol));
/* Load next symbol */
symbol = zbar_symbol_next(symbol);
}
}
/* clean up */
zbar_image_destroy(image_zb);
zbar_image_scanner_destroy(scanner);
}
/******************************************************************************
******* end of file **********************************************************
******************************************************************************/
I'm writing a small school project. It's a game of falling words - the word is moving from the top to the bottom. I had an idea to make two windows (one with interface and second with moving object). Words are randomized as you can see in the code. The problem is the input. I'm using mvwsacanw to write the word. Is there any way to write anything in second window while the word is moving in different window? For now the word is falling and when it reaches the bottom, the second window opens and I can type the word.
Hope somebody will help me.
#include <stdio.h>
#include <ncurses.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
void moving(WINDOW *move)
{
int j,random;
char *cmp=(char*)malloc(10*sizeof(char));
char word[6];
wclear(move);
box(move, 0, 0);
mvwprintw(move, 1, 1, "PIS");
wrefresh(move);
srand (time (NULL));
random=2+rand()%7;
for(j=0; j< random ; j++) //random word
{
word[j]= rand()%26+'a';
}
int poz = 2+rand()%24; //random position of moving word
for(int i=1; i<18; i++)
{
wclear(move);
box(move,0,0);
mvwprintw(move,i, poz, word);
wrefresh(move);
usleep(300000);
}
}
void interface(WINDOW *ui)
{
wclear(ui);
char *cmp=(char*)malloc(10*sizeof(char));
box(ui, 0, 0);
mvwprintw(ui,1,1,"wpisz wyraz: ");
mvwscanw(ui,2,1, "%s",cmp);
mvwprintw(ui, 3, 1, "->%s",cmp);
wrefresh(ui);
}
int main(int argc, char *argv[])//int argc, const char * argv[])
{
int x,y;
int sc = 3;
initscr();
noecho();
curs_set(FALSE);
getmaxyx(stdscr, y,x);
WINDOW *move = newwin(y-5, x-1, 0, 0);
WINDOW *ui = newwin(sc+2, x, y-5, 0);
while(1)
{
moving(move);
interface(ui);
wclear(move);
wclear(ui);
}
delwin(move);
delwin(ui);
endwin();
return 0;
}
You can't do that with your current code structure. You are keeping the word fall phase and the input phase in separate functions, so the only way to make them work at the same time is some kind of multithreading.
Assuming this is not what you want to do, you could try to merge the two features in a single function. In pseudocode:
pick random word
pick random position
set i = 0
set input = {} //empty array
do
> print word at (i, pos)
> set stoptime = time() + DELAY
> do
>> set c = getch()
>> append c to input
>> print interface
> while (time() < stoptime)
> i++
while (i < 18)
This, with timeout() set to an opportune delay, will give the impression that everything is happening simultaneously.
This is absolutely not the most efficient solution, but is simple and straightforward, and considering you are working on a school project, it should be just fine
Try to use the following code :
nodelay(your_window, TRUE);
which will make your input nonblocking for the given window !