parsing a string

This is a discussion on parsing a string within the C++ Programming forums, part of the General Programming Boards category; I have 2 strings like this: In these 2 cases I want to get "File1.txt" and "File2.txt" and put them ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    parsing a string

    I have 2 strings like this:
    In these 2 cases I want to get "File1.txt" and "File2.txt" and put them to a new string.
    I need the same method so I suppose I have to find the Last "/" in the string and then
    put the rest to a new string.

    What method and approach could be used to do this ?


    Code:
    std::string Line1 = "c:\Folder1\File1.txt";
    std::string Line2 = "c:\Folder1\Folder2\File2.txt";

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    std::string::rfind()

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Use std::string.rfind and add them together.
    Line1 += Line2;
    And as a side note, "C:\Folder1" is wrong. It should be "C:\\Folder1". For every \, you need to specify two (\\), since \ is an escape character (\n means newline for example).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    Yes okay, I will try that out.

    Thanks !

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You can also use a single '/' instead of double '\\' for path separators:
    Code:
    "C:/Folder/File.txt"

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Only works with API and I wouldn't actually recommend it. Proper filenames uses backslashes (\) and not forward slashes. Someday Microsoft might just change it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Only works with API and I wouldn't actually recommend it. Proper filenames uses backslashes (\) and not forward slashes. Someday Microsoft might just change it.
    What do you mean "only works with API"?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Type "cd c:/" in the command prompt and see what happens.
    Windows filesystem uses "\" and not "/". Windows API translates "/" into "\".
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,666
    Windows filesystem uses "\" and not "/". Windows API translates "/" into "\".
    Microsoft Windows supports both, no matter if it's translated or not. To demonstrate:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
        char word[100];
        const char * const filepath = "C:/Documents and Settings/JCK/My Documents/data.txt";
    
        FILE * fdata;
    
        fdata = fopen( filepath, "w+" );
        if ( fdata ) {
            fputs( "Hello, world! This is a sample text file.\n", fdata );
    
            rewind( fdata );
    
            while ( fscanf( fdata, "%99s", word ) == 1 ) {
                printf( "read from file: \"%s\"\n", word );
            }
            fclose( fdata );
            remove( filepath );
        }
        return 0;
    }
    
    /** as expected, the output:
    read from file: "Hello,"
    read from file: "world!"
    read from file: "This"
    read from file: "is"
    read from file: "a"
    read from file: "sample"
    read from file: "text"
    read from file: "file."
    **/
    This was compiled with Visual C++ 2005 Express on Windows XP Home SP2.
    Filepaths with forward slashes will be opened fine.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    Type "cd c:/" in the command prompt and see what happens.
    Windows filesystem uses "\" and not "/". Windows API translates "/" into "\".
    I'm not suggesting using forward slashes outside of source code.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by citizen View Post
    Microsoft Windows supports both, no matter if it's translated or not. To demonstrate:

    This was compiled with Visual C++ 2005 Express on Windows XP Home SP2.
    Filepaths with forward slashes will be opened fine.
    Well, as I mentioned, Windows API supports both, since it translates it. All the C runtime just wraps calls to Windows API in the end, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String parsing
    By broli86 in forum C Programming
    Replies: 3
    Last Post: 07-03-2008, 05:06 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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