mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2013-02-16, 04:28   #1
davar55
 
davar55's Avatar
 
May 2004
New York City

102068 Posts
Default end-of-file is the end-of-me-life

What's the end-of-file on Windows 7/8 ? Is it one CTRL-Z or two,
or a NULL, or is it just based on character count? I've tried
reading with read and _read, and unless I'm reading text
I can't get past the first two dozen or so bytes, as in trying to do
a character count on exe files or the like.

(In C, w8 x64 but my compiler is merely 32bit ATM.)
davar55 is offline   Reply With Quote
Old 2013-02-16, 05:19   #2
retina
Undefined
 
retina's Avatar
 
"The unspeakable one"
Jun 2006
My evil lair

2×32×11×29 Posts
Default

The Windows file system knows only the file size. The OS does not treat any character as a special EOF marker. Individual programs might do things differently but the OS won't know or care about it.
retina is online now   Reply With Quote
Old 2013-02-16, 05:30   #3
Dubslow
Basketry That Evening!
 
Dubslow's Avatar
 
"Bunslow the Bold"
Jun 2011
40<A<43 -89<O<-88

3×29×83 Posts
Default

He means what does the shell interpret as EOF, i.e. end of input stream? On GNU-Linux, that's ^D -- hitting that button sends a character (not an ascii character) that is defined as "EOF" in the standard C library (and I imagine in other common language references as well). Upon reading that character, bash terminates.

It has little to do with the OS per se.

Last fiddled with by Dubslow on 2013-02-16 at 05:30
Dubslow is offline   Reply With Quote
Old 2013-02-16, 18:21   #4
davar55
 
davar55's Avatar
 
May 2004
New York City

2×32×5×47 Posts
Default

Well, ok, thanks. But I'm trying to get a character/word count
program to work again, and now on w8 all my programs that
use read or _read can't read a single whole buffer from
any but a text file (the function works fine reading pure text).
Would fread do any better?
davar55 is offline   Reply With Quote
Old 2013-02-18, 11:18   #5
davar55
 
davar55's Avatar
 
May 2004
New York City

2×32×5×47 Posts
Default

I tried fread with second argument "rb" for read+binary,
and still can't read all non-text files past a few bytes.
In particular .exe files.
davar55 is offline   Reply With Quote
Old 2013-02-18, 11:28   #6
axn
 
axn's Avatar
 
Jun 2003

22×52×47 Posts
Default

Quote:
Originally Posted by davar55 View Post
I tried fread with second argument "rb" for read+binary,
and still can't read all non-text files past a few bytes.
In particular .exe files.
If you can, post your code here. It is difficult to diagnose problems using only your descriptions.

Also, what compiler, etc...?

EDIT:- Move to Programming forum?

Last fiddled with by axn on 2013-02-18 at 11:29
axn is offline   Reply With Quote
Old 2013-02-18, 13:03   #7
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by davar55 View Post
What's the end-of-file on Windows 7/8 ? Is it one CTRL-Z or two,
or a NULL, or is it just based on character count? I've tried
reading with read and _read, and unless I'm reading text
I can't get past the first two dozen or so bytes, as in trying to do
a character count on exe files or the like.

(In C, w8 x64 but my compiler is merely 32bit ATM.)

I'm not skilled in computers but if it won't cripple why not use the rename command, rename them as texts files, read then rename to .exe
science_man_88 is offline   Reply With Quote
Old 2013-02-18, 14:17   #8
davar55
 
davar55's Avatar
 
May 2004
New York City

2·32·5·47 Posts
Default

Quote:
Originally Posted by axn View Post
If you can, post your code here. It is difficult to diagnose problems using only your descriptions.
Also, what compiler, etc...?
Here's a code fragment in C (I'm using Borland 5.5.1 compiler on w8):

#include <stdio.h>
#include <io.h>
#define ENDFILE (256) /* outside range -128 to 255 */
#define BUFF (30*1024)
int getb(void); /* function prototype */

void main(void)
{
int n,count = 0;
while ( (n = getb()) != ENDFILE ) count++;
printf("count: %d\n", count);
}

int getb(void)
{
static unsigned char buf[BUFF];
static int bufcnt = 0, top;
int n;
if ( bufcnt == 0 )
{ if ( (top = _read(0,buf,BUFF)) <= 0 ) return(ENDFILE);
bufcnt = top;
}
n = (int)buf[top-bufcnt]; bufcnt--; return(n & 0xFF);
}

/******************/

The indentation gets lost in translation.

This counts the characters in a text file, but
misses most on e.g. an exe file.
davar55 is offline   Reply With Quote
Old 2013-02-19, 20:30   #9
Brian Gladman
 
Brian Gladman's Avatar
 
May 2008
Worcester, United Kingdom

523 Posts
Default

This worked for me on Windows 8 and reported the correct length of both text and binary files using Visual C++:

#include <stdio.h>

int main(void)
{
char buf[1024];
int cnt, total = 0;
FILE *inf;

inf = fopen("x64\\Release\\temp.exe", "rb");
do
{
cnt = fread(buf, 1, 1024, inf);
total += cnt;
}
while
(cnt);
fclose(inf);
printf("The length is %d bytes.\n", total);
}

Last fiddled with by Brian Gladman on 2013-02-19 at 20:37
Brian Gladman is offline   Reply With Quote
Old 2013-02-19, 21:08   #10
davar55
 
davar55's Avatar
 
May 2004
New York City

2×32×5×47 Posts
Default

Yes, I had another code segment using fopen and fread which I
tried on stdin, not an explicit file name.
It worked on text but not bnary, too.
(I.e. not on binary through < or | ).
Thanks, though, I can pass the filename in as an argument.
Not what I need, but it should work.
davar55 is offline   Reply With Quote
Old 2013-02-20, 21:05   #11
davar55
 
davar55's Avatar
 
May 2004
New York City

108616 Posts
Default

Do you know BTW how to pass in the stdin as if (instead of)
a filename? I'd like my program to run as program < file-to-count
or outputofsomething | program.

I tried usiing fread without fopen, using a 0 for stdin. Didn't help,
and isn't portable.
davar55 is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the purpose of my life ? science_man_88 science_man_88 89 2018-05-23 20:39
What is best in life? Is it PARI/GP? JM Montolio A Lounge 11 2018-03-08 06:26
life in a moment cmd cmd 1 2018-02-06 16:29
Universal Life Policies Zeta-Flux Soap Box 16 2012-05-27 04:09
Life after Death! mfgoode Soap Box 92 2010-12-19 19:04

All times are UTC. The time now is 01:39.

Thu Oct 1 01:39:58 UTC 2020 up 20 days, 22:50, 1 user, load averages: 1.37, 1.48, 1.59

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.