CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2005
    Posts
    50

    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.

  2. #2
    Join Date
    Aug 2012
    Posts
    12

    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.

  3. #3
    Join Date
    Jul 2005
    Posts
    50

    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?

  4. #4
    Join Date
    May 2007
    Posts
    811

    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.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

    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 )" ).

  6. #6
    Join Date
    Jul 2005
    Posts
    50

    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;
    }

  7. #7
    Join Date
    Aug 2012
    Posts
    12

    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
  •  





Click Here to Expand Forum to Full Width

Featured