b****t 发帖数: 82 | 1 来自主题: Programming版 - EOF一问 程序
#include "stdio.h"
main()
{
int c;
c=getchar();
while(c!=EOF)
{
putchar(c);
}
}
程序执行的时候,会提示输入字符(char),但是如何能够退出循环?也就是说如何能够
满足c=EOF?我尝试了-1,好像不行。
请大家帮忙看看
谢谢 |
|
r****y 发帖数: 1437 | 2 I think one big problem with IsoMap is how to define
"neighbor". My experience shows the result can be totally
different upto how large you define the "neighbor". As a result,
it makes your interpretation of your result more difficult.
There was a paper on Science recently, talking about this IsoMap
and howt to reduce to a subspace. We tried their code, the "neighbor"
problem still exists.
I also saw one paper dealing with nonlinear EOF with
neural network method, I cannot see th |
|
m********5 发帖数: 17667 | 3 why you use iss.eof()?
The common way to do this is directly check after iss>>temp, because their
can be fail, bad, whitespace, or eof. If either fail/bad signal is True, the
temp is not valid.
###
void test(){
string s = "A B C "; // output is A B C C
// If string s = "A B C", then the output would be A B C
istringstream iss(s);
string temp;
int _i = 0;
while (!iss.eof())
{
cout << _i << " iss stat[good,bad,fail,eof]: [" << iss.good() << iss
.bad() <阅读全帖 |
|
j***y 发帖数: 2074 | 4 在193~194页,书里谈到了一个下面的问题:
---
Signedness of char. In C and C++, it is not specified whether the char data type is signed or unsigned. This can lead to trouble when combining chars and ints, such as in code that calls the int-valued routine getchar(). If you say
? char c; /* should be int */
? c = getchar();
the value of c will be between 0 and 255 if char is unsigned, and between -128 and 127 if char is signed, for the almost universal configuration of 8-bit characters on a two's complement machin... 阅读全帖 |
|
y*****e 发帖数: 712 | 5 题意我有点迷糊,不明白为啥public int read(char[] buf, int n)给出说buf是
destination buffer,
但看了答案之后半懂不懂的写了一个c++的,通过了,是这样的
int read(char *buf, int n) {
bool eof=false;
int readBytes=0;
while(!eof && readBytes<=n)
{
int sz=read4(buf);
if(sz<4) eof=true;
int bytes = min(n - readBytes, sz);
readBytes+=bytes;
buf+=bytes;
}
return readBytes;
}
大概的思路就是每次用read4从buf里读,然后指针往后挪,一直挪到eof或者n reached
,这里的buf应该和... 阅读全帖 |
|
d****n 发帖数: 1637 | 6 差不多用了30分钟
#define BSZ 4096
char buffer[BSZ];//buffer same size as 4096
int curr=BSZ-1; //current position read in buffer
int eof=0; //eof flag
extern char * read4096();
char *getline(){
char *s; //return string
int i, ssz=0; //size of s
for(;;){
if (!eof){
if (curr==(BSZ-1)) {
strcpy(buffer, read4096()); //read API
curr=0;
if (strlen(buffer)<4096 ) e... 阅读全帖 |
|
h****r 发帖数: 68 | 7
Agree. See the revised code and output.
#include
#include
using namespace std;
int main()
{
istringstream sstr;
cout<<"eof state: "<
sstr.str("11 22");
cout<
int i , j;
sstr>>i>>j;
cout<<"i="<
sstr.str("55 66");
cout<<"After calling sstr.str()"<
cout<<"eof state: "<
int k, m;
sstr>>k> |
|
l*******b 发帖数: 2586 | 8 写成state machine得有7种状态,5种字符,还不能handle + -空格。。。
val table = Array(Array(0,0,0,0,0), // 0 false
Array(0,2,0,0,7), // 1 numbers eof
Array(0,2,3,5,7), // 2 numbers e . eof
Array(0,4,0,0,0), // 3 numbers
Array(0,4,0,5,7), // 4 numbers e eof
Array(0,6,0,0,0), // 5 numbers
Array(0,6,0,0,7), // 6 numbers eof
Array(7,7,7,7,7)) // 7 true
def isNumber(s: String) = {
def isNumber_helper(index: Int, stat: Int) :Boolean = {
val t... 阅读全帖 |
|
|
l*******b 发帖数: 2586 | 9 写成state machine得有7种状态,5种字符,还不能handle + -空格。。。
val table = Array(Array(0,0,0,0,0), // 0 false
Array(0,2,0,0,7), // 1 numbers eof
Array(0,2,3,5,7), // 2 numbers e . eof
Array(0,4,0,0,0), // 3 numbers
Array(0,4,0,5,7), // 4 numbers e eof
Array(0,6,0,0,0), // 5 numbers
Array(0,6,0,0,7), // 6 numbers eof
Array(7,7,7,7,7)) // 7 true
def isNumber(s: String) = {
def isNumber_helper(index: Int, stat: Int) :Boolean = {
val t... 阅读全帖 |
|
d**********o 发帖数: 1321 | 10 最终版本的compiler测试结果
=================================================
Output of Building User Code
Explode the tar
c-.l
c-.y
scanType.h
makefile
symtab.h
symtab.cpp
emitCode.h
emitCode.cpp
20131214164956-huang-CS445-F13-A5.tar: POSIX tar archive (GNU)
Tests: directory
c-.l: lex description text
c-.y: lex description text
emitCode.cpp: ASCII C++ program text
emitCode.h: ... 阅读全帖 |
|
d**********o 发帖数: 1321 | 11 最终版本的compiler测试结果
=================================================
Output of Building User Code
Explode the tar
c-.l
c-.y
scanType.h
makefile
symtab.h
symtab.cpp
emitCode.h
emitCode.cpp
20131214164956-huang-CS445-F13-A5.tar: POSIX tar archive (GNU)
Tests: directory
c-.l: lex description text
c-.y: lex description text
emitCode.cpp: ASCII C++ program text
emitCode.h: ... 阅读全帖 |
|
H***a 发帖数: 735 | 12 Np. My understanding is that (for your original code):
eof() or good() checks internal state flags "eofbit", which is modified when
getline() is encountered problem in reading from infile.
When getline() reads the last line, it stops by seeing the EOF, however it
consider this read is successful so won't flip eofbit to TRUE. The file
pointer moves to position of EOF (remember getline() returns istream&)
Since eofbit is still FALSE now, eof() or good() lets it loop over, getline(
) sees the EOF a... 阅读全帖 |
|
f****n 发帖数: 4615 | 13 Using "mt" command to control magnetic tape, there is a command like this:
mt -f /dev/rmt/0n eof 5
It will write 5 EOF marks in magnetic tape.
My question are:
1) where are these 5 EOF marks?
--on the position after the last data set
2) why write these 5 EOF marks?
And if I write 1000 EOF marks before last data set, will last data set
be covered by these marks? |
|
v*****r 发帖数: 1119 | 14 1. 简单的 query, 直接在 query 里dynamic赋值
sqlplus -silent id/password@oracel_instance <
select 'foo='id from table;
...
EOF
2.复杂的 Query ,re-direct here document output to a temporary file and awk
the temporary file
sqlplus -silent id/password@oracel_instance < tempfile
select column1,column2...,columnN from table;
...
EOF
awk .... tempfile
printf( |
|
z*y 发帖数: 1311 | 15 First, when eof bit is set
iss >> temp;
cout << temp << endl;
will always return the last element read, which in this case is C. This may
or may not be a bug depending how you look at it.
When it comes to the last white space, it returns immediately with element C.
But the eof bit is not set yet. Then it calls "iss >> temp" again. Nothing
is read, the last element C is returned (second time), the eof bit is set,
and the while loop quits.
If the last character is C, it returns C and at th... 阅读全帖 |
|
o****o 发帖数: 8077 | 16 you can use hash table to do this:
data have; do i=1 to 1E6; output; end; run;
%macro hashinit;
h = _new_ hash ();
h.defineKey('id');
h.defineData('i');
h.defineDone();
%mend;
data _null_;
declare hash h;
%hashinit;
id=1;
do until (eof);
set have end=eof;
rc=h.add(); id+1;
_numitems=h.num_items;
if h.num_items=100 or eof then do;
if ^eof then _n=int(_numitems/100);
|
|
m*********2 发帖数: 701 | 17 wow, good point.
yea, i think what the author saying is:
EOF == -1
0xFF == 255.
that's why you want to use int.
it's large enough to differentiate whether it's -1 or 255.
the short answer is:
getchar() returns int.
and c is int.
so, you are comparing integers, NOT char.
data type is signed or unsigned. This can lead to trouble when combining
chars and ints, such as in code that calls the int-valued routine
getchar(). If you say
between -128 and 127 if char is signed, for the almost universal
co... 阅读全帖 |
|
w*******s 发帖数: 138 | 18 书上说的是对的
EOF 是 (int)-1
unsigned char c = EOF;
c是0xff
c == EOF // false
(int)c == 255 // 不管是不是sign-extension都一样
char |
|
r****k 发帖数: 21 | 19 #define MAX_BUFFER_SIZE 4096
extern char *read4096();
char buffer[MAX_BUFFER_SIZE+1];
char *readline()
{
static int EOF = 0;
static int currentPos = MAX_BUFFER_SIZE;
char *s = NULL;
int i, ssz = 0;
for(;;)
{
if (!EOF)
{
// buffer is not empty, check buffer
if (currentPos != MAX_BUFFER_SIZE)
{
for ( i = currentPos; i < MAX_BUFFER_SIZE; i++)
if (buffer[i] = '\0' || buffer[i] = '... 阅读全帖 |
|
s********u 发帖数: 1109 | 20 挖个坟,这个readline的题目,搜了一下,好像应该是这个意思吧:
用一个buffer来存字符流,如果中间有换行,那么将这一行返回成一个字符串输出,但
是这个buffer里面的东西继续保留,下次readline()再输出;
如果一行非常长,那么可能需要用到几次read(),来拼出这个完整的line。
/*Implement a function char* readLine(); which returns single lines from a
buffer.
To read the buffer, you can makes use of a function int read(char* buf, int
len) which fills buf with upto len chars and returns the actual number of
chars filled in. Function readLine can be called as many times as desired.
If there is no valid data or newline ter... 阅读全帖 |
|
s********u 发帖数: 1109 | 21 这是readline那个题我参照网上的代码写的。比这个要复杂一些,因为一个line的字符
是不确定的。
#define MAX_LEN 4096
int read(char* buf, int len);
char *readLine(){
static bool EOF = false;
char *str = NULL;
int i, size = 0;
static int currentPos = MAX_LEN;
static char* buffer = new char[MAX_LEN];
while(!EOF || currentPos != MAX_LEN ){
// buffer is not empty, handle buffer first
if(currentPos != MAX_LEN){
for(i = currentPos; i < MAX_LEN; i++)
... 阅读全帖 |
|
s********u 发帖数: 1109 | 22 老题了吧,不过老要写错,尤其是那个read4写read的更恶心。
#define MAX_LEN 4096
int read(char* buf, int len);
char *readLine(){
static bool EOF = false;
char *str = NULL;
int i, size = 0;
static int currentPos = MAX_LEN;
static char* buffer = new char[MAX_LEN];
while(!EOF || currentPos != MAX_LEN ){
// buffer is not empty, handle buffer first
if(currentPos != MAX_LEN){
for(i = currentPos; i < MAX_LEN; i++)
if( buffer[i] ==... 阅读全帖 |
|
j***3 发帖数: 142 | 23 谢谢ggplay, 是不是这样就可以了。另外我再想办法处理下input file format。
另外如果input file有几行不止一个new line或者空格或者制表符导致通过 != EOF的判断之后indexMinus出错的话,为
什么在window size 小于行字符数时,没有 indexMinus出错报告呢?
if ( ch == EOF ) break;
if ( ch == '\n' )
{
ch = fgetc(in);
if ( ch == EOF ) break;
ungetc(ch, in);
} |
|
t****t 发帖数: 6806 | 24 checked standard. if EOF happens in the middle of a line, you see eofbit
alone. if EOF happens immediately after a newline, a sentry object is
constructed at that time and traits::eof() is returned. and sentry object
set eofbit|failbit.
i think usually badbit alone should be the exception condition. failbit can
happen quite easily. |
|
r****y 发帖数: 1437 | 25 SVD has something related to EOF, or called PCA, an very essential
statistical tool in data analysis.
Give SVD of matrix A
A = ULV
A*A = V*(L^2)V where * denotes adjoint, and L is diagonal matrix.
A*A is usually called covariance matrix of A, and the normalized
eigenvectors of A*A are called EOF (empirical othrogonal
functions). So far, EOF, or in another name, principal
component analysis, is very powerful a |
|
o****o 发帖数: 8077 | 26 没有任何结果in your log, but check out the work library
data have; do i=1 to 1E6; output; end; run;
%macro hashinit;
h = _new_ hash ();
h.defineKey('id');
h.defineData('i');
h.defineDone();
%mend;
options nonotes nosource;
data _null_;
declare hash h;
%hashinit;
id=1;
do until (eof);
set have end=eof;
rc=h.add(); id+1;
_numitems=h.num_items;
if h.num_items=100 or eof then do;
if ^eo |
|
h****n 发帖数: 1093 | 27 17:12
1.
vector > FindAllPairs(vector input, int givenSum)
{
vector > res;
vector oneRes;
if(input.size()<1) return res;
int i = 0; j = input.size()-1;
while(i
{
if(input[i]+input[j]
else if(input[i]+input[j]>givenSum) j--;
else
{
oneRes.clear();
oneRes.push_back(input[i]);
oneRes.push_back(input[j]);
if(find(res.begin(), res.end(), oneRes)==res.en... 阅读全帖 |
|
C***U 发帖数: 2406 | 28 #include
#include
#include
#define MAX 10000
int main(int argc, char *argv[]){
std::vector numbers1;
std::vector numbers2;
int k = atoi(argv[3]);
if(argc < 4)
std::cout << "we need 2 files and a number." << std::endl;
std::cout << "we need find " << k << "th number" << std::endl;
std::cout << "reading first array of numbers ..." << std::endl;
std::ifstream f1(argv[1]);
if(!f1){
std::cout << "cannot open... 阅读全帖 |
|
l*******b 发帖数: 2586 | 29 1: 第一个字符,可能是 number . space sign
2: 读取一个数字但未读取 . 的时候,下一个字符可以是 number . e space eof
读取到number保持状态
读取到 . 进入 5
读取到e 进入 6
读取到 space 或者 eof结尾
3: 读取到一个 . 而且之前没有读到数字,此时下一个字符必须为数字
4: 读取到第一个字符为 sign 下一个字符可以是 . 或者number,规约到情形2,3
5: 读取到一个 . 以后下一个字符可以是number e space
下一个如果是number, 则状态保持,读到e则进入下一阶段 6,读到space就扫结尾
9
6: e 已经读取,下一个可以是number sign
是number则进入 7 再下一个为number 或者space进入9扫描结尾
是sign 则必须进入 8 再读取一个数字,然后规约到 7
9: 扫描结尾的 space |
|
l*******b 发帖数: 2586 | 30 1: 第一个字符,可能是 number . space sign
2: 读取一个数字但未读取 . 的时候,下一个字符可以是 number . e space eof
读取到number保持状态
读取到 . 进入 5
读取到e 进入 6
读取到 space 或者 eof结尾
3: 读取到一个 . 而且之前没有读到数字,此时下一个字符必须为数字
4: 读取到第一个字符为 sign 下一个字符可以是 . 或者number,规约到情形2,3
5: 读取到一个 . 以后下一个字符可以是number e space
下一个如果是number, 则状态保持,读到e则进入下一阶段 6,读到space就扫结尾
9
6: e 已经读取,下一个可以是number sign
是number则进入 7 再下一个为number 或者space进入9扫描结尾
是sign 则必须进入 8 再读取一个数字,然后规约到 7
9: 扫描结尾的 space |
|
s*****b 发帖数: 8 | 31 我来贴一个。
Rocket fule (Software Engineer - Machine Learning Scientist ) 技术电面后code
test. code通过了所有test cases. 人家看过code 后就拒了。问题在哪里呢?请各位
牛人不吝赐教。题目本版以前贴过
You are standing in a rectangular room and are about to fire a laser toward
the east wall. Inside the room a certain number of prisms have been placed.
They will alter the direction of the laser beam if it hits them. There
are north-facing, east-facing, west-facing, and south-facing prisms. If the
laser beam strikes an east-facing prism, its cours... 阅读全帖 |
|
w*******e 发帖数: 395 | 32 你的代码好像有问题。
如果read()没有读满max_size, eof已经为1了,这个时候buffer是没有填满的。在此之
前如果hit了一个‘n’。currentPos会记录'n‘后面的一个位置。
当第二次进入该函数的时候,你的while循环会从currentPos一直都到buffer的尾端,
而实际上,由于上次hit了eof,buffer是没有填满的。你的代码读入了一些多余的数据。 |
|
|
S*******0 发帖数: 208 | 34 Can someone help me with the code, simply put is : find a cell in excel, and
copy the sql reusult dataset starting from the cell found, my code is as
follows, it did not indicate any error, but could not run, please help me to
identify where the problems, many thanks
Sub reshnepoolmonthValues()
Worksheets(2).Activate
Dim valuedt As String
Dim sql1 As String
Dim sql2 As String
Dim sql3 As String
Dim wrk As Worksheet
Dim inRng1 As Long
Dim inrng2 As Long
D... 阅读全帖 |
|
f****u 发帖数: 926 | 35 保存成.bat文件运行即可。自己改文件路径吧:
del C.txt
for /F %%i in (A.txt) do call :__FindKeyword %%i
goto :EOF
:__FindKeyword
call findstr %1 B.txt
if "%errorlevel%" == "0" echo %1>>C.txt
goto :EOF |
|
i*****l 发帖数: 10 | 36 So I created two files: list.pl and email.txt
For list.pl , I just copied whatever you wrote as following:
For email.txt , I just wrote two lines as you mentioned below:
Then I ran this under unix:
~~~~~~~~~~~~~~~~~~~~~~~How?
Then I got a message:
Can't find string terminator "EOF" anywhere before EOF at list.pl line 21.
What do I do now? |
|
t******g 发帖数: 10390 | 37 我看了一下,可能是你的EOF(倒数第二行,}上面那一行),结尾的地方有个空格.
把这个空格删了,EOF标记才能被认出来.
这个是我们通过浏览器传递文件出的问题.
关于打印进度,你可以参考这个程序:
#!/usr/bin/perl
$mailprog='/usr/lib/sendmail -t';
$i=0;
#打开邮件列表文件.
open (FILE, "email.txt");
@file=;
close (FILE);
#分解邮件列表成名字和地址.
foreach $file_line(@file)
{ ($name, $emailaddress)=split(/\|/, $file_line);
&sendpass;
}
#发信
sub sendpass {
open (MAIL,"| $mailprog") || die "Mail system error";
print MAIL "to: $emailaddress";
print MAIL "Subject: 标题换成自己的\n";
print MAIL "From: xxx\@hotmail.com\ |
|
z********i 发帖数: 60 | 38 读数据从文件中,
input=fopen(filename,"rb");
while ( (c=fgetc(input)) != EOF )
{
}
可是数据中有0xff, 255(int)好像这个被当成eof了,请问有什末办法读出来吗? |
|
Q**g 发帖数: 183 | 39 what's the data type of variable c? if it's int, the code should be fine.
if it's char, you may get a problem coz you can not differentiate EOF
and 0xff. the following paragraph is extracted from GNU C library:
These functions return an int or wint_t value (for narrow and wide stream
functions respectively) that is either a character of input, or the special
value EOF/WEOF (usually -1). For the narrow stream functions it is
important to store the result of these functions in a variable of type i |
|
y***d 发帖数: 2330 | 40 cat << EOF > test.txt
==$stasc==
EOF |
|
b*****l 发帖数: 9499 | 41 在 gnome-terminal 上是:eof = ^D; eol = M-^?; eol2 = M-^?;
在 xterm 上是:eof = ^D; eol = ; eol2 = ;
个毛 |
|
l*******G 发帖数: 1191 | 42 put the following in install.sh
#!/bin/bash
#----start----
#prepare answers here
answer1="/user/foo"
answer2="/user/bar"
password="test"
echo "starting program "
program <
$answer1
$answer2
$password
$password
EOF
echo "finished program"
#----end-------
and then
chmod +x install.sh
./install.sh |
|
w*m 发帖数: 1806 | 43 for example,
#/!bin/csh
sqlplus -silent id/password@oracel_instance <
"%s",$1;}' | echo(I want to set xx=..)
select id from table;
EOF
awk处理后的结果,如何赋值给一个变量呢? |
|
m****0 发帖数: 2236 | 44 It works, but I got lost.
max@desktop ~ $ tcsh
desktop:~> mkdir test\ space
desktop:~> echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr
/local/games
desktop:~> cd test\ space/
desktop:~/test space> cat > testrun.sh << EOF
? #!/usr/bin/tcsh
? echo "running tcsh!"
? EOF
desktop:~/test space> chmod +x testrun.sh
desktop:~/test space> cd ..
desktop:~> setenv PATH "/home/max/test space":$PATH
desktop:~> echo $PATH
/home/max/test space:/usr/local/sbin:/usr/local/... 阅读全帖 |
|
c*****t 发帖数: 1879 | 45 不是,^Z 在 pc 上是 eof,并不 terminate 程序。类似的,^D 在 unix 上
也是 eof 。 |
|
t****t 发帖数: 6806 | 46 来自主题: Programming版 - 紧急求助 for adding "%"
perl -pi -e 'print "%" if (1 .. 40); close ARGV if eof' your_files
for removing
perl -ni -e 'print unless ( 1 .. 40); close ARGV if eof' your_files |
|
g****y 发帖数: 436 | 47 你的sample和这个程序都有bug。
从程序来看,如果看到一个new line,它会再读入一个ch,判定是否EOF,但是判定完了
以后,没有用
ugetc()
把判定用字符推回去,然后马上进入下一个循环,又读了一次stream,相当于又丢失了
一个字符。
因此这个程序最终的结果基本是错误的。
另外一方面,可以断定你的sample file有几行不止一个new line或者空格或者制表符,
导致通过 != EOF的判断之后,indexMinus出错。 |
|
j******n 发帖数: 271 | 48 If the current tool is not sufficient, find another tool to help. An ugly
but working solution is to add some scripts to your code.
e. g.
// a.h.in:
#include "more.h"
// my macros
`echo "#define MACRO_0(f) f(0)"; i=0; while [ $i -lt 10 ] ; do j=$((i + 1));
echo "#define MACRO_$j(f) MACRO_$i(f); f($j)" ; i=$j; done`
// Makefile:
a.h: a.h.in
cat a.h.in | (echo "cat - < a.h
would generate your macros in a.h:
#include "more.h"
// my macros
#define MACRO_0 f(0)
#d |
|
c**********e 发帖数: 2007 | 49 #include
#include
#include
#include
using namespace std;
int main()
{
std::string line1(3, '*'), line2(5, '*');
std::cin >> line1;
std::getline(std::cin, line2);
std::cout << "Line 1:" << line1 << std::endl;
std::cout << "Line 2:" << line2 << std::endl;
std::cout << "EOF\n";
return 1;
}
Given the program above, what happens if, when the program is run, the user
enters "Hello world!" followed by a newline?
a) It will print out:
Line 1: Hello
Line 2: world!
EOF |
|
t****t 发帖数: 6806 | 50 no, most ppl don't need that. just know istream and ostream and their
variations is enough (istream is basic_istream, similar for ostream).
their variations include ifstream/ofstream, istringstream/ostringstream.
just know op<< for output, op>> for input, op! (usually you need that
instead of eof() member) to check eof, clear() for opening another file,
manipulators (such as setw(), endl, hex, etc.) for formating, and getline()
for reading a whole line. these are enough for 90% of ppl deal... 阅读全帖 |
|