reading and writing files

I will prepare files of “random” integers for you. The numbers will be stored 1 per line. Your program will:
Prompt the user for the name of the datafiles to use (note: you can call your output file anything you
want…within reason).
While there is data in the file
Read an integer
If the integer is even, add it to the even sum
If the integer is odd, add it to the odd sum
Be sure to check for an empty file. If there is, report this to the user and stop the program right there (exit).
o When there is no more data, output the sum of the even integers and the sum of the odd integers TO A TEXTFILE.
When I compile it doesn't seem to find the text or I may be doing something wrong
my ouput:

Enter file name: Ints1.txt
File not found or cannot be opened.
Make sure path is correct.
Press any key to continue . . .

Last edited on
Check whether the file(Ints1.txt) is located in the same path where your coding class file exists (lets say main.cpp)
Last edited on
I may sound really dumb here but I saved them on separate files on my documents.
Whats the path of Ints1.txt and your cpp file?
Last edited on
It is on my libraries/documents/Ints1.txt
Do i move them to local disc C?
My cpp file is in libraries/documents/C++/HW9
Its just a suggestion, move the .txt file to libraries/documents/C++/HW9 and check whether you are getting the same error or not.
I moved them to HW9 and I am still getting the same error. this is how I am calling it:
Enter file name: c:\libraries\documents\HW9\Ints1.txt
maybe this is where the erros is?
Once you moved, give the file name as Ints1.txt.

Like, Enter file name: Ints1.txt (instead of full path)
I get the same output.
int main()
  ofstream out("FindMe.txt");
  out << "Yay\n";
  return 0;

The location of the file that code creates is where you need to put your input file.
I tried and it was working file paths are


and I have given the file name in the runtime as Ints1.txt.
Is it possible for you to show the part of the code where you put the location of the file?
I didn't use the location of the file in my code. I just used your coding and placed the .txt file in the same location of main.cpp and executed.
That would meant that I wouldn't allow the user to enter the file name if i put it directly on my code. And I need to let the user be able to enter their own file name.
Topic archived. No new replies allowed.