For Loop to Read From File C++

while loop and read file program

i take to write a plan that 2. Apply file I/O to read the integers from the file. If the data file is saved in your project binder, only
the file name is needed when you lot declare an ifstream variable, otherwise, a path to the file is
required.
3. Suppose the ifstream variable is input, and so apply while(!input.eof()) for testing whether the cease
of file is reached.

This is all I accept so far... Delight help!

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream inFile;

inFile.open (" hw4dataf11.txt")

y=ten%2
if (y=0) = even;
if (y=one) = odd;
}

http://www.cplusplus.com/medico/tutorial/files/

Taken from the to a higher place link...

                          one
two
3
iv
v
6
7
8
9
10
xi
12
13
14
xv
xvi
17
xviii
xix
20
21
22
23
                                                      // reading a text file                            #include <iostream>                            #include <fstream>                            #include <string>                            using                            namespace                            std;                            int                            main () {   cord line;   ifstream myfile ("example.txt");                            if                            (myfile.is_open())   {                            while                            ( myfile.skilful() )     {       getline (myfile,line);       cout << line << endl;     }     myfile.close();   }                            else                            cout <<                            "Unable to open up file";                            return                            0; }                        

EDIT:
Woo my 100th post :D

Last edited on

In that location are so many things wrong nearly this information technology hurts a little.

@xander337 be squeamish, he is just starting :P

Here is a complete program that will take each line (if it is a number) and output "Odd" or "Even"

                          1
ii
3
iv
five
6
seven
eight
9
10
11
12
13
14
fifteen
16
17
18
19
xx
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                                                      #include <iostream>                            #include <fstream>                            #include <string>                            #include <stdlib.h>                            #include <sstream>                            using                            namespace                            std;                            // Determines if the string is a number                            bool                            isNumeric(string pszInput);                            int                            main () {                            // Hold the line in the file                            string line;                            // Open the file                            ifstream myfile ("example.txt");                            if                            (myfile.is_open()) {                            // While the file is good                            while                            (myfile.skillful() ) {                            // Get the current line in the file                            getline (myfile, line);                            // Verify that the line is an integer                            if                            (isNumeric(line)) {                            // Convert 'line' to an integer and summate                            if                            (atoi(line.c_str())%2 == 0) {  					cout <<                            "Even\due north"; 		 				}                            else                            {  					cout <<                            "Odd\n"; 				 				} 				 			}  		}  		myfile.close();  	}                            else                            {  		cout <<                            "Unable to open up file\n";   	}                            // Exit                            return                            0;  }                            // Determines if the cord is a number                            bool                            isNumeric(string pszInput) {  	istringstream iss(pszInput);                            double                            dTestSink; 	iss >> dTestSink;                            // was any input successfully consumed/converted?                            if                            (!iss) {                            return                            false; 	 	}                            // was all the input successfully consumed/converted?                            return                            (iss.rdbuf()->in_avail() == 0);  }                                                  

it might be a petty shotty.. whipped it upwardly in a couple minutes

Last edited on

No I mean. Well for ane he posted his thread in the wrong forum. He didn't and then much ask a question as plead for help, oh and he didn't even thinly vale the fact that this is a homework assignment that you just gave him a consummate answer to. Also no lawmaking tags (only that's nitpicking.)

I didn't hateful there was and then much wrong with his code, I meant there was then much wrong with this thread.

Quote "xander337"

oh and he didn't even thinly vale the fact that this is a homework assignment that you just gave him a complete answer to

I didn't think of that... I learn c++ on my own so my first thoughts are that it is a personal projection... >_>

Last edited on

Topic archived. No new replies allowed.

gagneaget1996.blogspot.com

Source: https://www.cplusplus.com/forum/lounge/52458/

0 Response to "For Loop to Read From File C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel