remove() - Called object 'remove' is not a function - c

So I've tried from another post to make this code:
while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
count++;
if(count == linha)
{
fprintf(tempFile,"%s;%d;%s\r\n",orderNameFile,orderSecondsFile,orderTimeFile);
}
else
{
fprintf(tempFile,"%s;%d;%s\r\n",fileName,seconds,timeValue);
}
}
fclose(tempFile);
fclose(orderFile);
remove("order.txt");
rename("temp.txt","order.txt");
I also have included the stdio.h lib
#include <stdio.h>
Yet when I run this code, it gives an error on the remove function, saying:
error: called object 'remove' is not a function or function pointer
I've tried to create a char name[] = "order.txt"; and use it inside the remove(); instead but didn't work as well, also already created an int variable like int x; x = remove("order.txt"); and it didn't work.
Any ideas?

You have a variable named remove somewhere in your code, rename it.

Related

map function pointers to specific numbers at runtime

I have a problem that i can even start to work on because i don't get it how can be done.
So we have a code
int test_handler() {
printf("Test handler called\n");
return 1;
}
// Test your implementation here
int main()
{
register_irq_handler(30, &test_handler);
do_interrupt(29); // no handler registered at this position, should return zero
do_interrupt(30); // calls handler at position 30, expected output: Test handler called
return 0;
}
I need to make those functions register_irq_handler, do_interrupt(29).
But i have no clue how to start, i am looking for a little help to send me on the right direction.
How i store 30 to point to this function when we don't have a global variable to store that "connection" or i am missing something.
You can't do it without a global variable (why would having a global variable be a problem?).
You probably need something like this:
// array of 30 function pointers (all automatically initialized to NULL upon startup)
static int(*functionpointers[30])();
void register_irq_handler(int no, int(*fp)())
{
functionpointers[no] = fp;
}
int do_interrupt(int no)
{
if (functionpointers[no] != NULL)
{
// is registered (!= NULL) call it
return (*functionpointer[no])();
}
else
{
// not registered, just return 0
return 0;
}
}
Disclaimer
This is non tested non error checking code just to give you an idea.

Function mocking in C?

I'm writing a unit-test to check some API calls. I am using check to test. My module is build with CMake (idk if it matters).
My test calls a function (which I need to test) and this function makes a call to another binary.
Simplified version of it looks like this.
/* unitTest.c */
#include "libraryAPI.h"
void letsMakeACall(void)
{
ck_assert_eq(foo("water"), 0);
}
-- Module I am working on---
/*libraryAPI.c*/
#include "legacyLib.h"
void foo(const char *drink )
{
if (checkDrink(drink)!=0)
{
return 1;
}else
{
return 0;
}
}
----LEGACY BINARY---
/*legacyLib.c*/
static const char* expected = "water";
void checkDrink(const char *drink)
{
if(drink == expected)
{
/*There are also a dozen functions being called which depend on legacy module initialisation*/
return 0;
}else{
return 1;
}
}
I'd like to mock response from legacyLib, because otherwise it call dozens of functions and breaks. My initial idea was to add some ifdef conditions when tests are being run, but it is against guidelines.
Because it is basically a call interception I don't know what it a best(or working) solution. What can I use to solve it?
I am also unsure how to solve this generally, I have posted a similar question, but in some cases you can do the following (presuming you are testing individual functions):
Include the .c file instead of the header .h, but after you "rename" your mocked function using a define directive:
#define checkDrink checkDrink_mocked
// preprocessor will now replace all occurrences of "checkDrink"
// with "checkDrink_mocked"
int checkDrink_mocked(const char *drink);
#include "legacyLib.c"
#undef checkDrink
Implement the renamed function:
int checkDrink_mocked(const char *drink)
{
return 15;
}

How to properly create a C function (including header file)

I'm trying to create my own functions in C, and then use #include with a header file. I know how to make the header file, and I've written the .c function. However, when I try to compile the .c, I get an error that says '[Linker error]undefined reference to 'WinMain#16'' and it fails to compile.
Then, if I try to use it in a program, it says '[Warning]no newline at end of file' and then '[Linker error]undefined reference to validf(int, int, int)'.
Can anyone help?
Function Code:
int validf(int current,int max, int zero)
{
if(zero==1)
{
if(current>max || current<0)
{
printf("Invalid Input");
return 0;
}
else
{
return 1;
}
}
else if(zero==0)
{
if(current>max || current<=0)
{
printf("Invalid Input");
return 0;
}
else
{
return 1;
}
}
else
{
printf("Invalid parameters");
return -1;
}
}
Main Code:
#include<stdio.h>
#include<stdlib.h>
#include "validf.h"
int main()
{
int valid=0;
valid=validf(4,5,0);
printf("%d",valid);
system("\npause");
return 0;
}
Header Code:
#ifndef VALIDF_H_
#define VALIDF_H_
int validf(int current,int max,int zero);
#endif
Your program consists of 3 files:
validf.h header file for validf. Contains declaration of validf function
validf.c code file for validf. Contains definition of validf function.
main.c contains the main function. You may have chosen another name for this file.
In your IDE, you should create a project that consists of these files.
You also need to configure the name of the resulting program. I am not familiar with that particular IDE, but it is usually done under Project->Settings->Compile or Build or Link.
This will make your IDE compile the two .c files and then link them into a single program.
If you dont create a project it is probable that the IDE treats each .c file as a different program, which will cause the errors you mention.

Why does this variable value return to default value after exiting the function?

I want to move the knight in a chess program. For this reason,
I have these two variables(currentRow and currentColumn) defined on top of all functions including main. (I did this because I wanted these variables as global variables to all functions) as below. Because when the knight moves, its position will change. And this will be the input to its next move.
What I don't understand is when I debug, I saw that these variables are changing in the function but as soon as it exits function, they return to their default values (3 and 4).
Can you tell me how to fix this? Thanks in advance...
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int currentRow=3;
int currentColumn=4;
int main(void){
...
}
int checkIsEmptyandMoveAccordingly(int moveNumber, int currentRow, int currentColumn){
if (chessBoard[currentRow+vertical[moveNumber]][currentColumn+horizontal[moveNumber]]==0 && currentRow+vertical[moveNumber]>=0 && currentColumn+horizontal[moveNumber] >=0 ){ //if empty,move to new location
currentRow+=vertical[moveNumber];
currentColumn+=horizontal[moveNumber];
printf("Move randomised to: %d\n", moveNumber);
printf("Knight has moved to chessBoard[%d][%d].\n",currentRow,currentColumn);
count++;
printf("Move count is %d.\n",count);
chessBoard[currentRow][currentColumn]=1;
if(!checkIsAroundFUll()){
moveNumber=randomiseMovement();
return moveNumber;
}
else {
printf("ALL TARGET SPACES ARE VISITED BEFORE. KNIGHT CAN NOT MOVE\n PROGRAM WILL BE TERMINATED!!!\n");
return -1;
}
}
else if (chessBoard[currentRow+vertical[moveNumber]][currentColumn+horizontal[moveNumber]]==1) { //if not empty, randomise again
printf("Knight CAN NOT MOVE! Target SPACE IS OCCUPIED\n");
if(!checkIsAroundFUll()){
moveNumber=randomiseMovement();
return moveNumber;
}
else {
printf("ALL TARGET SPACES ARE VISITED BEFORE. KNIGHT CAN NOT MOVE\n PROGRAM WILL BE TERMINATED!!!");
return -1;
}
}
else {
printf("OUT OF BOUNDS!! CAN NOT MOVE. TRYING ANOTHER MOVEMENT");
if(!checkIsAroundFUll()){
moveNumber=randomiseMovement();
return moveNumber;
}
else {
printf("ALL TARGET SPACES ARE VISITED BEFORE. KNIGHT CAN NOT MOVE\n PROGRAM WILL BE TERMINATED!!!");
return -1;
}
}
}
int currentRow, int currentColumn are in the function parameter list, so they are local variables. They are hiding the global ones with the same names.
Your function has new variables currentRow and currentColumn declared as parameters to the function. If you want to update the global variables, remove these parameters (and don't pass them when you call the function) and you should see the globals update.
What you're doing is shadowing the global variables. With the right compiler warning enabled (which varies by compiler) you would be told about this error.
Try compiling with -Wall -Werror if you are using gcc.
Your function is changing local copies. When you pass them to the function, they pass by value, the function creates local copies, and the local scope overrides the global scope. If you want to reference global variables, don't pass them into your function, just access them from there.

sqlite3 library undefined reference error

I am creating a word list searcher in C for my program using sqlite3 but I've got these errors .
I tried whatever I knew but it didn't fixed. I guess the problem is in my join function but I am not sure.
code :
bool *gb_wordlist_add_to_list (gbwordlist *word_list,char *str)
{
int sql_error;
char *error_massage;
if (gb_wordlist_in_list (word_list,str))
{
sql_error = sqlite3_execute(word_list->database, gb_wordlist_join(ADD_TO_TABLE_COMMAND"\'",str,"\';"),
NULL ,NULL, &error_massage);
if( sql_error!=SQLITE_OK )
{
fprintf(stderr, "SQL error: %s\n", error_massage);
sqlite3_free(error_massage);
return 0;
}
}
else
return 0;
}
char *gb_wordlist_join (char *s1,char *s2,char *s3){
char *s;
s = malloc(strlen(s1) + strlen(s2) + strlen(s3) + 1);
if(s)
{
strcpy(s,s1);
strcat(s,s2);
strcat(s,s3);
}
return s;
}
error:
gb-sql.o: In function `gb_wordlist_remove_from_list':
/home/reza/Project/GB/Search algorithm/Source/gb-search/src/gb-sql.c:104: undefined reference to `sqlite3_execute'
Also my full codes are here. Thanks a lot!
The reason you are getting undefined reference to sqlite3_execute is well there is no such function as part of library. You probably meant to use sqlite3_exec (which use have used in some parts of the code).
Side Notes:
The function gb_wordlist_callback is returning int but has been declared to return int*. You should change the return type to int to match the expected parameters to be passed to sqlite3_exec(after modifying from sqlite3_execute)
The 4th parameter passed to sqlite3_exec (after modifying from sqlite3_execute) is expected to be void* so existance should be &existance
You have quite a few functions with mismatch between return type declared and the actual return type returned from the function.
Compile your code with -Wall -Wextra compiler options & fix all the warnings. It is good practice.
Hope this helps!

Resources