![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
BrunoC12
Joined: 25 May 2017 Posts: 4
|
Problem using printf to print string variables |
Posted: Thu May 25, 2017 7:56 pm |
|
|
I am using strtok to separate any string. Then, I save the different values in a string variable. Up to this point the program is fine.
After that, I print all the values but I can not see that. The program prints three of the four values.
I'll pass the code, any opinions will be very helpful. Thanks
Code: |
#include <string.h>
#include <stdio.h>
char ID[], ST[], OR[], AK[];
void main()
{
char string[30], term[4], *ptr;
int i;
strcpy(string,"one,two,three,four");
strcpy(term,",;");
i=0;
ptr = strtok(string, term);
while(ptr!=0) {
i++;
puts(ptr);
if(i==1)ID=ptr;
if(i==2)ST=ptr;
if(i==3)OR=ptr;
if(i==4)AK=ptr;
ptr = strtok(0, term);
}
i=0;
printf("%s \n\r %s \n\r %s \n\r %s \n\r ",ID, ST, OR, AK);
} |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 26, 2017 1:12 am |
|
|
It works for me in MPLAB vs. 8.92 simulator with vs. 5.071. Of course, I
put a while(TRUE) at the end of the program, to prevent it from going
to sleep while it's still transmitting characters. Here is the output:
Quote: |
one
two
three
four
one
two
three
four
|
If it still doesn't work after you add a while(TRUE) statement at the end
of main(), then post a compilable test program, similar to what is shown
below:
Code: |
#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
#include <string.h>
#include <stdio.h>
char ID[], ST[], OR[], AK[];
//=======================================
void main()
{
char string[30], term[4], *ptr;
int i;
strcpy(string,"one,two,three,four");
strcpy(term,",;");
i=0;
ptr = strtok(string, term);
while(ptr!=0)
{
i++;
puts(ptr);
if(i==1)ID=ptr;
if(i==2)ST=ptr;
if(i==3)OR=ptr;
if(i==4)AK=ptr;
ptr = strtok(0, term);
}
i=0;
printf("%s\r%s\r%s\r%s\r", ID, ST, OR, AK);
while(TRUE); // Prevent PIC from going to sleep
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19385
|
|
Posted: Fri May 26, 2017 7:03 am |
|
|
He launched two separate threads at the same time about this.... |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 26, 2017 7:33 am |
|
|
Yes, I saw that. We posted only 3 minutes apart, and your post wasn't
visible to me at the time I posted. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19385
|
|
Posted: Fri May 26, 2017 10:32 am |
|
|
Happens...
Usually, one starts to reply and by the time one has typed the reply another answer has appeared!...
Perhaps one of the moderators could 'units' the threads?. |
|
![](templates/subSilver/images/spacer.gif) |
BrunoC12
Joined: 25 May 2017 Posts: 4
|
|
Posted: Fri May 26, 2017 10:43 am |
|
|
I tried with while(true), but the program send the same answer.
Quote: |
one
two
three
four
one
two
three
I think the problem is that it doesn't save the last data in the variable AK because it prints an empty space at the end, but I do not know why |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 26, 2017 11:03 am |
|
|
If you read my post, I said:
Quote: |
If it still doesn't work after you add a while(TRUE) statement at the end
of main(), then post a compilable test program, similar to what is shown below:
#include <18F46K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOPBADEN
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
#include <string.h>
#include <stdio.h>
char ID[], ST[], OR[], AK[];
etc.
|
Also post your compiler version. |
|
![](templates/subSilver/images/spacer.gif) |
BrunoC12
Joined: 25 May 2017 Posts: 4
|
|
Posted: Fri May 26, 2017 1:38 pm |
|
|
Oh, sorry, I had not considered it. With that code the program works correctly, but I need to do it with the 16F628a.
I'm going to try MPLAB to see if it works.
Thanks for your help.
By the way, I use PCWHD Compiler Version 5.015. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19385
|
|
Posted: Sat May 27, 2017 12:58 am |
|
|
So, post us the code that doesn't work in the 628A.
Your original code doesn't show the processor stuff. PCM_programmer has posted code that works, so now show us similar code that doesn't.
I've taken PCM_Programmer's code, changed the header to:
Code: |
#include <16F628A.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT, NOMCLR //no MCLR needed for my board
#use delay(internal=4M)
#use rs232(baud=9600, UART1, ERRORS)
|
and it runs fine on a 628A. Prints:
Quote: |
one
two
three
four
|
on hyperterminal.
and just amended to say that the test was with 5.015. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|