Article View: alt.comp.lang.learn.c-c++
Article #650Re: FILE READ PROBLEM
From: Jeff Elliott
Date: Sat, 15 Nov 1997 00:00
Date: Sat, 15 Nov 1997 00:00
48 lines
2547 bytes
2547 bytes
Greg,
I downloaded your source and compiled it in MSVC++ 5.0, and it seemed to
compile just fine. I don't know of a way to "rewind" input from a file
redirected at the command-line -- if that's your question then I think
you might want to consider an alternative to command-line redirection.
Here are two:
1) Scan the command-line for a given filename, and display an error
message (and terminate) if the user doesn't enter a filename. Use:
int main(int argc, char* argv[])
and look for argc>1 and strcpy(filename, argv[1]), then do an fopen() on
filename. You can use fgetc() to read a single character at a time out
of the file, and fseek() or rewind() to move the file pointer around
within the file.
2) Prompt the user for a filename at run-time, then open and process
that file as above.
However, the main reason I'm writing back is that I got an "Invalid page
fault" error and the program crashed when I tried to run it. A quick
look at the code shows me that you're only handling up to five different
characters, not the entire alphabet! The culprit seems to be the line
char CharArray[] = {0,0,0,0,0};
....and the loop reading until end-of-file, incrementing the 'i' index
counter of CharArray[] without regard to the size of that array.
The program works fine as long as I've got five characters or less in
the file I'm redirecting, but it can't count all possible letters a file
could contain (a-z, A-Z).
I found this by redirecting the source code file right into the program;
give that a shot and you'll see what I mean.
I'd suggest an array of either 26 or 52 characters to store the
character count for the entire alphabet.
Jeff
--
Please direct return correspondence to: mailto://cguru@ibm.net
--
comp.lang.c.moderated - clcm@plethora.net
Technical Details
Message-ID:
<clcm-19971115-0004@plethora.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<clcm-19971114-0010@plethora.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fab7a,75d4be5da465827f X-Google-Attributes: gidfab7a,public X-Google-Thread: 100850,75d4be5da465827f X-Google-Attributes: gid100850,public X-Google-Thread: 114592,75d4be5da465827f X-Google-Attributes: gid114592,public From: Jeff Elliott <cguru@ibm.net> Subject: Re: FILE READ PROBLEM Date: 1997/11/15 Message-ID: <clcm-19971115-0004@plethora.net> X-Deja-AN: 289752148 References: <clcm-19971114-0010@plethora.net> Organization: (missing) NNTP-Posting-Date: 15 Nov 1997 12:12:12 CST Reply-To: cguru@ibm.net Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c.moderated,ctdl.lang.c Originator: clcm@plethora.net (comp.lang.c.moderated)