| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
ungetc in c++
|
Original Message
|
Name: ungetc in c++
Date: October 17, 2003 at 19:07:00 Pacific
Subject: ungetc in c++ OS: unix CPU/Ram: ?
|
Comment: hello, sorry that i didn't reply to answers of my previous questions on "random number with probability", since i have finished the coding 2 days after (was feeling tired for a while). so i decided to say thanks this time, the answers did help me a lot =) well, i got another (simple) question this time. i know very little about c++. if i have the following code in c, what is the equivalent to C++ code? char c; c = getc(argv[1]); ... c = ungetc(c, argv[1]); well actually, the question is how to do ungetc in C++. thanks a lot =) wtk
Report Offensive Message For Removal
|
|
Response Number 2
|
Name: egkenny
Date: October 17, 2003 at 20:19:17 Pacific
|
Reply: (edit)With iostreams in C++ you can use the peek function to look at the next character in the stream without extracting it. The following program will first "peek at" the first character in a line and then read the whole line. #include <iostream> #include <fstream> using namespace std; int main(int argc, char* argv[]) { ifstream inp; char ch, buffer[80]; inp.open("test.dat"); ch=inp.peek(); while (!inp.eof()) { inp.getline(buffer,80); cout << ch << " " << buffer << endl; ch=inp.peek(); } inp.close(); return 0; } If the line in the file is: 6New York then the following will be printed: 6 6New York BTW, I did not test this program in unix but it works fine with the latest g++ under Cygwin.
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: wtk
Date: October 17, 2003 at 22:14:04 Pacific
|
Reply: (edit)thanks, got it!! i have added c code in a c++ program, and it compiles and works fine. =) but i know how to use cinpeek() now =) thanks wtk
Report Offensive Follow Up For Removal
|

Post Locked
This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
Go to Programming Forum Home