Article View: alt.comp.lang.learn.c-c++
Article #787Re: Basic file I/O - flame away!!!
From: Bill Barnett
Date: Wed, 14 Jul 1999 00:00
Date: Wed, 14 Jul 1999 00:00
144 lines
6035 bytes
6035 bytes
Thanks for you "candid" critique, Jack! As I'm sure my code has
demonstrated I am a beginning C++ programmer and actually have very
little other programming experience, except about a year of Ada and
various dialects of BASIC. You did, however, totally avoid my problem.
Nonetheless, I found the trouble, with the aid of another contributor,
to be my use of the "fin.peek()" function. I do not mind at all having
experienced programmers, such as yourself, rip my code apart as it is a
wonderful way to fill the gaps in my formal training. I would love to
show you some of my other "gems"!!!
Thanks again,
Bill Barnett
Jack Klein wrote:
>
> On Sat, 10 Jul 1999 21:55:21 -0400, Bill Barnett
> <barnetwc@email.uc.edu> wrote in alt.comp.lang.learn.c-c++:
>
> Ugh. A lot of silliness here...
>
> > Before you diehard flamers get started, I've read FAQ [15.4] and
> > understand the principal (I thought) but have still got the same
> > problem. I'm reading data in from a text file using the code below and
> > still get stuck in an infinite loop. I'm really not as dumb as this
> > dilemma would indicate!!! Any thoughts about this problem or my future
> > as a programmer are welcome.
> >
> > Bill Barnett
> > "Unrepentant Ada programmer"
> > barnetwc@email.uc.edu
> > http://homepages.uc.edu/~barnetwc
> > Je parle un peu fran๏ฟฝais! - Anglophones go here -> translator.go.com
> >
> > Check out the program specification if you like here;
> > http://www.cba.uc.edu/faculty/rokeyrs/cs321assignment2.htm
> > As you can see I've got a ways to go...
> >
> >
> > #include <string>
>
> Surely you meant <string.h> here? The ANSI/ISO C++ header <string> is
> completely different and does not provide a prototype for strcpy().
>
> > #include <iostream.h>
> > #include <fstream.h>
> > #include <stdio.h>
> #include <ctype.h>
>
> > void main()
>
> If you write "void main()" then it is not C or C++. Both languages
> require that main be returned with a return type of int.
>
> int main()
>
> > {
> >
> > char F_Name[16];
> > char L_Name[16];
> > char C_Name[22];
> > char ch;
> >
> > ifstream fin;
> > ofstream echo = "echo.txt";
> >
> > fin.open("student.dat");
>
> Where's the checking for a successful open?
>
> > do // get file data
> > {
> > do // get line data
> > {
> > char ClassTitle[22];
> > fin >> F_Name >> L_Name;
>
> What happens if there are 100 characters without a white space in this
> file? What happens when those 100 characters are written into your 16
> character array? If you guessed undefined behavior and probably a
> nasty program crash, you are right.
>
> > // Correct capitalization
> > for (int i = 0; i < 16; i++)
> > {
> // if ((F_Name[i] >= 'A') && (F_Name[i] <= 'Z'))
> // F_Name[i] = F_Name[i] + 32;
>
> Extremely bad because: not all character sets are continuous without
> holes, not all character sets exhibit the property that adding 32 to
> the numeric value of an upper case character changes it to a lower
> case character. Why go through all of this trouble when there is a
> perfectly good standard library function guaranteed to do the job
> correctly in any execution character set and any language? This is
> why I included <ctype.h>.
>
> F_Name [i] = tolower((unsigned char)F_Name [i]);
>
> Absolutely, positively guaranteed to generate the correct lower case
> character if and only if the argument is an upper case alpha character
> for which a valid lower case character exists, and return its argument
> unchanged otherwise.
>
> > }
> > F_Name[0] = F_Name[0] - 32;
>
> This is just plain silly, you should have just used toupper() on the
> first character.
>
> > for (int j = 0; j < 16; j++)
> > {
> > if ((L_Name[j] > 'A') && (L_Name[j] < 'Z'))
> > L_Name[j] = L_Name[j] + 32;
> > }
> > L_Name[0] = L_Name[0] - 32;
> >
> > fin.get(ClassTitle, 22);
> > strcpy(C_Name, ClassTitle);
>
> What is the point of inputting into ClassTitle instead of directly
> into C_Name? Makes no sense.
>
> > cout << L_Name << ", " << F_Name << " - " << C_Name << "\n";
> > echo << L_Name << ", " << F_Name << " - " << C_Name << "\n";
> > }
> > while (!fin.peek()== '\0');
> > }
> > while (fin >> ch);
> > fin.close();
> > }
> >
> > The data file is;
> >
> > zongjun zhang Data Structures
> >
> > Marti Morthorst Data Structures
> >
> > Anne Maseck Data Structures
> >
> > SRIHARI KORTIKERE Programming for IS I
> >
> > Jason Wilson Data Structures
> >
> > Rhonda Rains Data Structures
Technical Details
Message-ID:
<378C1850.82765E1B@email.uc.edu>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3787F989.A3867A44@email.uc.edu> <37893026.152888474@netnews.worldnet.att.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 114592,cea2866abe4eec88 X-Google-Attributes: gid114592,public X-Google-Thread: fdcad,cea2866abe4eec88 X-Google-Attributes: gidfdcad,public From: Bill Barnett <barnetwc@email.uc.edu> Subject: Re: Basic file I/O - flame away!!! Date: 1999/07/14 Message-ID: <378C1850.82765E1B@email.uc.edu> X-Deja-AN: 500805187 Content-Transfer-Encoding: 8bit References: <3787F989.A3867A44@email.uc.edu> <37893026.152888474@netnews.worldnet.att.net> X-Accept-Language: en Content-Type: text/plain; charset=iso-8859-1 X-Complaints-To: newsabuse@remarQ.com X-Trace: 931928309.553.41 FC4FGIW.C7860D844C qube-02.us-ca.remarq.com Organization: No thanks! MIME-Version: 1.0 NNTP-Posting-Date: Wed, 14 Jul 1999 04:58:29 GMT Newsgroups: alt.comp.lang.learn.c-c++,fr.comp.lang.c++