I have spent awhile messing with File I/O and I thought I was finally getting it, until I ran into the issue of trying to read an existing file. I was able to create a file via a program, and write to it, open it and close it, however when I tried to read a txt file that I previously made myself it will not work. Any idea what I'm doing wrong? Here is my simple simple code I'm using to test this.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
string FileName;
cout <<"Please enter the original file name and see if it works muahahah\n";
cout <<"File Name: ";
cin >> FileName;
ifstream WontWork(FileName);
string line;
if (WontWork.is_open())
{
while ( WontWork.good() )
{
getline (WontWork,line);
cout << line << endl;
}
WontWork.close();
}
cin >> FileName;
return 0;
}
The text file I'm trying to read is called "Problem1.txt" I have littered all my folders with this txt file and it still won't read it. ATM I have put it in, Mydocuments/Visualstudio2010/Projects/test/debug/ (the exe is in here)
and in Mydocuments/Visualstudio2010/projects/test/test/ (when you create a file through a program it puts it in this folder). Still no go. please help!
string filename[25];//the size of the file
use cin.getline(filename,25); and you forget to declareWontWork.open(filename);.
If it dont work I will try it myself. Good Luck.
Thanks for the help guys, I feel real silly, but what ended up happening is that I didn't have file extensions on, and when I created a new file, I made it Problem1.txt, and then the file name was actually. Problem1.txt.txt Silly me :(