Welcome, Guest. Please login or register.

Author Topic: Executing c command in c  (Read 10457 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: General C help
« Reply #14 on: May 19, 2017, 08:57:11 AM »
Well Olsen and Thomas thanks to your explanations about APTRs and BPTRs something clicked and I was able to work out how to get the string value from the mui_pop_string.

The talk of pointers, objects and strings made me realise that muia_popstring_string was returning a mui string object and not a string so I retrieved the value in 2 parts.

First I did a get() on the pop object to get the popstring_string object and then did another get on the returned object to get the strings content.

So now I think I understand everything I need to to get my first program completed without having to ask anymore questions.
There's still some stuff that I don't understand such as hooks but I think I'll be able to avoid creating any of my own.

So hopefully I won't be bothering anyone for help and advice till I have something to show.

You guys have helped point me in the right direction and understand so much more than all the guides and tutorials I spent weeks on and the reason why I always return to Amiga.org when I have an Amiga related question.

Cheers :D
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: General C help
« Reply #15 on: May 26, 2017, 09:02:15 AM »
Quote from: foleyjo;825982


So hopefully I won't be bothering anyone for help and advice till I have something to show.



Well it seems that I was wrong.
I have got through a lot and nearing first release. However I have hit a wall.

My code was getting too long and difficult to read so I decided to separate it into extra files.

I have created .h files and put includes to them in the .c files. I have got externs for when I want to share variables across pages and everything compiles fine.
Yet I keep getting crashes.
I have narrowed it down to if commands not working.

In the main file if I have
Code: [Select]

FILE * fp;
file = fopen("new_file","w");
if(fp){
  printf("file open");
  fclose(fp);
}
printf("file closed");


The output as you would expect is
-file_openfile closed

If the same code is placed in one of the sub files the output is
-file closed
and then when I close the program I get an error because the file wasn't closed.

Any ideas what I've missed ?
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #16 on: May 26, 2017, 10:25:03 AM »
With the extern command does it matter that I'm not declaring fp globally or passing it between functions?

so when I had the code in the sub function, in main I had a call to the subfunction with nothing else and then I just had the code from the example in the subfunction.

Everything else had been commented out.

It seems to be happening with all if/while statements where the result is expecting TRUE or FALSE and just taking the FALSE result.
The variables themselves are working without the if/whiles.
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #17 on: May 26, 2017, 12:53:01 PM »
Think I've confused everyone with my explanation.

fp is declared locally. So I either have (just a quick example so ignore typos etc)

Example 1 = in 1 file
int main ()
{
  FILE *fp;
  fp =fopen("file","w");
  if(fp){do something}
  return 0;
}

Example 2 = in 2 files (with  .h files declaring the functions)
in file2.c
int function()
{
  FILE *fp;
  fp =fopen("file","w");
  if(fp){do something}
  return 0;
}

in file1.c

int main()
{
  function();
  return 0;
}


In Example 1 the if(fp) returns TRUE.
In Example 2 the if(fp) returns FALSE

Think I've confused everyone with my explanation.

fp is declared locally. So I either have (just a quick example so ignore typos etc)

Example 1 = in 1 file
int main ()
{
  FILE *fp;
  fp =fopen("file","w");
  if(fp){do something}
  return 0;
}

Example 2 = in 2 files (with  .h files declaring the functions)
in file2.c
int function()
{
  FILE *fp;
  fp =fopen("file","w");
  if(fp){do something}
  return 0;
}

in file1.c

int main()
{
  function();
  return 0;
}


In Example 1 the if(fp) returns TRUE.
In Example 2 the if(fp) returns FALSE

I haven't got fp declared globally. I try to avoid global variables as I've learned my lesson in the past from a global getting changed in a function when I didn't mean it to be changed.

The thing is, it all worked fine before I separated the code it into separate files. Anything in my main code file still works. It's only when I come to the other sections of code. If I put it back into 1 file it should work again but it's getting very long and hard to follow.
I suppose I could try adding comments to split the code up into sections

With variables I admit I don't name them the best but I do try to make them meaningful to me and usually use full words in my actual code. I use abbreviations for some things but only when the abbreviation is automatic to me. So when I see fp I'm actually reading it as File Pointer
« Last Edit: May 26, 2017, 01:11:42 PM by foleyjo »
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #18 on: May 26, 2017, 01:46:23 PM »
LiveForIt - You might be onto something. (though I must point out my function isn't called function I just used that for the example. The one where I noticed the problem is actually called WriteNewDataToFile but I noticed the same issue across all functions not just with files).

I do have the header files and they are referenced in the c files.
What I haven't done is added the extern function lines in the main h file.
I have done it for variables that are passed between files.

Olsen - The file does seem to open. It creates an empty file in the correct folder with the correct name.
It doesn't close though as the if command doesn't work and my fclose is within the if.
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #19 on: May 26, 2017, 01:55:58 PM »
Just thinking about what has been suggested already I'm just wondering,


if I have (again just examples not actual function names, headers all declared and bits missed out)


main.c
Code: [Select]

function1()
{
  FILE *fp
  fp =  fopen("thisfile","r");
  if(fp){
  do stuff....
  fclose(fp);}
}

main()
{
  function1();
  function2();


other.c
Code: [Select]
function2()
{
  FILE *fp
  fp =  fopen("thisfile","w");
  if(fp){
  do stuff....
  fclose(fp);}
}

because I have used fp in main.c would I need to set an extern File *fp in one of the headers to use it again in other.c ? Even though fp was declared locally in both function1 and function2 ?
My current way of thinking is that I wouldn't need to because the 2 fps are actually different because they are not declared globally.
However I'm starting to wonder if that is the case.
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #20 on: May 26, 2017, 03:52:13 PM »
again in my real code the functions and declarations are declared properly and I've just been lazy in the examples.
So my real main lookslike

int main(int argc, char *argv[])
{
....
return 1;
}

I'll try adding externs for the functions and if it doesn't work I think I'll just go back to 1 file.
Everything was working as expected till I moved to separate files. In trying to locate the issue I ended up commenting out everything till all I had left was similar to the example I gave where if(fp) didn't work.
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #21 on: May 30, 2017, 09:05:06 AM »
Well quite embarrassing but the issue was in another section of code where I hadn't put in a semi colon inside and else statement.

Only thing I'm struggling with is strings. I've been using malloc wrong and think I've made a mistake of not putting in a close character.

Need to put an integer into a structure string too. I've tried using sprintf(list_items->max_number"%d",total); but it just crashes
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #22 on: May 30, 2017, 01:55:27 PM »
Had a typo there. List_items->max_number is my char *. Should have been a , after it.
I think it'll be easier to make it an int inside the structure. I had problems. Not sure why I did it as a char chat in the first place. Think I was getting a crash and changing it fixed things. Should have known it would come back to haunt me.
I'll check out snprintf too
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #23 on: May 30, 2017, 03:06:34 PM »
Thanks for the tip.
I changed it to an int and now it works as expected. I created it as a char because I didn't understand pointers.
I've just been through my code and comments and realised one of my functions uses char ** .
When I tried to make a similar function to work with integers and not strings I had used int ** instead of int*.
I found a good tutorial on pointers and addresses  so was able to spot the mistake this time and understood why it caused a problem.

Just been reading about snprintf and I had come across it before but there is lots of conflicting information.
sprintf,snprintf,strcpy and strncpy all seem to have people saying you should use them or you shouldn't use them.
I tend to malloc my destination string to the size of the source string and then add a null terminator
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #24 on: May 30, 2017, 03:50:24 PM »
tried strdup() previously and just tried strlcpy().

For both I get no prototype function.
Guessing that relates to the compiler I'm using (Storm C 3.0) .

So really I should be using snprintf in the places where I used strcpy.
I actually used strcpy because the tutorial I read said I shouldn't use sprintf. I only started using sprintf when I was looking for a way to write an integer into a string and that's when I started reading the reverse argument that strcpy shouldn't be used. Both arguments talked of the snprintf and strncpy but again both claimed 1 should be used and the other was bad.
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #25 on: May 31, 2017, 11:07:34 PM »
Ok I solved my issue for this post.I had a do while instead of a while do which was causing silly things to happen. My 2nd question I still don't know so leaving it below
Which leads me to a curiosity I have.
If calloc is used to assign memory for an array and a string is an array of characters, why is calloc not used for strings (at least not in the examples I can find)
« Last Edit: May 31, 2017, 11:34:48 PM by foleyjo »
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #26 on: June 01, 2017, 02:41:47 PM »
Quote from: olsen;826554

If the tutorial does not explain why one choice is better suited than the other, what can you do but ask us? :)


I don't know where I'd be if it wasn't for asking you guys.
Actually yes I do. I'd have given up after "Hello world" .
I have a few bugs that are causing crashes relating to memory now. I may be able to solve them once I have stepped through the code.
Will probably be asking for help later though knowing me
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #27 on: June 02, 2017, 12:13:54 PM »
I got rid of the memory crashes.
Just a problem with my mui list now.
I have 2 columns ID and Name but only 1 displayed.
When an entry is clicked it should return the ID and then I use the ID to do tasks relating to the entry.
However the mui examples I found only return the item number in the list.
This is fine until I sort my list. Then the returned number and ID no longer match.

Now I think I need to use a compare hook. but I don't understand how the hooks work.
Or is there another way to return the correct ID.

This is the code I'm currently using to get the number of the item in the list
Code: [Select]

int num;
get (list_mylist,MUIA_List_Active,&num);
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #28 on: June 02, 2017, 09:24:52 PM »
Apart from the mui specific stuff (Not sure if my question on getting items from a sorted list was missed or if nobody had an answer) my main problems have been strings. I'm used to languages where I don't have to worry about where they are in memory or remembering to have the end string characters. Now I have my head around those I'm getting the desired outputs and not getting any memory related errors.
It's nice to learn a different way of looking at them. I always knew they were arrays of characters but never thought too much about what the functions were actually doing.
 

Offline foleyjoTopic starter

  • Hero Member
  • *****
  • Join Date: Nov 2005
  • Posts: 608
    • Show all replies
Re: Executing c command in c
« Reply #29 from previous page: June 03, 2017, 08:41:30 AM »
I think I might change from mui to reaction. I have more of an understanding of how things are working now.