Thread: Tokens?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    43

    Tokens?

    I am making a simple DOS program to launch programs...

    So to make it user configurable there's a text file where the user can add and delete stuff.

    Notepad=C:\windows\system32\notepad.exe
    is what's in my text file.

    I have two questions...

    1. How do I read directly from a line without going thru a loop?

    2. How to I separate the above quote into two sections, Notepad and C:\windows\system32\notepad.exe?

  2. #2
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    If you're using Windows, why not use the INI file functionality built into the Windows API? eg. look at the GetProfileProfileString function to get you started.

    If you want this to work across platforms, then you'll probably end up needing to loop through the file yourself.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    Thanks. I am not planning to port it, so it'll work I guess...

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    I'm quite a noob to this, and the MSDN library is almost like Greek to me... can you give me a demonstration?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    24
    Well, for 1. question. You can use fgets() to read constant numbers of characters from a stream to a string. But it stops reading if it hits '\n', newline character.

    And for 2. question, if I got your meaning right, I'd use strchr() to find '=' mark from the string got with fgets().

    So, for your notepad thing:
    Code:
    char str1[256];
    char str2[256];
    char str3[256];
    FILE* f = fopen("<text file>", "r");
    
    // reads a line from the text file
    fgets(str1, 256, f);
    
    // this may take the rest of the string str1 in str2. if so do it 
    // other way.
    // if str1 would be "Notepad=" and its memory addresses would 
    // be 1 2 3 4 5 6 7 8.
    // strchr() returns pointer to '=' mark (address 8).
    // so, wouldn't you get "Notepad" string by using pointer 
    // arithmetic? just an idea. I got no time to think it better now ;) 
    str3 = strchr(str1, '=');
    Finally you should have str2 with name "Notepad" and "C:\windows\system32\notepad.exe" in str3.
    Last edited by JulleH; 05-03-2005 at 06:13 AM.

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    But I wish to specify which line to read, eg read line 3... Is this possible?

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    whats stopping you reading and discarding the first two?
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    Quote Originally Posted by Stoned_Coder
    whats stopping you reading and discarding the first two?
    What do you mean?

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if you wish to specify which line to read then use a loop and read all the lines up to the one you are interested in and discard them, then read the one you want to work on and away you go.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    I tried the example and it returns a Windows error... Using pointers solved the problem, but now I can't get rid of the ='s

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can move the pointer one spot over:
    Code:
    const char* str = "=something";
    str++;
    	
    cout<<str<<endl;
    Last edited by 7stud; 05-03-2005 at 07:24 AM.

  12. #12
    Registered User
    Join Date
    Nov 2002
    Posts
    43
    Thanks. So I guess I'll put the read line stuff inside a function...

    Also, str2 does not seem to work. It outputs some funny ASCII character instead of "Notepad". Why is this so?

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by trenzterra
    Thanks. So I guess I'll put the read line stuff inside a function...

    Also, str2 does not seem to work. It outputs some funny ASCII character instead of "Notepad". Why is this so?
    It must point to invalid data.

    I think your whole approach is wrong. Since this is a C++ forum and not a C forum, I would assume you want a C++ solution. In that case, you should be using C++ strings, which eliminate all the problems you are having. You can then use the find() function to get the index position of the equals sign:

    Code:
    //open input file myInputFile, etc.
    
    string input;
    
    getline(myInputFile, input); 
    //read in a line of data from myInputFile and store it in the string input
    
    int equals = input.find('=');
    And, you can use the substr() function to divide up the string:

    Code:
    string program = input.substr(0,equals); 
    //substring from index 0 to equals--not including the char at index position equals.
    
    string path = input.substr(equals + 1);
    //substring from equals + 1 to the end of the string
    Last edited by 7stud; 05-03-2005 at 08:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 11-19-2008, 01:16 AM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Splitting A String Into Tokens!
    By bobthebullet990 in forum C Programming
    Replies: 15
    Last Post: 03-22-2006, 09:24 AM
  4. Tokenising Tokens..
    By StanleyC in forum C Programming
    Replies: 2
    Last Post: 06-04-2004, 10:53 AM
  5. How to output tokens in reverse order using strtok
    By Ninz in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 09:00 PM