Beginning I/O

Apparently I missed an important day of class due to weather related reasons. There is an inclass assignment we were given (First day of I/O streams) that I would like to at least attempt to do before I have class again. I really would like some input on how to begin and an idea of what something like this should look like? Thanks in advance


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @author Your First Name and Last Name
* @file  InClass10.cpp  */

#include <iostream>
#include <cstdlib>
using namespace std;


// Write a complete C++ program to read the integers in the file
//   "numbers.txt" and report to cout how many numbers are in the
//	 file and the sum of all the numbers.
//   Copy and paste the output to the end of your source file.

// Then using notepad, modify the file "numbers.txt" by adding the numbers
//	 224 and 543 at the end of the file.  Run your program again and copy
//	 and paste this second output to the end of your source file.

// NOTE:  For end-of-file processing for a file of numbers,
//	      review the sample programs presented in class.

int main()
{



	return 0; 
} // end main 



the txt file given included the numbers
12 82 34 24 33 47 21 88 123 21
Since you have more than 10 posts, I'm assuming you know we don't do homework for users. Read this tutorial and it may help you, if not you can try googling C++ File I/O.

http://www.cplusplus.com/doc/tutorial/files/
I am not asking for anyone to do the work for me. I think the part I am most unclear about is how I would tell (Visual Studio in my case), where the file is located that I want to read from so I can at least have a solid place to begin.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>

int main()
{
    // specify the path to the file eg.
    const char* path_to_file = "C:/Documents and Settings/cozier/Desktop/numbers.txt" ;
    // or: "C:\\Documents and Settings\\cozier\\Desktop\\numbers.txt"

    std::ifstream file(path_to_file) ; // open the file for input

    int n ;
    while( file >> n ) // read integers from the file one by one till eof
    {
        // for each int 'n' that was read
        // .... do something with it ...
    }
}
Thanks JLBorges,
This is what I ended up with. It seems to be working correctly. I ended up saving the .txt file in the source files folder but am not entirely sure if that is the best way to do that. I would assume saving the file in another location might be a better way to go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <fstream>		// file input/output
#include <iostream>		
#include <cstdlib>		// exit
using namespace::std;

int main()
{
	
	int num, sum = 0, count = 0;
	ifstream inFile("numbers.txt",ios::in); //open file
	
	if ( inFile.fail() )	// checking to see if successfully opened file
	{
		  cerr << "Could" << endl;
		  exit(1);
	}

    // Loop to read from the file
	while ( inFile >> num )	
	{
		count++;
		sum += num;
	}
	
	// Close the file
	inFile.close();

	// Output information 

	cout << "The file contained: " << count <<" numbers\n";
	cout << "The sum of the numbers = " << sum << endl;
	
	return 0;
}
Topic archived. No new replies allowed.