Thread: Array to .txt file

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

    Array to .txt file

    I have an array like this. ReadLines is the array[]:

    Code:
    array<System::String^>^ ReadLines =  gcnew array<System::String^> ( textBox2->Lines->Length ); 
    ReadLines = textBox2->Lines;
    
    System::String ^ Name1 = textBox3->Text;
    
    StreamWriter File = gcnew FileStream("C:\\file5.txt", FileMode::Append);
    I have managed to create a file(file5.txt) under c:\\

    What I want to do now is to put all the content from the array into a this .txt file under c:\\ .
    Also instead for the name file5.txt, I want this name to be what is written in textBox2.
    Last edited by Coding; 02-14-2008 at 05:46 PM.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    well, even though this doesn't look like c#...
    Code:
    string fileName = "C:\\" + textBox3.text + ".txt";
    
    StreamWriter myFile = new FileStream(fileName);
    That should solve the filename.

    Then:

    Code:
    for(int i = 0; i < myArray.Length;i++)
    {
    myFile.WriteLine(myArray[i]);
    }
    myFile.Close();
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    Yes I should be in the C++. Thank you. That did solve. So the finished code look like this:

    Code:
    array<System::String^>^ ReadLines =  gcnew array<System::String^> ( textBox2->Lines->Length ); 
    			
    ReadLines = textBox2->Lines;
    
    System::String ^ Name1 = textBox3->Text;
    System::String ^ Name2 = ReadLines[0];
    			
    			
    String^ path = "c:\\" + textBox3->Text + ".txt";
    		
    
    StreamWriter File = gcnew FileStream(path, FileMode::Append);
    
    
    	for(int count = 0; count < textBox2->Lines->Length; count++)
    	{
    		File.WriteLine(ReadLines[count]);
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM