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;
        }
5
  • 1
    String.Join Method Commented Sep 3, 2021 at 17:09
  • 2
    Why are you creating your own custom file format when working tested packages for formats like CSV have been around for decades? Commented Sep 3, 2021 at 17:12
  • @DourHighArch The author essentially uses CSV, except for custom list with the "/" delimiter. Are you sure lists are supported in CSV? Commented Sep 3, 2021 at 17:24
  • 1
    It sounds like you should be serializing and deserializing JSON, not trying to create your own file format. Commented Sep 3, 2021 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" Commented Sep 3, 2021 at 18:10

1 Answer 1

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 ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.