0

How to save this content in text file? in C#

using System;
using System.Collections.Generic;
using System.Text;

namespace LinkList
{
    class Program
    {
        static void Main(string[] args)
        {

            list x = null;
            list first;
            Random rnd = new Random();
            x = new list();
            first = x;
            x.data = rnd.Next(20, 500);
            x.next = null;
            for (int i = 0; i < 20; i++)
            {
                x.next = new list(); //create new node
                x = x.next;
                x.next = null;
                //x.data = System.Convert.ToInt32(Console.ReadLine());
                x.data = rnd.Next(20, 500);
            }
            x = first;
            int count = 0;
            int y;
            while (x != null)
            {
                Console.WriteLine(x.data);
                x = x.next;

            }
        }
    }
    class list
    {
        public int data; //4 byte
        public list next;  // 4 byte
    }
}
Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
mahdi
  • 1
  • 2
  • Possible duplicate of [Easiest way to read from and write to files](http://stackoverflow.com/questions/7569904/easiest-way-to-read-from-and-write-to-files) – Mostafiz May 07 '16 at 12:02

1 Answers1

0

One possible way would be to serialize it. JSON is a pretty standard format, so you could use a JSON serializer such as Newtonsoft.JSON:

string json = JsonConvert.SerializeObject(first);
File.WriteAllText("list.txt", json);

Or if you don't want to use third party libraries you could use the JavaScriptSerializer class that's built into the framework to achieve the same:

string json = new JavaScriptSerializer().Serialize(first);
File.WriteAllText("list.txt", json);

If you prefer XML as serialization format you could do this instead:

var serializer = new XmlSerializer(typeof(list));
using (var output = File.OpenWrite("list.xml"))
{
    serializer.Serialize(output, first);
}

For this to work you might need to make the list class public because in your example it is internal.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Where should I use ? – mahdi May 07 '16 at 12:06
  • Wherever you want to save the `list` instance to a file. For example once you have constructed it, after your `while` loop. – Darin Dimitrov May 07 '16 at 12:07
  • I want the contents of the linked list in that file . in c# – mahdi May 07 '16 at 12:10
  • Yes, and that's exactly what my answer illustrates. Use one of the proposed serializers as shown in my answer in order to save your linked list instance in a file. – Darin Dimitrov May 07 '16 at 12:12
  • Like this: `var serializer = new XmlSerializer(typeof(list)); using (var output = File.OpenWrite("list.xml")) { serializer.Serialize(output, first); }`. Did you even read my answer? It contains the code you need. – Darin Dimitrov May 07 '16 at 12:13
  • have error Error 1 A namespace does not directly contain members such as fields or methods C:\Users\ghost\Documents\Visual Studio 2008\Projects\ConsoleApplication7\ConsoleApplication7\Program.cs 41 1 ConsoleApplication7 and – mahdi May 07 '16 at 12:16
  • You need to put this code **inside** the `Main` method of your program, just after the `while` loop. It could be the last instruction in your `Main` method. – Darin Dimitrov May 07 '16 at 12:16
  • thank you but is not save list.xml is a my txt file? – mahdi May 07 '16 at 12:21
  • It's an XML format. You could name it `list.txt` if you want but still it will contain XML. If you prefer JSON then you could use a JSON serializer. – Darin Dimitrov May 07 '16 at 12:21
  • That would not be necessary as it is obvious. No need to copy paste. The XML file will be created at the same directory in which your executable is started. You could provide an absolute path of you want: `File.OpenWrite(@"c:\work\list.xml")`. – Darin Dimitrov May 07 '16 at 12:22
  • using System; using System.Collections.Generic; using System.Text; namespace LinkList { class Program { static void Main(string[] args) { – mahdi May 07 '16 at 12:23
  • list x = null; list first; Random rnd = new Random(); x = new list(); first = x; x.data = rnd.Next(20, 500); x.next = null; for (int i = 0; i < 20; i++) { x.next = new list(); //create new node x = x.next; – mahdi May 07 '16 at 12:23
  • x.next = null; //x.data = System.Convert.ToInt32(Console.ReadLine()); x.data = rnd.Next(20, 500); } x = first; int count = 0; int y; – mahdi May 07 '16 at 12:23
  • Error 1 The type or namespace name 'XmlSerializer' could not be found (are you missing a using directive or an assembly reference?) C:\Users\ghost\Documents\Visual Studio 2008\Projects\ConsoleApplication7\ConsoleApplication7\Program.cs 35 38 ConsoleApplication7 – mahdi May 07 '16 at 12:30
  • Error 2 The name 'File' does not exist in the current context C:\Users\ghost\Documents\Visual Studio 2008\Projects\ConsoleApplication7\ConsoleApplication7\Program.cs 35 87 ConsoleApplication7 – mahdi May 07 '16 at 12:30
  • Did you add `using System.Xml.Serialization;`? – Darin Dimitrov May 07 '16 at 12:31
  • and this code var serializer = new XmlSerializer(typeof(list)); using (var output = File.OpenWrite(@"c:\work\list.xml")) { serializer.Serialize(output, first); } – mahdi May 07 '16 at 12:31
  • Did you add `using System.IO;`? – Darin Dimitrov May 07 '16 at 12:31
  • Do you know how to use Visual Studio to suggest you automatically adding namespaces for unknown classes? You are better of reading about those things if you would be getting into .NET development. – Darin Dimitrov May 07 '16 at 12:31
  • You put the cursor over the unknown class (for example `XmlSerializer`), press `Shift+Alt+F10` and a context menu will appear with suggestions. One of the suggestions will be to add the proper `using` and thus bring the class into scope. – Darin Dimitrov May 07 '16 at 12:39
  • please write this program and give me. – mahdi May 07 '16 at 12:41
  • I am afraid that this is not how StackOverflow works. You might need to rent-a-coder for those kind of services as they are not offered here. – Darin Dimitrov May 07 '16 at 12:47