[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

String saved to a .txt file

Posted on 2009-07-06
8
Medium Priority
?
361 Views
Last Modified: 2012-06-27
I currently have a string array that has data in each section as a comma sepparated list.

I need to pull this data into a txt file for later use.

how is this done.
0
Comment
Question by:cotj73
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 400 total points
ID: 24791387
// This might be wrong, I am writing this quick before heading out.


// Something like this
using System.IO;


StreamWriter file = new StreamWriter("out.txt");

string [] strings = {"red", "blue"};

foreach(String s in strings) {
   file.WriteLine(s);
}
0
 
LVL 1

Author Comment

by:cotj73
ID: 24791416
I tried this, but when i look at the file it is blank.
Also how do i determine where this out.txt will be saved
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 24791489
file.Close() is missing at the end of code.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Assisted Solution

by:drypz
drypz earned 400 total points
ID: 24791490
hi! try this...

        private void button1_Click(object sender, EventArgs e)
        {
            string[] strArray = new string[] {"val1","val2","val3"};
            string filePath = Application.StartupPath + @"\Output.txt";

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath))
            {
                foreach (string value in strArray)
                {
                    sw.WriteLine(value);
                }
            }
        }

You can change the file path if you want.
0
 
LVL 1

Assisted Solution

by:vaibhav_dw
vaibhav_dw earned 800 total points
ID: 24791500
Hi cotj73

Try with attached code.
You can check if the file is existing and open it FileMode.Append mode.

string[] arr = { "text1", "text2" };
 
FileStream f = new FileStream("d://output.txt", FileMode.);
StreamWriter s = new StreamWriter(f);
 
foreach (string temp in arr)
{
    s.WriteLine(temp);
}
 
s.Close();
f.Close();
    

Open in new window

0
 
LVL 1

Assisted Solution

by:vaibhav_dw
vaibhav_dw earned 800 total points
ID: 24791542
Hi

I just missed the mode in above answer.
Dont forget to close both the streams.
string[] arr = { "text1", "text2" };
 
FileStream f = new FileStream("d://output.txt", FileMode.Create);
StreamWriter s = new StreamWriter(f);
 
foreach (string temp in arr)
{
    s.WriteLine(temp);
}
 
s.Close();
f.Close();

Open in new window

0
 
LVL 20

Assisted Solution

by:ddayx10
ddayx10 earned 400 total points
ID: 24791552
I think we're all on the same path here,but if that gives you any trouble here is another (lazy) way to go. I dunno how you're trying to layout the text but this should give ya the idea:

        string[] myArray = new string[]{"One,Two","Buckle,My,Shoe"};

        StreamWriter sw = File.CreateText(Server.MapPath("~/files/") + "newFile.txt");
        foreach (string str in myArray)
        {
            string split = ",";
            char[] c = split.ToCharArray();
            string[] myArray2 = str.Split(c);
            foreach (string str2 in myArray2)
            {
                sw.WriteLine(str2);
            }
        }
        sw.Close();
0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24791557
I think it goes without saying that for all these solutions you need to get the namespace:

using system.io;
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question