How to replace external file with internal string Array ?

This is a discussion on How to replace external file with internal string Array ? within the C++ Programming forums, part of the General Programming Boards category; Hello Everybody, I'm having a tiny issue that is like a GIANT for me This program use a text file ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    90

    How to replace external file with internal string Array ?

    Hello Everybody,

    I'm having a tiny issue that is like a GIANT for me This program use a text file named yes.txt with these lines and NO blank line at the beginning or end of file:
    sing
    Sing
    SANG!!!
    yes
    no
    nope YES nope YES

    I want to eliminate this external file and do the same thing with the same text being inside the program in a string array. I thought it should be simple, I included a array "string zfile[6]" to replace the external file but I don't know how to make it sing again. I google but it seems that everyone want to do the opposite. I been trying for hours. What do i need to change to make this happen. Thanks in advance

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    string zfile[6]={"sing", "Sing", "SANG!!!", "yes", "no", "nope YES nope YES",};
    //string zfile[6];
    
    
    int main()
      {
         ifstream xfile("yes.txt");
    
            if ( !xfile )
            {     cerr << "File does not exist.\n\n"; system("pause");  exit( 1 );  }
    
            vector<string> lines;
            string line;
    
            while (getline(xfile, line))
            {
            lines.push_back(line);
                                    
            cout << "results:  " <<  line  << endl;
            }
    
            xfile.close();
    
            system("pause");
    
     }

  2. #2
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    I think ifstream is to read.
    You should use ofstream.

    Refer this :Input/Output with files

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    I tried to change things to use ofstream and ostream as suggested and I came up with nothing that works. I better tell you guys "this is not homework". When I ask for help it's usually to do something beyond the homework required. I don't code for school. I code for me. I write FASM and MASM since 1999 and never saw big words like ofstream and ostream. Just to give you an idea of how we work with string at processor level and this is what those C++ lib do under the hood without our knowledge::

    Code:
        mov     edi, HTML.Day_S ;Destination Ptr 
        mov     esi, Day.Wk ;Source Ptr (Array of Days) 
        xor     eax, eax 
        mov     ax, [Time.wDayOfWeek] ;0 &lt;= eax < 7 
        add     esi, eax              ;esi =+ eax * 3 
        add     esi, eax                   ; Indexes the Array 
        add     esi, eax 
        mov     ecx, 3                     ;3B per Day String 
        cld                                ;Copy Left to Right 
        rep                                ;    (esi++, edi++) 
        movsb

    Anyway, I now like C++ "for me" and this is something I always wondered about how to do in C++ and I need to see how it is done. It will answer many other questions for me just by looking at the code (based on the VERY FEW changes you can offer for the original code I posted).

    Could someone show me the syntax to make this work as I indicated in my first post. As you see, I came this far own my own with vector and it's FAR beyond what is required for class ... It's only required for me.. Class section "strings" only lead me to ask the more important question that even Harvard may NEVER get around to doing in a hundred years.

    Btw: Thanks auralius, that's the best clue I got and I been trying everthing I can think of instead of being a sitting duck trying to use ifstreams. Anyway, it seems like a fair question to me to ask this forum.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    I had the right idea but presented a bad example. I mainly need to know how to do this using C and C++ standard string libraries. This will leave me with a clear understanding of how to switch back and forth from external files usage to an internal arrays of strings when needed for difference types of coding project. But after more googling I see vector may even need boost and I don't think I even need vector to achieve what I am after and no one would have a clue because the vector code I posted would be mis-leading because of read-line I guest, so forget it.

    Thanks anyway

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    90
    Thank you Google

    Another Masterpiece!!!


    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    
    void ShowTime (const vector<string>& j)
         {
         for (int i = 0; i < j.size(); ++i)
         {
         cout << "  " << j[i] << endl;    }   }
    
    int main ()
        {
        vector<string> str1(10);
    
        cout << endl << endl;
     
        str1[0] = "sing_C++";
        str1[1] = "sing";
        str1[2] = "Sing_C++";
        str1[3] = "or";
        str1[4] = "to";
        str1[5] = "the";
        str1[6] = "slammer";
        str1[7] = "you";
        str1[8] = "GO!";   
        str1[9] = " ";
                     ShowTime (str1);
    
        vector<string> str2;// vector with size zero, will grow to accommodate
    
        str2.push_back(" ");
        str2.push_back("la");
        str2.push_back("la");
        str2.push_back("la");
        str2.push_back("lalalaaaa");
                     ShowTime (str2);
    
        cout << endl << endl;
    
    system("pause");
    system ("CLS");
    //return main();
    }
    This is pure C++
    Two down three to go

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading file characters into an int array
    By spongefreddie in forum C Programming
    Replies: 9
    Last Post: 10-28-2010, 01:23 PM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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