Your target can only be one word. Change it to:Zitat:
Zitat von CtrlAltDeleteDie
Code:TARGET = SelectSquare
Printable View
Your target can only be one word. Change it to:Zitat:
Zitat von CtrlAltDeleteDie
Code:TARGET = SelectSquare
Spoiler for old post:
Strings and variable length
Hi,
how can I create strings of variable length in C(non++)? I have a viariable that will be filled in a loop; I can't say when this loop will stop, I don't know how long the string will be. Do I have to use realloc(string, sizeof(string)+sizeof(add edString)) each time in the loop? Or is there a better way?
char string[variable]; ?
I can't remember, its been so long since I've programmed, and now I'm taking a Java class in school...
And what if the string gets longer than <variable>?Zitat:
char string[variable];
-= Double Post =-
I'd actually even be happy if someone could confirm that the method I thought of (using realloc each time in the loop) actually is a method and not utter bull****
malloc & realloc.
You really want to avoid reallocating memory at runtime on a console. Just make the buffer a fixed size (eg 4096) and ensure that the message sent is no longer then that.
Your method of realloc would work but might be slow depending on the implementation of the function.
Another method would be to send the size of the message of first then the actual message.
I'm retrieving a list of artists from winamp, always different and surely larger than 4096 at many PCsZitat:
Just make the buffer a fixed size (eg 4096) and ensure that the message sent is no longer then that.
If it makes the console run slow I think I'll go with the other solution you mentioned. Can you actually send and recieve X bytes or is there a limit? Like i need to send a list of 411 Artist (~6165bytes) at once...Zitat:
Your method of realloc would work but might be slow depending on the implementation of the function.
-= Double Post =-
Converting numbers to char-arrays
I get a number from sizeof and need to save it in an array of chars (char length[8]). How do you do that?
Number to char arrays, use sprintf.
Yes there is a limit to how much data you can send in a packet on the network. I can't remember off hand what it is though.
You'd have to use the itoa function.Zitat:
Zitat von Lukeson
Or sprintf if you want.
Thanks guys
-= Double Post =-
Hmmm but how do I convert a char to an int? I tried
artist_list_length = (int)artist_list_length_c ;
but it fills artist_list_length with 16thousand-whatever-the-unsigned-int-maximum is...
atoi()
Yay, everything works fine now, thanks again!
PS: This is the first time I'm doing something in C, so please be kind to me
-= Double Post =-
i have another noob-question: I got the following code
to get the artists from winamp. The part with malloc freezes my PSP. How else can I make buffer the right size?Code:void get_artists(int socket, char * recivedMessage)
{
buf[recieved_size] = recieve_message_of_variable_length();
recivedMessage = (char)malloc(recieved_size);
}
char buffer;
get_artists(socket, buffer);
I've been trying to get the debug text to work with a prx for devhook.
I first tried using the normal one from pspdebug.h (pspDebugScreenPrintf)
but that gave me these errors
Spoiler for errors:
I also tried without defining which I thought maybe could possibly workbecouse I had -fno-builtin-printf which I have in CFLAGS. I didn't get any errors then but no text appeared..
Do I have to do something else to get it working in prx'es? Or is there some other simple text function that exists that works?
waterbottle - Sorry, i havent any experience with PRX printing routines... Or anything PRX that matter...
Lukeson - 'malloc' doesnt just stop working and makes it crash. Either you havent free'd the current memory in recieveMessage, you're trying to allocate memory to something whose memory already has been allocated or all else fails, something you're calling before is causing it to crash -- meaning maybe a call to malloc before yours corrupts the stack, or something of the sort... All i know for sure is malloc doesnt just stop working for no reason...
Best of luck with your problem, wish i could help more...
Can vars have double quotes in them?
For example:
Code:short inches = "Inches";
psphacker - character arrays, yes. Others, not so sure but you CAN add the ASCII value together and have hte same effect I suppose, but after each char you need an + operator...
int string = 'h'+'e'+'l'+'l'+'o'; // add ASCII values together, not so useful
char string[] = "hello"; // auto sizes array
char string[6] = "hello"; // make room for '\0' char
The correct version should be:Zitat:
Zitat von Me
However this version freezes after printing "recievedMessage-size: 15" and doesn't print "get_artists returned: message".Code:void get_artists(int socket, char * recivedMessage)
{
recieved_size = recieve_message_size();
buf[recieved_size] = recieve_message_of_variable_length();
recivedMessage = (char*)malloc(recieved_size);
printf("recievedMessage-size: %d\n", sizeof(recivedMessage));
return;
}
char * buffer;
get_artists(socket, buffer);
printf("get_artists returned: %s", buffer);
Does anyone have an explanation for this?
-= Double Post =-
Or, if not an explanation, probably even a better solution for getting a string of variable length from a function
you assign the return value from receive_message_of_variab le_length() to the array, not the variable recieved_sizeCode:void get_artists(int socket, char * recivedMessage)
{
buf[recieved_size] = recieve_message_of_variable_length();
}
Since recieved_size is not assign a value, it contains random value. You're allocation a random number of memory.Code:
recivedMessage = (char*)malloc(recieved_size);
}
Memory allocated, but you didn't assign any value to it. You're trying to print some random value.Code:
printf("recievedMessage-size: %d\n", sizeof(recivedMessage));
return;
}
Two more things:
1. sizeof(recivedMessage), it returns the size of a char*, becoz recivedMessage is a char*. It won't return the message size.
2. If you don't use the pointer correctly, it's always result in a crash.
correct;)
Ahh, sorry, what I wrote up there is simplified code just to explain the problem, here's the real deal:
In another function that is called in main():Code:void get_artists(int socket, char * recivedMessage)
{
char *command;
int command_len;
char artist_list_length_c[8];
int artist_list_length;
long rc;
command = "get_artists";
command_len = strlen(command);
if (send(socket, command, command_len, 0) != command_len) {
printf("send() sent a different length of bytes than expected!\n"); // needs real exceptionhandling
return;
}
printf("\n\nsending command get_artists...\n");
if((rc = recv(socket, artist_list_length_c, 8,0)) < 0) {
printf("Error while recieving the artist-list-length; error-code: %s\n",strerror(errno));
return;
}
if(rc==0) {
printf("Client has closed the connection before sending the artist-list-length\n");
return;
}
printf("Recieving artist-list-length\n");
artist_list_length_c[rc]='\0';
artist_list_length = atoi(artist_list_length_c);
printf("Artistlist length: %d\n", artist_list_length);
artist_list_length++;
char buf[artist_list_length];
recivedMessage = (char*)malloc(artist_list_length);
printf("recieving artist-list...\n");
rc = recv(socket,buf,artist_list_length,0);
if(rc==0)
{
printf("Server has closed the connnection before sending the artist-list\n");
return;
}
if(rc==SOCKET_ERROR)
{
printf("Error: recv, error-code: %s\n",strerror(errno));
return;
}
buf[rc]='\0';
strcpy(recivedMessage, buf);
printf("recivedMessage length: %d\n", strlen(recivedMessage));
printf("buf: %s\n", buf);
return;
}
Sorry for the confusionCode:char* artist_list;
get_artists(sock, artist_list);
printf("\n\nget_artists returned: %s\n", artist_list);
-= Double Post =-
Hoever it works using malloc after the initialization and the reallocating it... .
i have some problem when you make array's and let the user put in the value
how can you "+" them
for exemple
note two :Zitat:
int repeat()
{
int totaal , scores[5];
totaal=?????????????????
cout<<"geef de scores van de toetsen\n";
cin>>scores[1]>>scores[2]>>scores[3]>>scores[4]>>scores[5];
cout<<"de gemmidelde score voor deze test is "<<totaal /(sizeof(scores)/sizeof(int));
how cane you let the user put in more numbers for example
the user wants to put in 20 numbers expect 5
Zitat:
int repeat()
{
int totaal , scores[?????];
totaal=??????????;
cout<<"geef de scores van de toetsen\n";
cin>>??????????;
cout<<"de gemmidelde score voor deze test is "<<totaal /(sizeof(scores)/sizeof(int));
i use cout , but dont wory this is something for psp;)
Errm, what do you mean?Zitat:
how can you "+" them
totaal = scores[1] + scores[2] + scores[3] + scores[4] + scores[5];
?
Example:Zitat:
how cane you let the user put in more numbers
Code:int n=0, max=5, z,i;
int *scores=NULL;
scores = (int *)calloc(max, sizeof(int));
printf("enter scores; end with 0\n");
while(1)
{
printf("Enter Score #%d: ", n+1);
scanf("%d", &z);
if(z==0)
break;
if(n >= max) {
max += max;
scores = (int *)realloc(scores,max*sizeof(int));
}
scores[n++] = z;
}
printf("The following scores have been entered ->\n\n");
for(i = 0; i < n; i++)
printf("%d ", scores[i]);
printf("\n");
free(scores);
lol , ofcourse i know that:ROFL:Zitat:
Zitat von Lukeson
but 1) it's a bi work
2) you answered that in question 2 , thnxxx
Now I've got a question:
if you open a file of let's say 200byte and you write 50bytes with fprintf, will it just replace the first 50 bytes an leave the rest as it it (I guess so) or will it replace the file with a new one of 50 bytes? And if you write 300bytes into that file, will there be problems, or will he just create a file of 300bytes then?
Thanks, Luke
PS: What the hell does [WiP] actually mean?
PPS: How do I remove a file? remove(filename)?
That depends whether you choose to truncate the file when you open it. I would suggest reading some about the fopen function.Zitat:
Zitat von Lukeson
WiP = Work in Progress ;)
Not sure if the remove function actually works, but you can use the sdk function sceIoRemove(filepath).
An answer! Thank you!
PS: How do you know so much about the SDK? The only documentation I found sucks and the samples don't include AFAIK no fileIO examples...
The includes (.h) in /usr/local/pspdev/psp/sdk/include have some documentation, like right before the functions it says what it does etc.
i c...
All the documentation are stored in usr/local/pspdev/psp/sdk/doc/html. Then just open main.html. I get the feeling that must stuff is documented there.
You need that doc thingy thing for the pspsdk to install the docs though...
To answer a few off the last posts....
Doxygen is that doc thingy ;)
'remove' or 'unlink' works fine.
The sceIo* functions are in at least one sample, see samples/kernel/fileio for one.
I'd recommend using the newlib variants though over the SDK versions.
hmm, what is newlib? Google doesn't really say much on this
http://en.wikipedia.org/wiki/Newlib
When I say newlib variants, I mean the regular stdio functions, see here:
http://www.cplusplus.com/ref/cstdio/
Can any one tell me what sceAudioOutputPannedBlock ing dose exacly
it is not documented and am currently using sceAudioOutputPanned for audio out, and need to now play longer sample files and was looking at this funtion sceAudioOutputPannedBlock ing and needed to know what it ment exacly by blocking.
Thanks Inso
Another short question, hope I'm not beginning to get on yer nerves:
Can you only free() variables that have been declared using xalloc? And, I'm pretty sure it is the case, but just wanna -know- it - do (non-static) variables get freed automatically at the end of a function?
You can only free allocated memory yes.
Variables declared within a function only exist until that function ends.
ThanksZitat:
You can only free allocated memory yes.
By now I find my question more than embarrassing.Zitat:
Variables declared within a function only exist until that function ends.
hehe i also had a few errors because of variables inside functions hehe
question on fgets: I read a file artists.txt which contains:
using this codeZitat:
1234567890
A perfect Circle
timestamp then contains "1234567890♪"Code:FILE *f = fopen("artists.txt", "r");
fgets(timestamp, 16, f);
fclose (f);
How do I get rid of that last symbol?
PS: I wrote that file using windows, is that symbol probably a fragment of the widows-carriage-return?