Prev: Outlandish Particle Periodic Table update IX
Next: Is it possible that compilers or projects have bugs?
From: none on 27 Dec 2009 22:21 Hello all, This may be dumb, but I'm stuck! The code below is called when a button on the main window is activated. It is supposed to read a text file to the first '\n' and set the labelString of a label box on the same main window. It reads the file ok, but the string set in the label box is the decimal value, not the character in the file. I know that getc() returns an integer, but how do I convert it into a character? Thanks in anticipation. ----------------------------- #include <WScom.h> #include <WSCfunctionList.h> #include <WSCbase.h> #include "WSCvlabel.h" //to access WSCvlabel class //#include "stdio.h" //#include "stdlib.h" //---------------------------------------------------------- //Function for the event procedure //---------------------------------------------------------- extern WSCvlabel* newvlab_000; FILE *file; void butColour(WSCbase* object){ WSCstring in; int ch; file = fopen("stuf.txt", "rt"); object->setProperty(WSNbackColor,"light blue"); //just for fun while((ch = getc(file)) != EOF) { if(ch == '\n') break; in.addString(ch); //put first line of text file into WSCstring in, char by char } fclose(file); newvlab_000->setProperty(WSNlabelString, (WSCstring) in); //in.getString() works too } static WSCfunctionRegister op("butColour",(void*)butColour); -------------------------------
From: Leigh on 28 Dec 2009 02:44 Leigh wrote: > Sorry, my first post was rude, not showing a From Name. I hope this is > better. > > Hello all, > > This may be dumb, but I'm stuck! > > The code below is called when a button on the main window is activated. > It is supposed to read a text file to the first '\n' and set the > labelString of a label box on the same main window. > > It reads the file ok, but the string set in the label box is the decimal > value, not the character in the file. I know that getc() returns an > integer, but how do I convert it into a character? > > Thanks in anticipation. > > ----------------------------- > #include <WScom.h> > #include <WSCfunctionList.h> > #include <WSCbase.h> > #include "WSCvlabel.h" //to access WSCvlabel class > //#include "stdio.h" > //#include "stdlib.h" > //---------------------------------------------------------- > //Function for the event procedure > //---------------------------------------------------------- > extern WSCvlabel* newvlab_000; > FILE *file; > void butColour(WSCbase* object){ > WSCstring in; char temp[1]; > int ch; > file = fopen("stuf.txt", "rt"); > > object->setProperty(WSNbackColor,"light blue"); //just for fun > > while((ch = getc(file)) != EOF) { > if(ch == '\n') > break; sprintf(temp, "%c", ch); > in.addString(ch); //put first line of text file > into WSCstring in, char by char > } > > > fclose(file); > > newvlab_000->setProperty(WSNlabelString, (WSCstring) in); > > } > static WSCfunctionRegister op("butColour",(void*)butColour); > ------------------------------- I have discovered sprintf(). And it works. Yum. Is it a good solution?
From: Ulrich Eckhardt on 2 Jan 2010 17:53 Leigh <""leigh\"@(Leigh)"> wrote: > The code below is called when a button on the main window is activated. > It is supposed to read a text file to the first '\n' and set the > labelString of a label box on the same main window. These are many things that happen together. If one of them fails, the overall operation fails. It is easier to tackle one problem at a time. > It reads the file ok, but the string set in the label box is the decimal > value, not the character in the file. I know that getc() returns an > integer, but how do I convert it into a character? getc() either returns the value of the next char or EOF. In order to correctly convert it to a char, use this code: int n = getc(...); if(n==EOF) ... // handle end of file char c = (char)(unsigned char)n; Note that I'd generally avoid any C-style casts. In C++, I'd use IOStreams instead. > #include <WScom.h> > #include <WSCfunctionList.h> > #include <WSCbase.h> > #include "WSCvlabel.h" //to access WSCvlabel class These all are not part of standard C++, so it's hard for me to comment their use. > FILE *file; > void butColour(WSCbase* object){ > WSCstring in; > int ch; > file = fopen("stuf.txt", "rt"); > > object->setProperty(WSNbackColor,"light blue"); //just for fun > > while((ch = getc(file)) != EOF) { > if(ch == '\n') > break; > in.addString(ch); //put first line of text file > into WSCstring in, char by char > } > > > fclose(file); > > newvlab_000->setProperty(WSNlabelString, (WSCstring) in); > //in.getString() works too > } Why would you declare 'file' a global object? Further, for the sake of all readers, please format your code consistently. Remember that your newsclient might break too long lines. Also, as mentioned above, remove code not necessary to demonstrate the problem. Lastly, why cast 'in' to a WSCstring, which it already is when calling setProperty()? Anyway, back to the problem. The question is what WSCstring::addString() does? I'd guess it tries to add something to the string object, but when called with an 'int', I could imagine it doesn't. There is no way around reading the documentation. Since you don't describe how all this fails, it is also possible that something else is the problem. Uli
From: Leigh on 5 Jan 2010 17:13
Ulrich Eckhardt wrote: > Leigh <""leigh\"@(Leigh)"> wrote: >> The code below is called when a button on the main window is activated. >> It is supposed to read a text file to the first '\n' and set the >> labelString of a label box on the same main window. > > These are many things that happen together. If one of them fails, the > overall operation fails. It is easier to tackle one problem at a time. > >> It reads the file ok, but the string set in the label box is the decimal >> value, not the character in the file. I know that getc() returns an >> integer, but how do I convert it into a character? > > getc() either returns the value of the next char or EOF. In order to > correctly convert it to a char, use this code: > > int n = getc(...); > if(n==EOF) > ... // handle end of file > char c = (char)(unsigned char)n; > > Note that I'd generally avoid any C-style casts. In C++, I'd use IOStreams > instead. > Yes, I have done this now, but I was interested in how to do this in C, so thanks. >> #include <WScom.h> >> #include <WSCfunctionList.h> >> #include <WSCbase.h> >> #include "WSCvlabel.h" //to access WSCvlabel class > > These all are not part of standard C++, so it's hard for me to comment their > use. > >> FILE *file; >> void butColour(WSCbase* object){ >> WSCstring in; >> int ch; >> file = fopen("stuf.txt", "rt"); >> >> object->setProperty(WSNbackColor,"light blue"); //just for fun >> >> while((ch = getc(file)) != EOF) { >> if(ch == '\n') >> break; >> in.addString(ch); //put first line of text file >> into WSCstring in, char by char >> } >> >> >> fclose(file); >> >> newvlab_000->setProperty(WSNlabelString, (WSCstring) in); >> //in.getString() works too >> } > > Why would you declare 'file' a global object? Further, for the sake of all > readers, please format your code consistently. Remember that your newsclient > might break too long lines. Also, as mentioned above, remove code not > necessary to demonstrate the problem. Lastly, why cast 'in' to a WSCstring, > which it already is when calling setProperty()? Sorry about the word wrap. I cast 'in' because I was trying anything to get it to work. I included the whole file here because I was hoping to unearth any programmers familiar with WideStudio, and it isn't very long. > > Anyway, back to the problem. The question is what WSCstring::addString() > does? I'd guess it tries to add something to the string object, but when > called with an 'int', I could imagine it doesn't. There is no way around > reading the documentation. Since you don't describe how all this fails, it > is also possible that something else is the problem. The failure is that I was getting 'int's not 'char's from getc() and didn't know how to convert them. Your assumptions about WSCstring::addString() are correct, of course. As you point out, reading documentation is important, and upon further reading, I discovered sprintf(), which does the conversion nicely. I am used to C Builder (self taught), and lazily used the File object provided. Now I am dabbling in WideStudio, largely for my own amusement. > > Uli > Thanks for the suggestions people. Leigh |