Listado de la etiqueta: File


“SQLite allows us to run commands straight from a file. This is particularly beneficial if you have a lengthy program, such as establishing several different tables and populating them with data. While using the SQLite command prompt shell, use the .mode dot command in association with the .output or .merienda commands to output your query results to a file, or use the .read command to read the SQL command from the file in the SQLite shell. Here, we will see how to execute the SQL command in the SQLite shell and show the results in the file.

We must first install the SQLite3 shell application before we can use it. If SQLite is installed, then check that the program’s location has been added to our particular PATH environment variable since this will make it easier to access the database file as well as other files we might need. Then, cd to the database file’s folder from a command prompt. Make sure the file is in the same directory folder as the SQLite database you generated.”

Use sqlite3 as a command; this should bring up the SQLite shell, with a screen similar to the one seen below. First, we have specified the path of the folder where our file is then created a database name “hospital.db” by using the sqlite3 command.

The database is successfully created in SQLite; you can see it by using a .databases command in the shell. Now, we have a CREATE statement which is used to create a table in the given database. We created a table with the name “patients.” This table keeps the record of the patients, which includes ID as the primary key with the data type INT, NAME with the CHAR data type, and WARD with the type TEXT.

CREATE TABLE patients(
      ID             INT     ,
      NAME           CHAR(10)   ,
      WARD          FLAOT    
    );

The table is being built, and the columns are being defined. Using the INSERT command, we have now included the data for each column.

INSERT INTO patients VALUES (1, ‘Nayab’, ‘medical’);
INSERT INTO patients VALUES (2, ‘Sadia’,‘cardiology’ );
INSERT INTO patients VALUES (3, ‘Aman’,‘neurosurgery’);
INSERT INTO patients VALUES (4, ‘Balaj’,‘skin specialist’);
INSERT INTO patients VALUES (5, ‘Raima’,‘urology’);

Example 1
We are running SQL commands here, and the results are printed in the file. The following commands must be used to accomplish this: The .header on the command is used to turn on the result set’s heading.

To direct the sqlite3 tool to provide the result in CSV mode, specify the output mode to CSV.

The .output FILENAME command is used to save the outcome of a query to a file. Following the .output command, all subsequent queries’ results will be stored in the file you selected in the FILENAME parameter. The .merienda FILENAME command is used if you just wish to save the outcome of another single query to a file.

We can use the .output command without any arguments to re-display the query’s result to the standard output.

.headers ON
.mode csv
.output File.txt

After using the above command, we have a SELECT statement that displays the table and the table record within our file.

The SQL command runs here when we open our file, and the table record is displayed.

Example 2
As in the above example, we have shown how to insert the record from the shell to the file. Here, we have a SQL select command in the file. This command fetches all the records in the table patients. We have used the method .read file name.

The File.txt script reads from the current folder/directory in this case. If in a separate directory, specify the entire path. By using the .read File.txt in the SQLite shell, the table is viewable here as we have executed the command from the file.

SELECT NAME, WARD
FROM patients
ORDER BY NAME
LIMIT 4;

Example 3
Here also, we are executing the SQL command from the file but with the .timer command in the shell. You can use the .timer command to activate or deactivate CPU time measurements. SQLite will maestro and report the operating system duration needed to process each query if this option is enabled. Firstly, we have the UPDATE command, which sets the NAME = UZAMA with the WHERE CLAUSE, which has the ID = 1 in the File.txt.

UPDATE patients SET NAME = ‘Uzama’ WHERE ID = 1

Now, we have set the header on command and also the timer on command. Then, use the .read File.txt to display the results of the command from the File.txt. Notice that the timer is on and showing the record with the updated values as follows:

.header ON
.mode COLUMN
.timer ON
.read File.txt

Example 4
Here’s a different approach to using the .read command. The following SQL command from the File.txt, which we are executing in the SQLite shell.

Without having to open SQLite, you can use the .read command. The contrast between this case and the last one is that the last one was done after you’d already connected to SQLite from within SQLite. This example, however, is executed from outside of SQLite. We have given the command “sqlite3 hospital.db “.read File.txt” within the folder where the database and file are present. The results are represented in the image below.

sqlite3 hospital.db «.read File.txt»

Example 5
This is our third method for executing SQL commands from the file. The command in File.txt is as follows:

SELECT *FROM patients
WHERE ID < 3
ORDER BY NAME ASC;

When connecting to SQLite and opening a database with sqlite3, the following approach forwards the information to the database file.

sqlite3 hospital.db< File.txt

Example 6
Another way to execute SQL commands from the file is by using the init method. Below is the SQL command, which will be executed by the init method.

SELECT WARD FROM patients;

Hence, the command “sqlite3 hospital.db -init File.txt” also executes the SQL command from the file as shown in the snapshot.

sqlite3 hospital.db init File.txt

Conclusion

In the end, we have shown the four different ways which execute SQL commands from the file. First, we have to place the file and the database on the same path; then, we can execute the several SQL commands successfully. Each method is implemented by displaying the output.



Source link


“In this article, the different examples for opening a file will be discussed and implemented in the C# programming language by using the Ubuntu 20.04 command line terminal. The use of classes like FileStream and Stream Writer will be helpful while creating, opening, and reading files, as we will see later on in the different examples. These classes are a part of the namespace System.IO, which has different classes related to all input and output functions.”

The File.Open() Method in the C# Program

The File.Open() method is inherent to the File Stream Class and is accessed by creating an object of the class. The syntax for opening a file in the C# programming language is shown below:

File Stream is a part of the system.IO namespace where File Stream is used for file manipulations like reading, writing, and even finding existing files in a directory. To use File Stream, we will first need to create an object of the File Stream class; the object can be given four parameters that will help us access the files. The FileName parameter consists of the title of the file, while the FileMode parameter specifies the mode in which you want to work on the file. The FileAcess parameter defines the state of the access for a file, and the FileShare parameter is used to determine the sharing of files with other FileStreams objects.

Example 01: Using the File Open Method to Open a Pre-Existent File in Ubuntu 20.04

In this instance, we will be opening a file that is pre-existing in a directory. To access the file, we must provide the file’s name and path to the object of the File stream as a parameter and also give the Mode in which we want to access it.

Text Description automatically generated

In this C# program, we will use the file open method and give it some parameters; one of them will be FileMode.Open. After completing the parameters, we will move on to creating a byte array with the size of “1024”. We will generate an object of the UTF8Encoding class. After that, we will create a read method using the while loop; using the length of the array to run the loop, we will be able to read the file until its end.

Text Description automatically generated

Graphical user interface, text, application Description automatically generated

As we can see in the output screen and the file as well that the Text of the file is the same and was accessed through the File open method.

Example 02: Using the Stream Writer and File Stream Class to Create and Open a File in Ubuntu 20.04

Stream Writer is a class that also belongs to the System.IO namespace. It is used to write characters in a stream in the specified encoding.

Graphical user interface, text, application Description automatically generated

In this C# program, we will generate an object of the class StreamWriter and use the Write() method to write our stream of characters in the file specified. After that, we will use the File.Open function of the File Stream class. Then we will create a byte array and create an object of UTF8encoding class to convert the characters in the file. The While loop will run equal to the length of characters in the file and will exit after reading.

A screenshot of a computer Description automatically generated with medium confidence

As we can see in the output that the file is formed, and the data is saved correctly.

Graphical user interface Description automatically generated with low confidence

Example 03: Using the FileStream Class to Make a Temporary File and Launch it in Ubuntu 20.04

We can use FileStream class to create and access a temporary file. This time we will be using FileStream to create two objects for different purposes. The first time we will build an object to create the temporary file and write some data in it will provide all the parameters, including File access and FileShare too, according to our needs.

A screenshot of a computer Description automatically generated with medium confidence

In this program, we will use the GetTempFileName() method to create a path for and name for the file and FileMode.Open top opens the file FileAccess.Write and FileShare. After this, we will create a Byte array with UTF8encoding; we will also use the GetByte method to allow us to enter data in the file. The Write() function will be used to insert the entered string in a file. Write() method will enter data to the length of the string we gave in Byte Array. Then we will state the File.open method and write all its parameters. After this, we will move on to creating a byte array with a size of “1024”. We will build an object of the UTF8Encoding class.

Text Description automatically generated

As the output suggests, we have opened a text file and successfully added some text to it.

Example 04: Opening and Reading a Text File With Multiple Lines Using the File Stream Class in Ubuntu 20.04

In this instance, we will be opening and accessing a text file. The Read() method is used to read any text.file even with multiple lines using the file stream class, which will be used in this example.

Graphical user interface, text, application Description automatically generated

The Read() method will be used to access the file in this C# program. We will create a byte array with a size of “1024” after finishing the parameters of the File.Open() method. We are going to make a UTF8Encoding class object. Then, using the while loop and the length of the array to execute the loop, we will be able to read the file till it finishes, with the loop ending when all multiple lines have been read.

Text Description automatically generated with medium confidence

The output of the program and the file suggests that the file was accessed and read correctly.

Timeline Description automatically generated with low confidence

Example 05: Opening a Read-only Text File Using the File Stream Class in Ubuntu 20.04

In this instance, we will be focusing on how to access a read-only file using the File Stream class. We will use the File.Open() method to open the file and also try to access and edit the file and see how the compiler will react to this command.

Text Description automatically generated

In this C# program, we will first create a file. Now we will build an object from the FileStream class; this object will be used to access and read the file we just created. To access the file, we will call the File.Open() method and fill the necessary parameters. After that, we will create a read method using the while loop; using the length of the array to run the loop, we will be able to read the file until its end. We will be performing exception handling by using the try and catch method to see if we are allowed to write on the file or not.

Text Description automatically generated

As the output suggests, we can see that the system did not allow us to write in the file, and an error is thrown “Stream does not support writing.”

Conclusion

In this article, we have deliberated the method for opening a file in the C# programming language. We used the File Stream and the Stream Writer class present in the System.IO namespace to create and open files in the C# programming language. We also implemented several examples of opening files in different conditions and several types of data in the file by using the Ubuntu 20.04 Command-line terminal.



Source link


“In our life, we can rename a file by using manual methods. But here in C# language, we have different methods to rename our existing file. The C# programming allows us to rename a file using different built-in methods. We can easily rename our existing file by writing a simple code. In this tutorial, we will explain to you in a very easy and understandable way how you can rename your existing file by writing a few lines of code in C# programming. We demonstrate different examples of using different methods to rename our file.”

Methods for Renaming a File in C#

Example # 1: Using Move() Method

In this given example, we are going to use the Move() method for renaming a file in C# programming. We perform these examples in Ubuntu 20.04 text editor. The file extension for this C# code is “.cs.” So, you must save your file with this file extension.

We start our C# code with “using System.” We use a library in our program to access classes such as the “Console” class and functions such as the “WriteLine” function. We also import “System.IO,” used for input/output. If we don’t use this “System.IO,” the program doesn’t get the file from your system. It gives an error. The namespace is here with the name “Demo.” We also declare a class with the name “Program” and call our “main” function. Our C# program starts from this “main” function when executing our code.

Here, we just give the file name because this C# code and the file we want to rename are in the same directory. If you want to rename a file in the other directory, you must give the proper path of the directory. After this, we declare another variable of a string data type with the name “location2” and give the file’s new name. Here we want to change the name of file “MyProgram.txt” file to “MyFirstProgram.txt,” so we store this name in our string variable “location2”.

Now, we use File.Move() method for renaming the existing file. In “File.Move(location1, location2)”, we give our string variables so that it renames our location1 file with the location2 file name. When it moves this file and renames it, then we have to delete this “location1” file, so for this, we use “File.Delete(location1)”. It will delete our previous file and save it with the new name, which means it renames our existing file. After renaming the file, we want to print a line on the screen, so for this, we use the “Console.WriteLine()” method and then close all the brackets and save this code.

We can get the output of this code by using the “mcs” command for compiling our code and the “chimpancé” command for executing this compiled code in Ubuntu 20.04. When this code is compiled and executed successfully, we get the output, and our existing file is renamed.

In this output, you can see that both commands run successfully, and the existing file is renamed, and then it prints the line on the terminal that is “The file successfully renamed.”

Example # 2: Using the Copy() Method

We give another example of using the Copy() method for renaming our existing file in C# programming. Check this example of how this method renames a file.

In this example, the “namespace” is named “Csharp,” and the class name here is “Example.” After invoking the “main” function, we are going to declare two variables of the “string” data type with the name “loc1” and “loc2,” respectively. We store the path of the existing file which we want to rename in “loc1,” which is “MyFirstProgram.txt,” and we want to change this file name and give a new name to this file which is “MyCsharpProgram.cs”, so we store this new file name and path to the second “string” variable “loc2”. We must put the “@” sign whenever we give any file path.

Here, we give the filename because this code file and the file we want to rename are saved in the same directory as we explained in the previous example. After this, we use the Copy() method. The copy() method is used for renaming a file. It will copy the existing file to a new file and change the new file’s name. Here, we have “File.Copy(loc1, loc2, true),” which means if we have this file whose path is given in “loc1,” then this file is copied and saved with the new name which we give in the “loc2” string variable.

After copying this, we deleted our previous file, which is stored in “loc1” by using the “File.Delete(loc1)” method. After successfully renaming the file, we have to print one line on the screen, which is “The file Renamed using Copy ()” with the help of the “Console.WriteLine()” function. The output of this code is given in the image below:

Example # 3

In this example, we give the file name that doesn’t exist in the given directory and use the try-catch method, which displays an exception. Let’s try this example of how it shows an exception when the file is not found.

Here we declare a public class “Csharp” and have a “main” function inside this “Csharp class.” We have two string variables named “source” and “destination.” We initialize the “source” string variable with the file name, which doesn’t exist in the directory. And initialize the second variable, “destination,” with the new file name. After this, we utilize the try-catch method here. The “try” method works when the file exists in the directory; otherwise, it moves to the catch statement, which throws the exception.

In this try method, we have the File.Move() method, which we discussed in our first example. This method works if the file exists. If the file exists, then it moves the file and renames it. We have an “if” statement that checks if the file saved in the “source” variable does not exist, which means; the file is renamed. In the catch statement, we have “IOException,” which throws an exception while accessing the file. And stores this exception in “e.” We will print the exception also by using “ConsoleWriteLine.”

In the output, we see that it throws an exception that “file not found” because the path or name of the file is not correct, so it throws an exception. We must give the correct name and path for renaming that file; otherwise, it throws an exception.

Example # 4

We use the same example, i.e., example 3, and give the correct file name for renaming it. In this example, we give the name of the file that exists in the directory, and we rename the file with the help of the same code we discussed in example 3.

This code is the same as discussed in our previous example 3. But here, we store the correct file in the “source” variable and rename this file by using the same code. We discuss this code in example 3 in detail.

Just look at the output. If we give the correct path and correct file name for renaming the file, then our file is renamed successfully, and it doesn’t throw an exception.

Conclusion

In this tutorial, we have explained how to rename a file in C# programming. We have discussed this renaming file concept in detail and show you how to give the path and the file name for renaming the existing file. In this tutorial, we have used two different methods for renaming the existing file. We also show you what happens if the file does not exist. And how it throws exceptions when the file does not exist in the directory. I hope you will understand this tutorial, and it will be helpful for you when you want to rename your existing file.



Source link


We will be discussing different methods to create a text file through a C# program in this article. The C# programming language allows us to create, edit and update text files using special classes like the Stream Writer, Stream Reader, and File Stream present in the System.IO namespace. These classes are specially made to perform all types of operations on a file that we could use for creating or editing during a C# program. All the input and output operations for a text file in the C# programming language are stored in the System.IO namespace.

We will now discuss several C# programming language methods and implement them in the Ubuntu 20.04 environment.

Method 01: Using the Stream Writer and Stream Reader Classes in Ubuntu 20.04 to Create and Read Text Files

In this method, we will be using the Stream Writer and Stream Reader class to create a text file while adding some text to them. The Stream writer class is a part of the System.IO namespace and is used for input functions in a text file, while the Stream Reader class, which is also present in the same namespace, is used for reading the text file in the C# programming language. Both these classes will help us in achieving our goal in this method.

Text Description automatically generated

The above C# program starts with the initialization of a string variable named “file Name.” We gave this variable the text file name that we will be creating and reading on our device. Then we will be initiating the creation of the object of the Stream Writer class, which will have the File.CreateText Function, and this function will have the string variable “file Name” which has the name of the text file. Within the parameters of the stream writer class’s object, we have used the object to call the Write Line method to add some lines to the new text file created.

After this, we will be initiating an object for the Stream Reader class, which will have the Open Text function with the File name string variable; this function will give us access to the text file. Now we will initiate a string variable and put it in a while loop which will have the stream reader object assigning all the text of the file to the string variable, and it will all be printed using the Console Write Line function.

Text Description automatically generated

After compiling and executing the previous code, we will get the above output. In this snippet, we can see the output of the while loop in the Stream Reader function, which reads the text file and prints its content. We will look at the text file that we created and verify that the text was added successfully.

Graphical user interface, website Description automatically generated

Method 02: Using File Stream and Stream Reader Class to Create and Read a Text File in Ubuntu 20.04

In this technique, we will create a text file and add some text to it using the File Stream and Stream Reader classes. For text file input methods, the File Stream class from the System.IO namespace will be utilized, while the Stream Reader class from the same namespace will be used to read the text file in this method.

Text Description automatically generated

The C# code above starts with initializing a string variable named “f.” This variable is named after the text file we will be creating. Then we will start the Stream Writer class object, which will have a “File.CreateText Function” with a string variable containing the file name. We have used the object to use the Write Line function to add some lines to the new text file generated using the stream writer class’s object’s arguments.

After that, we will create an object for the Stream Reader class using the “Open Text” method and the File name string variable, which will allow us to read the text file. Now we will create a string variable and place it in a while loop, with the stream reader object assigning all of the file’s text to the string variable and printing it all using the “Console Write Line” method.

Text Description automatically generated

We will obtain the above output after compiling and running the prior code. The result of the while loop of the Stream Reader function, which reads the text file and displays its content, can be seen in this snippet. We will now examine the text file we prepared.

Graphical user interface, application, website Description automatically generated

The text file has the line that we added to it using the Stream Writer Class object, as we can see in the following example.

Method 03: Using File Security and File Stream Class to Create a Text File With Secure Access in Ubuntu 20.04

In this method, we will be using the File Security class to restrict the access of a text file, and only specified users will be allowed to open and edit the file. The File Security class and the File Stream Class are both found in the System.IO namespace and will be utilized to create a text file on our system in this function.

A screenshot of a computer Description automatically generated with medium confidence

In this C# program, we will initialize a string variable with the name of the text file stored in it. Then we will create an object of the File Security class and use it for the “Add Access Rule function”, which is the further inheritance for the File System Access Rule; this will allocate the new text file its access rights. After this, we will create an object of the File Stream class to create a text file, and in the parameters, we will pass the object of the File Security class along with file size and other details. Then with the help of the Byte array, we will add some text to the file as well.

Text Description automatically generated

In this output, we can see that the text file was created successfully as the file contents were displayed in the terminal.

Method 04: Using File.Write All Text to Create and Add Text to a Text file in Ubuntu 20.04

In this method, we will be using the functions of the System.Text namespace to create a text file. This namespace in the C# Programming language has all the functionalities related to textual operations. In this method, we will use the Write All Text function of the System.Text namespace to create and add text to a new text file.

Text Description automatically generated

In this C# program, we will initialize two string variables, one with the file name and the other with the text that we want to add to the file. Then we will call the Write All Text function with both these string variables as the parameter.

After executing the above program, we will get the following output:

A screenshot of a computer Description automatically generated with low confidence

The terminal and the text file are evidence that the Write All Text function successfully created a text file with some text in it.

A picture containing timeline Description automatically generated

Conclusion

In this article, we discussed several C# programming language methods to create a text file on your device. The use of special classes built for editing and creating text files present in the System.IO namespace, which are some special operations(input and output) libraries related to text files in the C# programming languages, were also discussed in this article. We focused on the function of stream classes to create a file which is the Create() method and was common in several classes. We implemented this method and other text file creating methods in the Ubuntu 20.04 environment to better understand how these different classes and functions work together in the C# programming language.



Source link