|
From: Marco R. <ma...@ne...> - 2004-06-26 13:22:09
|
Sorry Eric, but I think the propose of this mailing list is not that. I
mean, my idea of it is every one should try to help the way it's =
possible.
Well I have always two options:
1) Try to Help
2) See the mail and not help (even if I know the answer or at least a =
clue
for it)
But the best part is this ;) You have two options too:
1) See the mail and go to the link
2) See the mail and discard ;)=20
Well but I understand your point, so here is the text you would find in =
that
page:
NOTE: I DID A COPY PASTE OF THE PAGE, SOME LINES MIGHT BE IN DIFFERENT
ORDER!!!!=20
Hope it helps...=20
C++ Notes: Reading Text Files
Reading a text file is very easy using an ifstream (input file stream).=20
Include the necessary headers.=20
#include <fstream>
using namespace std;
Declare an input file stream (ifstream) variable. For example,=20
ifstream inFile;
Open the file stream. Path names in MS Windows use backslashes (\). =
Because
the backslash is also the string escape character, it must be doubled. =
If
the full path is not given, most systems will look in the directory that
contains the object program. For example,=20
inFile.open("C:\\temp\\datafile.txt");
Check that the file was opened. For example, the open fails if the file
doesn't exist, or if it can't be read because another program is writing =
it.
A failure can be detected with code like that below using the ! (logical
not) operator:=20
if (!inFile) {
cerr << "Unable to open file datafile.txt";
exit(1); // call system to stop
}
Read from the stream in the same way as cin. For example,=20
while (inFile >> x) {
sum =3D sum + x;
}
Close the input stream. Closing is essential for output streams to be =
sure
all information has been written to the disk, but is also good practice =
for
input streams to release system resources and make the file available =
for
other programs that might need to write it.=20
inFile.close();
Example
The following program reads integers from a file and prints their sum. =
1=20
2=20
3=20
4=20
5=20
6=20
7=20
8=20
9=20
10=20
11=20
12=20
13=20
14=20
15=20
16=20
17=20
18=20
19=20
20=20
21=20
22=20
23=20
24=20
25=20
26=20
27=20
// io/read-file-sum.cpp - Read integers from file and print sum.
// Fred Swartz 2003-08-20
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
int sum =3D 0;
int x;
ifstream inFile;
=20
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
=20
while (inFile >> x) {
sum =3D sum + x;
}
=20
inFile.close();
cout << "Sum =3D " << sum << endl;=20
return 0;
}
The text from the above example can be selected, copied, and pasted into =
an
editor. =20
Copyleft 2000-3 Fred Swartz Last update 2003-09-25, URL=3Dundefined
-----Original Message-----
From: Eric [mailto:eri...@cl...]=20
Sent: s=E1bado, 26 de Junho de 2004 10:17
To: Marco Ribeiro; 'Noorez Kassam'
Cc: dev...@li...
Subject: Re: [Dev-C++] file handling
COULD YOU NOT INCLUDE THE PAGE WITHIN THE EMAIL
SOME PEOPLE LOG ON, GET THE EMAIL AND LOG OFF.
IT IS VERY ANNOYING WHEN YOU "GO TO THIS URL TO FIND YOUR ANSWER"
THANKS FROM ERIC
----- Original Message -----
From: "Marco Ribeiro" <ma...@ne...>
To: "'Noorez Kassam'" <coo...@ho...>
Cc: <dev...@li...>
Sent: Wednesday, June 30, 2004 11:48 AM
Subject: RE: [Dev-C++] file handling
Sorry about the last post :)
The link
http://www.fredosaurus.com/notes-cpp/io/readtextfile.html
bye.
Marco
-----Original Message-----
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Noorez
Kassam
Sent: sexta-feira, 25 de Junho de 2004 22:09
To: dev...@li...
Subject: [Dev-C++] file handling
How can I get integers from a file. The get() method only seems to work
with
chars.
_________________________________________________________________
STOP MORE SPAM with the MSN Premium and get 2 months FREE*
http://join.msn.com/?pgmarket=3Den-ca&page=3Dbyoa/prem&xAPID=3D1994&DI=3D=
1034&SU
=3Dhtt
p://hotmail.com/enca&HL=3DMarket_MSNIS_Taglines
-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
digital self defense, top technical experts, no vendor pitches,
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
digital self defense, top technical experts, no vendor pitches,
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.710 / Virus Database: 466 - Release Date: 23-Jun-04
|