CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2003
    Location
    Guayama, P.R.
    Posts
    41

    Back in the game ... strings and files?!?!

    To make the post shorter, the new problem example is a few posts
    below.

    Thanks guys!
    Last edited by eagle1; February 21st, 2006 at 09:14 AM.

  2. #2
    Join Date
    Jul 2003
    Location
    Guayama, P.R.
    Posts
    41

    Re: Back in the game ... strings and files?!?!

    Hmmmmmmm... 25 views and no replies... I guess this is going to
    be difficult to implement!

    I'll try something tonight to see what I come up with.

    Thanks.

  3. #3
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: Back in the game ... strings and files?!?!

    You should more clearly define the problem. Level of detalization in examples suggests complexity of problem, but the part I understood is that you just need to count ".NAME" substrings. And there evidently is something more, but it's not clear what exactly.
    "Programs must be written for people to read, and only incidentally for machines to execute."

  4. #4
    Join Date
    Oct 2004
    Posts
    6

    Re: Back in the game ... strings and files?!?!

    I don't know exactly what your meaning is,
    but I can definitely say that this is very easy.

    When confused me in your problem is:
    "Now I want to run the program, and make it ask the user
    how many entries it has (he should input 4 since he took an entry out)".
    ~~why is it necessary here to ask the user to input something like 4?
    as the entry is four here, even if the user input something else, e.g, 5,
    the entries is still 4.

    Quote Originally Posted by eagle1
    Hey guys.

    It's been a couple of years since I made
    my last "program" and I'm just so lost.
    I have been trying to get back into the game
    but haven't found the time.
    Now, I'm in need of doing something useful
    to optimize a task I do but I don't know if I have chosen a way
    too difficult task for me to tackle on and be back.

    Ok, so enough crying.
    What I want to do is the following, let's look at the example below:


    TEST1 TST.STRING.NUMBER "5"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_CUATRO"
    TEST1 TST.STRING.NUMBER[2].STRING2 "1"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "EVEN more text"
    TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_CINCO"
    TEST1 TST.STRING.NUMBER[3].STRING2 "1"
    TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "A lot more text"
    TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SEIS"
    TEST1 TST.STRING.NUMBER[4].STRING2 "1"
    TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Yep, keep going"
    TEST1 TST.STRING.NUMBER[5].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[5].STRING2 "2"
    TEST1 TST.STRING.NUMBER[5].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[5].STRING2[2].LINE "of text."

    All this text is on a textfile, let's call it Sample.txt
    The user can manually modify this file but I want
    to be able to do it via the program.
    So let's say that I erase manually the text strings with the number
    4 and we will have this:

    TEST1 TST.STRING.NUMBER "5"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_CUATRO"
    TEST1 TST.STRING.NUMBER[2].STRING2 "1"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "EVEN more text"
    TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_CINCO"
    TEST1 TST.STRING.NUMBER[3].STRING2 "1"
    TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "A lot more text"
    TEST1 TST.STRING.NUMBER[5].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[5].STRING2 "2"
    TEST1 TST.STRING.NUMBER[5].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[5].STRING2[2].LINE "of text."

    Now I want to run the program, and make it ask the user
    how many entries it has (he should input 4 since he took an entry out).
    After this, all of the numbers (after .NUMBER[]) should
    re-organize and everytime a .NAME is found the counter
    should increment, etc. So we will finally have:

    TEST1 TST.STRING.NUMBER "4"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_CUATRO"
    TEST1 TST.STRING.NUMBER[2].STRING2 "1"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "EVEN more text"
    TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_CINCO"
    TEST1 TST.STRING.NUMBER[3].STRING2 "1"
    TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "A lot more text"
    TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[4].STRING2 "2"
    TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[4].STRING2[2].LINE "of text."

    and it should be saved to another file.

    Is this doable in C++???
    Is PHP or Perl better???

    I already started in C++ and can just get the first line:
    TEST1 TST.STRING.NUMBER "4"

    with the number the user inputs.
    But everything else it's a blur and what I have tried
    has crashed and burned my Compiler!! lol

    If you can give me hints, tips, links, etc.
    I will appreciate it.
    If you can give me code snippets, even better.

    Thanks.

    And please welcome me back!

  5. #5
    Join Date
    Jul 2003
    Location
    Guayama, P.R.
    Posts
    41

    Re: Back in the game ... strings and files?!?!

    Just one minute..
    Something happened to my browser.

  6. #6
    Join Date
    Jul 2003
    Location
    Guayama, P.R.
    Posts
    41

    Re: Back in the game ... strings and files?!?!

    Hello guys.

    Thanks for posting and thanks
    for saying this can be done easily
    (at least this keeps me trying).

    Let me see if I can explain a little better.

    I have a text file.
    The program will ask the user for a text file to use.
    The program will ask the user for a text file for output.

    After this, the program should ask:

    How many entries are in the text file:
    And according to what the user inputs, the output
    text file will have the updated numbers.

    A visual example.
    Let's say we have this below:


    TEST1 TST.STRING.NUMBER "3"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_CUATRO"
    TEST1 TST.STRING.NUMBER[2].STRING2 "1"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "EVEN more text"
    TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[3].STRING2 "2"
    TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[3].STRING2[2].LINE "of text."

    The user MANUALLY alters this text above to be the following:

    TEST1 TST.STRING.NUMBER "3"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[3].STRING2 "2"
    TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[3].STRING2[2].LINE "of text."

    He should now run the program and this should be what comes up:

    Program: How many entries are in the file:
    User: 2

    Program Output:

    TEST1 TST.STRING.NUMBER "2"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[2].STRING2 "2"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."


    It can happen the other way around.
    Let's say that the file originaly has this:

    TEST1 TST.STRING.NUMBER "2"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[2].STRING2 "2"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."

    and I manually alter it to have the following:

    TEST1 TST.STRING.NUMBER "2"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[2].STRING2 "2"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
    TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SEIS"
    TEST1 TST.STRING.NUMBER[4].STRING2 "1"
    TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Yep, keep going"
    TEST1 TST.STRING.NUMBER[5].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[5].STRING2 "2"
    TEST1 TST.STRING.NUMBER[5].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[5].STRING2[2].LINE "of text."

    Then the user can run the program and the following should result.

    Program: How many entries are in the file:
    User: 4

    Program Output:

    TEST1 TST.STRING.NUMBER "4"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[2].STRING2 "2"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
    TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SEIS"
    TEST1 TST.STRING.NUMBER[3].STRING2 "1"
    TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Yep, keep going"
    TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[4].STRING2 "2"
    TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[4].STRING2[2].LINE "of text."


    So as you can see is "Basically a replace" code.
    If the line has a .NAME in it, it would mean that the number
    should increase.
    But it's proving to be difficult for me to implement.

    I was using something called istreamstring to separate each
    line with whatever type I wanted and making what is in [] (i.e. the number)
    an INT parameter.
    But I don't know if I'm over-thinking this!

    Any help, or snippets welcome.

    Thanks Again.

    P.S. The format of the text file is just like you see above with the periods and all of that stuff!

  7. #7
    Join Date
    Oct 2004
    Posts
    6

    Re: Back in the game ... strings and files?!?!

    I still wonder why you ask the user to input the count of entries.
    because I think the count can be obviously figured out from the file.
    If there are only 3 entries,
    What do you want to do if the user input 10?

  8. #8
    Join Date
    Jul 2003
    Location
    Guayama, P.R.
    Posts
    41

    Re: Back in the game ... strings and files?!?!

    Oh ok.
    Now I get what your saying.

    The thing is that this line:

    TEST1 TST.STRING.NUMBER "3"

    needs to be updated by the user.
    So even if there 3 entries and the user wants this line to display
    10, then it will have to display 10:

    TEST1 TST.STRING.NUMBER "10"

  9. #9
    Join Date
    Jul 2003
    Location
    Guayama, P.R.
    Posts
    41

    Re: Back in the game ... strings and files?!?!

    Hey guys.

    This is the partial code I have right now:

    Code:
    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
         string infile, outfile, line;
         int n, lastn, index, pos1, pos2, Entries;
    
         cout << "Input File : ";
         cin >> infile;
         cout << "Output File : ";
         cin >> outfile;
         cout << "# of Entries : ";
         cin >> Entries;
    
         cout << infile << endl;
         cout << outfile << endl;
         cout << Entries << endl;
    
         ifstream fin(infile.c_str());
         ofstream fout(outfile.c_str());
    
         n = 0;
    
         getline(fin, line);
    
         while (! fin.eof())
         {
                   
              if (n == 0)
              {
                   pos1 = line.find('"');
    
                   fout << line.substr(0, pos1 + 1);
                   fout << Entries;
                   fout << line.substr(pos1 + 2);
                   fout << endl;
    
                   lastn = ++n;
                   index = 1;
              }
              else
              {
                   pos1 = line.find('[');
                   pos2 = line.find(']');
    
                   istringstream iss;
    
                   iss.str(line.substr(pos1 + 1, pos2 - pos1 - 1));
                   iss >> n;
    
                   if (n != lastn)
                   {
                        lastn = n;
                        index++;
                   }
    
                   fout << line.substr(0, pos1 + 1);
                   fout << index;
                   fout << line.substr(pos2);
                   fout << endl;
              }    
    
              getline(fin, line);
         }
    }
    What this can do is that if I have the following from the text file I will read:

    TEST1 TST.STRING.NUMBER "2"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[2].STRING2 "2"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
    TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SEIS"
    TEST1 TST.STRING.NUMBER[4].STRING2 "1"
    TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Yep, keep going"
    TEST1 TST.STRING.NUMBER[5].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[5].STRING2 "2"
    TEST1 TST.STRING.NUMBER[5].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[5].STRING2[2].LINE "of text."

    the text file output will have:


    TEST1 TST.STRING.NUMBER "4"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[2].STRING2 "2"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
    TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SEIS"
    TEST1 TST.STRING.NUMBER[3].STRING2 "1"
    TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Yep, keep going"
    TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[4].STRING2 "2"
    TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[4].STRING2[2].LINE "of text."


    As you can see it's working partially because the index is actually
    changing the number inside the brackets ... BUT!!!!!!

    If I add another entry to the file, lets say:

    TEST1 TST.STRING.NUMBER "4"
    TEST1 TST.STRING.NUMBER[1].NAME "TEXTO_TRES"
    TEST1 TST.STRING.NUMBER[1].STRING2 "1"
    TEST1 TST.STRING.NUMBER[1].STRING2[1].LINE "More TEXT"
    TEST1 TST.STRING.NUMBER[2].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[2].STRING2 "2"
    TEST1 TST.STRING.NUMBER[2].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[2].STRING2[2].LINE "of text."
    TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SEIS"
    TEST1 TST.STRING.NUMBER[3].STRING2 "1"
    TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Yep, keep going"
    TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SEIS"
    TEST1 TST.STRING.NUMBER[3].STRING2 "1"
    TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Yep, keep going"
    TEST1 TST.STRING.NUMBER[3].NAME "TEXTO_SEIS"
    TEST1 TST.STRING.NUMBER[3].STRING2 "1"
    TEST1 TST.STRING.NUMBER[3].STRING2[1].LINE "Yep, keep going"
    TEST1 TST.STRING.NUMBER[4].NAME "TEXTO_SIETE"
    TEST1 TST.STRING.NUMBER[4].STRING2 "2"
    TEST1 TST.STRING.NUMBER[4].STRING2[1].LINE "Two Lines"
    TEST1 TST.STRING.NUMBER[4].STRING2[2].LINE "of text."


    as you can see, I repeated the 3rd entry 2 times.
    If I run the program, the output wont be correctly BECAUSE I still have not implemented
    that the program should increment the number inside the brackets if a .NAME tag is found.


    Any solutions?

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