********IT WORKS! But you have to type in the TextBox before anything shows. I guess that's because I used the 'TextChanged' event handler. Which event handler do I use if I want it to show the contents of the text file without user interaction?********
So I want to write some data to a text file in a C# Windows 10 Universal Platform App when a button is pressed, and I want a TextBlock or a TextBox to read the contents of that text file in my app.
I'm using a pivot-style app, the button to write the file is on one pivot and the TextBlock or TextBox I want to contain the contents of the text file is on another pivot.
My code is below. It doesn't work. I'm not sure it's even creating and writing the file and there is nothing in either my TextBox or TextBlock. :(
I got the code from here: https://msdn.microsoft.com/en-us/library/windows/apps/mt185401.aspx
Code to write the file:
private async void submitButton_Click(object sender, RoutedEventArgs e)
{
//WRITE THE TICKET TO A LOCAL DATABASE (txt)//
//Create the text file to hold the data
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile ticketsFile =
await storageFolder.CreateFileAsync("tickets.txt",
Windows.Storage.CreationCollisionOption.ReplaceExisting);
//Write data to the file
await Windows.Storage.FileIO.WriteTextAsync(ticketsFile, "Swift as a shadow");
}
Code to read the file in a TextBox:
private async void viewTickets_TextChanged(object sender, TextChangedEventArgs e)
{
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile ticketsFile =
await storageFolder.GetFileAsync("tickets.txt");
string savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile);
viewTickets.Text = savedTickets;
}
You can even use fputs(). Only strings can be read or write instead of integers, float or characters. 18) What are the C functions used to read or write a file in Binary Mode.? fwrite(pointer, size, count , filepointer);
Getc and putc functions are used to read and write a single character. We can write to a file after creating its name, by using the function fprintf() and it must have the newline character at the end of the string text.
Your code is perfectly fine, the only problem is it doesn't get executed. When you click on the button
your file is created. But you don't type anything in the textbox
so you never read the file.
I think you want to read it immediately after writing it. Put your Read
file code right after the Write
code:
private async void submitButton_Click(object sender, RoutedEventArgs e)
{
//WRITE THE TICKET TO A LOCAL DATABASE (txt)//
//Create the text file to hold the data
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile ticketsFile = await storageFolder.CreateFileAsync("tickets.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);
//Write data to the file
await Windows.Storage.FileIO.WriteTextAsync(ticketsFile, "Swift as a shadow");
//read file
string savedTickets = await Windows.Storage.FileIO.ReadTextAsync(ticketsFile);
viewTickets.Text = savedTickets;
}
and remove the viewTickets_TextChanged
event handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With