Using ifstream for all .txt files in directory - c++

I am wondering how to read in all files in a directory (.txt) one by one without knowing their file names?
I am familiar with the following simple case and its derivatives:
ifstream inTestData;
string inputFile("filename.txt");
inTestData.open(inputFile.c_str());
but I wasn't sure if you didn't know the file name and you had multiple files in the directory, of which you want to read all of them (assuming you put all of the .txt's in that directory).
Any thoughts?
Thanks!

Related

how to correctly write path for files in the network for c++11 fstream

i need to open file that is in the network but my ifstream couldnt find the file.
when i rightclick --> properties the file the file location said is
\\LAPTOP-UDC1U1DT\Users\Public\Documents\log.txt
but this doesn't work in ifstream because of compiler errors.
\\\\server\\share\log.txt
i then tried this as said in another question, although this has no errors but the program couldn't find the file.
can somebody teach me how to correctly write the path for c++ so that fstream can find the file?
the text file by the way is shared using ad hoc.
thank you.
If you want to open or read/write to a file in C++, you have got to use a double backslash instead of a single one. Moreover you have to use the full path because it may happen that your file is not located in the same directory as your .exe is.
Maybe try something like this:
ifstream inputFile;
inputFile.open("C:\\folder1\\folder2\\text.txt", ios::in);
This should also work if you want to get a file out of your server which is your running system is connected to.

Reading from a .txt file with an unspecified name but with a known directory c++

I have built a program in c++ whitch checks how many words a text has.
The text is stored in a .txt file in the same directory as my .exe file. I was wondering if there is a way to make the name of my .txt file irrelevant as long as the .txt file is in the same directory as my .exe file is? I would like to be able to change the name of the .txt file and still run my program successfully without getting a "error opening file" message.
You need to enumerate the files in your app's folder until you find one with a .txt file extension.
However, there is nothing in the standard C++ libraries to handle that.
You need to use platform-specific APIs 1 to determine the folder where your app is running from, and then you can use platform-specific APIs 2, or a 3rd party cross-platform API 3, to enumerate the files in that folder.
Once you discover the file, only then can you open it.
1: (like parsing the result of GetModuleFileName() on Windows)
2: (like FindFirstFile() and FindNextFile() on Windows)
3: (like boost::filesystem)

VS2010 doesnot pick up file from resources folder

I am required to parse a text file in my VS project in mfc in c++. The text file is supposed to be a part of the entire exe product. For that purpose, I placed the text file in my resources folder and set the path in my code as:
char fileName[] = "../myFile.txt";
The problem I'm facing is that VS doesn't find this file in its Resources folder. I added the file in the project file, but that just gave me a corrupt file error. However, the file access works if I provide the absolute path to the file in my code i.e. "C/abc/myFile.txt"
I need the code running on all machines, hence need some method to get VS to read this file using a relative path. Can anybody please provide some assistance? I am a newbie and have tried all that's in my knowledge.
Actually, if it's a resource file it should be copied over to the bin folder, which means your fileName should just be:
char fileName[] = "myFile.txt";
if that doesn't work then, you might need to change the properties of your myFile.txt to ensure it does get copied over with the build process.
Here you can find an answer for your question: http://www.cplusplus.com/forum/general/54255/

How can I rename files of which I don't know the extension?

I have a directory with these files:
one.txt
two.mp3
three.bmp
Is there any way to rename files using MoveFile() but without specifying the extension of the file (all the files have different filenames)?
If not, how can I rename these files to anything I want, when I only know the
one
two
three
?
You'll have to use the underlying OS API to scan through the directory for files and compare each filename with your desired prefix. Here is another question that shows you how to list the contents of a directory in Windows.
Once you know prefix read the filelist of the directory and find a valid filename with that prefix, and use that filename with the function.

Reading from folder in working directory C++

I want to read from a text file which is saved in a folder in my working directory. But unless the text file is in the working directory, it wont open.
Im using a line like this one. what should it be changed to?
ifstream myfile ("./folder/example.txt");
If you are on Windows, use this format to open the file.
ifstream myfile ("C:\\Book\\file.txt") ;
Supposing your file is in Book folder of C Drive.
First write the Directory and then the subsequent folders with double slashes in between till you reach the text file.
Though it is better if you put in your project directory itself, using this.
ifstream myfile ("file.txt") ;
You can access the directory your program is, by simply writing
ifstream myfile ("example.txt");
If example.txt is in the same directory as your program.
Note that this may not work if you can't save in this directory.