0

I have a program that displays books from a list (Windows Forms). You can view the information about each one when selected and add a book as well. The format in the text file the program is reading from is:

(Type,Book Name,Year,Authors,Page Count)

Example:

Book,Summertime,2014,Pete Bear/Douglas Patrick,411

Since this book has more than one author, the delimiter is '/' and all authors are put into a list called Books, from the class Book. Since Books.Authors is a list rather than a string object, I have to use a method to put in the file output. How can I finish this method to associate it properly?

 private void SaveFile()
        {

            //  Declare file destination and empty
            System.IO.File.WriteAllText("myBooks2.txt", string.Empty);

            // Declare a StreamWriter variable.
            StreamWriter outputFile;

            // Create a file and get a StreamWriter object.
            outputFile = File.AppendText("myBooks2.txt");

            //  For each book item existing, write the outputs
            for (int i = 0; i < Books.Count; i++)
            {
                outputFile.WriteLine(Books[i].Type + "," + Books[i].Title + "," + Books[i].Year + "," + getElementsInList(Books[i].Authors) + "," + Books[i].Pages);
            }

Method for author list

private string getElementsInList(List<string> aList)
        {
            string elements = "";

            for (int i = 0; i < aList.Count; i++)
            {
                
            }
            return elements;
        }
Bly
  • 3
  • 1
  • 1
    [String.Join Method](https://learn.microsoft.com/en-us/dotnet/api/system.string.join?view=net-5.0) – 001 Sep 03 '21 at 17:09
  • 2
    Why are you creating your own custom file format when working tested packages for formats like [CSV](https://stackoverflow.com/questions/1684667/) have been around for decades? – Dour High Arch Sep 03 '21 at 17:12
  • @DourHighArch The author essentially uses CSV, except for custom list with the "/" delimiter. Are you sure lists are supported in CSV? – TheNightdrivingAvenger Sep 03 '21 at 17:24
  • 1
    It sounds like you should be serializing and deserializing JSON, not trying to create your own file format. – Daniel Mann Sep 03 '21 at 17:41
  • 1
    I suspect this is homework, and if so, you can discard this advice in favor of the homework rules. In the "real" world, don't make *any* assumptions about what characters won't be in a name. You'll eventually be proven wrong, and it'll break something important at 2AM on Saturday. Any protests about invalid characters will be met with "you don't get to tell me how my name is spelled" – Gus Sep 03 '21 at 18:10

1 Answers1

1

You could simply use Linq for this. ie:

void Main()
{
    var books = new List<Book> {
        new Book {Type="Book", Name="Summertime",Year=2014,Authors=new List<string> {"Pete Bear", "Douglas Patrick"},Pages=411},
        new Book {Type="Book", Name="My Book",Year=2021,Authors=new List<string> {"You", "Me", "Him"},Pages=100},
        new Book {Type="Book", Name="My Book #2",Year=2021,Authors=new List<string> {"Me"},Pages=100},
    };

    File.WriteAllLines(@"c:\temp\MyCSV.txt",
        books.Select(b => $@"{b.Type},{b.Name},{b.Year},{string.Join("/", b.Authors)},{b.Pages}"));
}


public class Book
{ 
    public string Type { get; set; }
    public string Name { get; set; }
    public int Year { get; set; }
    public int Pages { get; set; }
    public List<string> Authors { get; set; }
}

However, I would strongly suggest you to use a database instead, say postgreSQL or LiteDb, SQLite ...

Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39