Using string operations to save part of a file path

This is a discussion on Using string operations to save part of a file path within the C++ Programming forums, part of the General Programming Boards category; I need to get the filename out of a full path name and am getting unexpected output and a fault. ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    27

    Using string operations to save part of a file path

    I need to get the filename out of a full path name and am getting unexpected output and a fault.

    I am compiling the code on Visual C++ .NET.

    The code for this function is as follows:
    Code:
    virtual void fileSelected(const char *n) {
    		string name = n;  //copy file path
    		int startName = 0;  //initialize position where filename starts
    		int endName = 0; //initialize position where the filename ends
    		string filename; //stores filename without path or extension
    
    		editor->getSceneGraph()->saveFile(n);
                   editor->setLoadedFileName(n);
    
    		ofstream a_file ( "file.txt" ); //open file
    		
    		for (int i = 0; i < name.length(); i++)  //iterate through given path to find where filename begins and ends
    		{
    			if (name[i]= '/')
    				startName= i;
    			if(name[i] = '.')
    				endName = i;
    		}
    
    		startName= startName+1; //filename actually starts immediately after the last '/'
    
    		//filename = name.substr(startName+1,name.length()-endName); //get filename from name   <---FAULTS HERE
    		
    		//save information to file
    		a_file << filename << endl;  //the filename that I need
    		a_file<< n << endl;  //path input to function
    		a_file << name << endl; //string that is copied from path
    		a_file<< startName << endl; //where the filename starts in the path
    		a_file<< name.substr(name.length()-4, 4)<<endl; //file extension
    		a_file.close();
        }
    If the path is C:\ProgramFiles\test.doc, the output should be:

    C:\ProgramFiles\test.doc
    C:\ProgramFiles\test.doc
    17
    .doc

    I am getting:

    C:\Program Files\test.doc
    .........................
    25
    ....

    Also, when I un-comment the line
    Code:
    filename = name.substr(startName+1,name.length()-endName); //get filename from name
    I get the Microsoft Development Environment error "Unhandled exception at 0x7c812a5b in Interactive Architecture.exe: Microsoft C++ exception: std:ut_of_range @ 0x0012ee60."

    Does anyone know what might be causing my problems and have any suggestions?

    Thank you,
    JackR

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> if (name[i]= '/')
    To start off with, you probably didn't want assignment there and in the next if.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    27
    I do not understand what you mean.

    The first if statement is to find '/' the second if statement is to find '.'.

    Would you please clarify?

    Thanks again,
    JackR

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You are testing equality. A single = means assignment. You want ==.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    27
    Oh. Thanks for the catch. That fixed everything.

    JackR

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Replies: 6
    Last Post: 01-03-2007, 02:02 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21