Article View: alt.comp.lang.learn.c-c++
Article #789Re: Basic file I/O - flame away!!!
From: Jack Klein
Date: Mon, 12 Jul 1999 00:00
Date: Mon, 12 Jul 1999 00:00
129 lines
4574 bytes
4574 bytes
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:
<37893026.152888474@netnews.worldnet.att.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<3787F989.A3867A44@email.uc.edu>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: fdcad,cea2866abe4eec88 X-Google-Attributes: gidfdcad,public X-Google-Thread: 114592,cea2866abe4eec88 X-Google-Attributes: gid114592,public From: jackklein@att.net (Jack Klein) Subject: Re: Basic file I/O - flame away!!! Date: 1999/07/12 Message-ID: <37893026.152888474@netnews.worldnet.att.net> X-Deja-AN: 499916293 Content-Transfer-Encoding: 8bit References: <3787F989.A3867A44@email.uc.edu> Content-Type: text/plain; charset=ISO-8859-1 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc03.worldnet.att.net 931738300 5412 12.75.161.141 (12 Jul 1999 00:11:40 GMT) Organization: AT&T WorldNet Services Mime-Version: 1.0 NNTP-Posting-Date: 12 Jul 1999 00:11:40 GMT Newsgroups: alt.comp.lang.learn.c-c++,fr.comp.lang.c++