-
August 10th, 2012, 01:10 AM
#1
extract the file name and extension from a .txt
I have a list of files stored in a .txt file
$codeguru\c++\display.txt$15$
Directory File Folder: codeguru\c++
File Name: display.txt
File Size: 15kbs
$ is an delimiter
I want to extract the name and the extension from txt files. Can you give a example of a simple way to do this?
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
struct MyFile
{
std::string Directory;
std::string Filename;
unsigned filesize;
};
int main()
{
std::ifstream readfile ( "mylog.txt");
if(!readfile)
{
std::cout << "Please check that the file exists" << std::endl;
exit(1);
}
while (!readfile.eof())
{
//string::find()
// string::substring()
// substr or find_last_of
}
// copy all the data to a new file and save it..
}
Last edited by jonnybravo; August 10th, 2012 at 01:13 AM.
-
August 10th, 2012, 01:33 AM
#2
Re: extract the file name and extension from a .txt
Hmm. Could you give more information? Are you looking for an actual working way to do this, an efficient way, or just a simple way?
The logic behind the procedure is straight forward. While reading each line, store each section (sections being marked by the delimiter) into separate variables. For the first section, the most reasonable thing to do is look for the very last '\' and remove everything before it. Next, look for the very last '.' because there can be more than one in a file name, then separate the stuff before the period into its own variable and the stuff after the period into its own variable. Now, you should have a variable for the full file path, a variable for the file name, a variable for the file extension, and a variable for the file size.
Last edited by Vlykarye; August 10th, 2012 at 01:35 AM.
-
August 10th, 2012, 07:51 AM
#3
Re: extract the file name and extension from a .txt
Could you post a example?
Maybe something like this?
Code:
typedef unsigned char byte;
byte splitdelimiter = '$';
// find name
int id = std::find( splitdelimiter );
if (id == -1)
{
break;
}
else if( id == 0 )
{
std::vector.erase(0);
id = std::find( splitdelimiter );
}
I don't know something like that mabye?
-
August 10th, 2012, 09:12 AM
#4
Re: extract the file name and extension from a .txt
If you are not everse to using boost, look it up http://www.boost.org/doc/libs/1_50_0.../doc/index.htm. There are functions to handle path manipulation and in a cross platform way.
-
August 10th, 2012, 10:04 AM
#5
Re: extract the file name and extension from a .txt
as a side note, do not use exit() or abort(),terminate(),etc... without a valid reason. Just return an error code from main() instead.
second, do not rely solely on eof() to check for stream loop termination, you could easily end up with an infinite loop. Just use the stream boolean operator instead ( like, say, "while( readfile )" ).
-
August 10th, 2012, 01:21 PM
#6
Re: extract the file name and extension from a .txt
Open notepad copy this into it.
$codeguru\c++\display.txt$15$
save it as mylog.txt
Using streams to get the Directory, Filename, filesize.
Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int StringSplit(const std::string &Str, char Delimeter, std::vector<std::string> &StringTokens)
{
stringstream Stream(Str);
string Token;
while(getline(Stream, Token, Delimeter))
StringTokens.push_back(Token);
return StringTokens.size();
}
int main()
{
ifstream InFile("mylog.txt");
if(!InFile.fail())
{
while(!InFile.eof())
{
string Line;
vector<string> StringTokens;
getline(InFile,Line,'\n');
if(StringSplit(Line,'$',StringTokens) > 0)
{
// do something with StringTokens
}
}
InFile.close();
}
return 0;
}
-
August 10th, 2012, 02:06 PM
#7
Re: extract the file name and extension from a .txt
I personally believe it would be better for the OP to figure out how to do some of the things himself. But that is just my opinion.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|