reading excel table with path

how can u read a excel table by typing in the path in the console? I found this code but its not working and I don't know if i could use it.

#include <fstream>
#include <iostream>
#include <string>

#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

using namespace std;

int main()
{
ifstream fin;
string dir, filepath;
int num;
DIR *dp;
struct dirent *dirp;
struct stat filestat;

cout << "dir to get files of: " << flush;
getline( cin, dir ); // gets everything the user ENTERs

dp = opendir( dir.c_str() );
if (dp == NULL)
{
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}

while ((dirp = readdir( dp )))
{
filepath = dir + "/" + dirp->d_name;

// If the file is a directory (or is in some way invalid) we'll skip it
if (stat( filepath.c_str(), &filestat )) continue;
if (S_ISDIR( filestat.st_mode )) continue;

// Endeavor to read a single number from the file and display it
fin.open( filepath.c_str() );
if (fin >> num)
cout << filepath << ": " << num << endl;
fin.close();
}

closedir( dp );

return 0;
}

thx in adv
define "not working"?

what OS are you on. I think
#include <unistd.h>
is linux only, but not positive.
actually i want to open a csv file..... and want to include the filepath when i run the programm.... no idea how to do that. so i tried this code but with this code i couldnt open a file....

it would be great if anyone can tell me how to create a code where i can include the filepath throug my input to open and read the file.
closed account (Dy7SLyTq)
why are you using the c headers? you only need fstream.
u mean like this?

#include <fstream>


using namespace std;

int main()
{
ifstream fin;
string dir, filepath;
int num;
DIR *dp;
struct dirent *dirp;
struct stat filestat;

cout << "dir to get files of: " << flush;
getline( cin, dir ); // gets everything the user ENTERs

dp = opendir( dir.c_str() );
if (dp == NULL)
{
cout << "Error(" << errno << ") opening " << dir << endl;
return errno;
}

while ((dirp = readdir( dp )))
{
filepath = dir + "/" + dirp->d_name;

// If the file is a directory (or is in some way invalid) we'll skip it
if (stat( filepath.c_str(), &filestat )) continue;
if (S_ISDIR( filestat.st_mode )) continue;

// Endeavor to read a single number from the file and display it
fin.open( filepath.c_str() );
if (fin >> num)
cout << filepath << ": " << num << endl;
fin.close();
}

closedir( dp );

return 0;
}
closed account (Dy7SLyTq)

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream file;
string line, filename;

cout<<"file w/ path: ";
getline(cin, filename);

file.open(filename);

while(getline(file, line))
{
//whatever you want to do with the line
}
}
MyFile.open (PATH);
if (!MyFile)
{
printf ("Error, cant open file!\n");
....
}

int main()
{

ifstream PATH;
string line, MyFile;
string PTAH;

cout<<"file w/ path: ";
getline(cin, MyFile);

}


i get just the error message
still not working.....any help?
Topic archived. No new replies allowed.