![]() |
|
#1
|
|||
|
|||
Strange behaviour in printf (interesting)Hi,
I was doing one of my lab and had this very strange bug. It's a very simple program, it processes 2 commands namely "prompt <sth>" which changes the prompt to <sth> and "exit 0" which exits. (The program isn;t as long as u think Here's the code: CPP / C++ / C Code:
There're 2 modes of entering input (1) File (2)stdin. The file contains 2 lines : prompt bash exit 0 to run simply use: > ./a.out <filename> //i attached test.txt for <filename> NOTE: You can ignore the before buffer, after buffer thingy that just shows that strtok() messes with the input string. Here're the STRANGE problems: You'll notice i printed the prompt twice using puts and printf. The STRANGE thing is after the command "prompt bash" is read, puts displays bash BUT printf displays NULL ??? I'd be very grateful if anyone can explain this STRANGE behaviours. Last edited by JdS : 05-Apr-2004 at 05:11.
Reason: fixed missing closing C BBCode
|
|||
|
#2
|
||||
|
||||
|
The tags don't work because you were holding the shift key when you typed the ending bracket, causing you to use a brace instead of a bracket.
__________________
-Aaron |
|
#3
|
||||
|
||||
|
Quote:
Quote:
Quote:
Quote:
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#4
|
|||
|
|||
|
Hi WaltP,
Sorry about that, I've given a much shorter program below that is Windows compliant I hope. Please run it as ./a.out test1.txt (attached below) Quote:
CPP / C++ / C Code:
You'll notice the difference removing and adding the line puts("pass"); |
|
#5
|
|||
|
|||
|
Here's a suggestion: make your debug printf()s more informative. For example
CPP / C++ / C Code:
and CPP / C++ / C Code:
Notice that the angle brackets '<' and '>' delineate exactly what the heck the string is. All together, now: "printf() is your friend" Dave (There is nothing mysterious here, you just didn't see what you were looking for. I don't think that I've ever seen printf() get confused; just me, from time to time.) |
|
#6
|
||||
|
||||
|
Hi nusstu.
I am not 100% sure what is going on. If I change the printf statement to: CPP / C++ / C Code:
I think the root of the problem is strtok. It is not a highly recommended function and I don't think that I would trust what is in local_buffer after a call to strtok. Here, again, are some choice quotes about strtok: Quote:
Quote:
If you don't want to write your own parser that is fine, but make sure that you know what you are getting when you use strtok. Basically after the first call to strtok, your local_buffer variable should not be used again. HTH d |
|
#7
|
|||
|
|||
|
Hi dsmith,
I know that adding "\n" will print the stuff to screen because printf is line buffered. But, shouldn't it still print if i used fflush(stdout); The point is I don't want it to go to the next line. Here's an updated version of the program using fflush(stdout): CPP / C++ / C Code:
|
|
#8
|
||||
|
||||
|
I think your problem is: \r.
Add this charecter to your seps string, ala: CPP / C++ / C Code:
It appears that your text file was created in windows, which adds the \r symbol. C is not particularly fond of this character. The problem was occuring in your changePrompt function by the way. It may be that upon recieving the return charecter printf awaits for the newline charecter to print the line (otherwise it would overwrite the original line) until printing. Just a thought. HTH, d |
|
#9
|
|||
|
|||
|
Hi dsmith,
Thanks, I feel relieved now Quote:
hm..yes, I agree with you. Somehow, the printf() statement in changePrompt seems to be overwriting stdout for some bizarre reason upon reading /r. I think, the reason why it doesn't overwrite after it reads \n, is because stdout is flushed after newline. This makes me wonder also why is it fflush(stdout) doesn't flush stdout onto the screen? P.S: I just started C a few weeks ago and already the future looks bleak. |
|
#10
|
||||
|
||||
|
Quote:
Quote:
I hope thats not true . I probably have as much of a love/hate relationship with C as anyone. But since I first learned it in 1986, I haven't found any other language that I can understand better or would rather use... |
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Having trouble with Galaga-game | DWk | C Programming Language | 49 | 17-Feb-2005 09:10 |
| Can somebody look at this and point out any errors to me | soulfly | C Programming Language | 7 | 31-Mar-2004 10:45 |
| Dining Philosophers program | Sam | C Programming Language | 9 | 19-Feb-2004 08:46 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The