> For the complete documentation index, see [llms.txt](https://swonh.gitbook.io/sdmp-user-manual-eng/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://swonh.gitbook.io/sdmp-user-manual-eng/data-handling/writing-output-data/writing-to-file.md).

# Writing to File

### 1. Write to file

Once you have added all the data to the `OutputTable`, you can write the data to a file. The output data file is in .csv format and is stored in the solution path Output folder. After registering the data table in the `OutputManager`, it can be written to a file using the `WriteToFile` method.

```csharp
OutputTable outputTable = new OutputTable();

SampleOutputData row = new SampleOutputData(); // Create data row

row.COL_1 = 1;
row.COL_2 = 2;

outputTable.AddRow(row); // Add data row to table

OutputManager.Instance.SetOutput(outputTable.Name, outputTable); // Register table in OutputManager
outputTable.WriteToFile(); // Write to .csv file
```

{% hint style="info" %}
When writing a file, if a file with the same name exists in the path, it defaults to deleting the existing file and creating a new one.
{% endhint %}

When writing to a file, the `WriteToFile` method provides the following functionality

#### Appending to the last line of an existing file

If a file with the same filename exists, you can append to the contents of the existing file to output data as needed without deleting the existing file. This can be accomplished by setting the value of the **isAppend** parameter of the `WriteToFile` method to True. The example code below outputs data following the last line of an existing file.

```csharp
outputTable.WriteToFile(path, true); // Append data to an existing file
```

#### Setting whether to add column names for the first row

When writing to a file, the default is to write the column names of the corresponding output data schema in the first row. However, you can choose not to write the column names in the first row as needed. You can accomplish this by setting the value of the **isHeaderInclude** parameter of the `WriteToFile` method to False. The example code below prevents the column names from being written to the first row.

```csharp
outputTable.WriteToFile(path, false, false); // Not include header (column names) 
```

#### Setting file name

When writing to a file, it defaults to writing the file name to be the same as the output data schema name. Users can change the output file name as needed. This can be accomplished by setting the file name you want to change in the value of the **name** parameter of the `WriteToFile` method. The example code below renames the file to "Result".

```csharp
outputTable.WriteToFile(path, false, true, "Result"); // Set file name to "Result"
```

### 2. Example code

The following example code outputs the contents of the Best Solution in the [`OnDoneSolve`](/sdmp-user-manual-eng/general-module/user-controls/eventcontrol.md#ondonesolve) method, which is called after the end of the Solver run. It defines the Result output data schema to correspond to the Solution structure and adds data rows to the `OutputTable`. After adding the data is complete, write that data table to a file.

```csharp
public override void OnDoneSolve()
{
    OutputManager outputManager = OutputManager.Instance;

    OutputTable resultsTable = new OutputTable();

    SolutionManager solutionManager = SolutionManager.Instance;
    Solution bestSol = solutionManager.BestSolution;

    IOrderedEnumerable<KeyValuePair<int, State>> states = bestSol.States.OrderBy(x => x.Key);

    int seq = 1;
    foreach (KeyValuePair<int, State> item in states)
    {
        CRPState state = item.Value as CRPState;

        if (state.IsInitial)
            continue;

        Result row = new Result();

        row.SEQUENCE = seq;
        row.CONVEYOR = state.CurrentConveyor.ConveyorNum;
        row.JOB = state.LastRetrievedJob.Number;
        row.COLOR = state.LastRetrievedJob.Color.ColorNumber;

        resultsTable.AddRow(row);

        seq++;
    }

    outputManager.SetOutput(resultsTable.Name, resultsTable);
    resultsTable.WriteToFile();
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://swonh.gitbook.io/sdmp-user-manual-eng/data-handling/writing-output-data/writing-to-file.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
