CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2009
    Location
    Brazil
    Posts
    1

    Exclamation Using Case Function To Read File Lines

    Hello,
    I want to build a program that read all the lines of a file and with some case functions. Here is a file example:

    example.txt
    Code:
    test
    example
    And here is some example of case function:
    Code:
    case test:
       cout << "Hello, World";
    [...]
    case example:
       cout << "Enter a number: ";
            cin >> number;
    But i want that my program reads this sample file, read it's content and put the things that are in the case function in the screen(command line, in Linux: Terminal) in the order that is in the file.

    Thanks,
    Nathan Paulino Campos

  2. #2
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Using Case Function To Read File Lines

    The simplest way is to compare string which you read from file with the string which you code in your program.
    Your case function would something like this
    Code:
    void foo(std::string xStr)
    {
        if(xStr == "example")
           //do something
        else if(xStr == "test")
          //do other thing
        ...
    }
    or you can use more complex and more cute method: read line from file, calculate its hash with some hash function and compare its hash with already coded hash in your application. Something like this:
    Code:
    void foo(size_t uiHash)
    {
        switch(uiHash)
        {
             case uiExampleHash:
               //Do something
                break;
              ...
        };
    
    }

Tags for this Thread

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