How can I automatically open and read the content of a file within a given directory from a C++ application without knowing the file's name?
For example (a rough description of the program):
#include iomanip
#include dirent.h
#include fstream
#include iostream
#include stdlib.h
using namespace std;
int main()
{
DIR* dir;
struct dirent* entry;
dir=opendir("C:\\Users\\Toshiba\\Desktop\\links\\");
printf("Directory contents: ");
for(int i=0; i<3; i++)
{
entry=readdir(dir);
printf("%s\n",entry->d_name);
}
return 0;
}
This will print the name of the first file in that directory. My problem is how to read that particular file's content and save it in a .txt document. Can ifstream
do that? (Sorry for my bad English.)
The mkdir function creates a new, empty directory with name filename . The argument mode specifies the file permissions for the new directory file. See The Mode Bits for Access Permission, for more information about this. Write permission is denied for the parent directory in which the new directory is to be added.
When a C program starts its execution the program automatically opens three standard streams named stdin , stdout , and stderr . These are attached for every C program.
Call opendir() function to open all file in present directory. Initialize dr pointer as dr = opendir("."). If(dr) while ((en = readdir(dr)) != NULL) print all the file name using en->d_name.
For this open a command prompt, navigate to the folder where you want to create a new folder using cd. Then use command mkdir followed by the name of the folder you want to create. After that you can simply use command dir to check if the folder has been created.
In the Excel Options dialog box, click on Advanced (in the left pane of the dialog box) Scroll down and within the General options, enter the location of the folder in the field with the description – “At startup, open all files in:” That’s it! Now when you start Excel, it will automatically open all the files in this specified folder.
Opening a file - for creation and edit. Opening a file is performed using the fopen () function defined in the stdio.h header file. The syntax for opening a file in standard I/O is: ptr = fopen ("fileopen","mode"); For example, fopen ("E:\\cprogram\\newprogram.txt","w"); fopen ("E:\\cprogram\\oldprogram.bin","rb");
Double-click on the Excel StartUp location. This will open the trusted location dialog box with the Excel StartUp folder location. Copy this location. Open any folder and enter the copied location and hit Enter. This will open the Excel StartUp folder Place the file (or the shortcut to the file) that you want to open in this folder.
But you can make folders open on the next reboot with a quick check in “File Explorer Options.” Here’s how to do it. To make sure your open folders reopen when you start a new session, open File Explorer and from the ribbon, select the View tab and Options > Change folders and search options.
this should do it
#include <iostream>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
using namespace boost::filesystem;
using namespace std;
void show_files( const path & directory, bool recurse_into_subdirs = true )
{
if( exists( directory ) )
{
directory_iterator end ;
for( directory_iterator iter(directory) ; iter != end ; ++iter )
if ( is_directory( *iter ) )
{
cout << iter->native_directory_string() << " (directory)\n" ;
if( recurse_into_subdirs ) show_files(*iter) ;
}
else
cout << iter->native_file_string() << " (file)\n" ;
copyfiles(iter->native_file_string());
}
}
void copyfiles(string s)
{
ifstream inFile;
inFile.open(s);
if (!inFile.is_open())
{
cout << "Unable to open file";
exit(1); // terminate with error
}
//Display contents
string line = "";
//Getline to loop through all lines in file
while(getline(inFile,line))
{
cout<<line<<endl; // line buffers for every line
//here add your code to store this content in any file you want.
}
inFile.close();
}
int main()
{
show_files( "/usr/share/doc/bind9" ) ;
return 0;
}
If you're on Windows you can use the FindFirstFile in the Windows API. Here is a short example:
HANDLE myHandle;
WIN32_FIND_DATA findData;
myHandle = FindFirstFile("C:\\Users\\Toshiba\\Desktop\\links\\*", &findData);
do {
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
cout << "Directoryname is " << findData.cFileName << endl;
}
else{
cout << "Filename is " << findData.cFileName << endl;
}
} while (FindNextFile(myHandle, &findData));
Otherwise I'd go with ayushs answer, Boost works for unix systems as well
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With