To Test to See if a File Is Not Read Only, Use the Following Vbscript Code _____.

C File direction

A File can be used to store a big book of persistent data. Similar many other languages 'C' provides following file direction functions,

  1. Creation of a file
  2. Opening a file
  3. Reading a file
  4. Writing to a file
  5. Closing a file

Post-obit are the well-nigh important file management functions available in 'C,'

function purpose
fopen () Creating a file or opening an existing file
fclose () Closing a file
fprintf () Writing a cake of data to a file
fscanf () Reading a block data from a file
getc () Reads a single character from a file
putc () Writes a single graphic symbol to a file
getw () Reads an integer from a file
putw () Writing an integer to a file
fseek () Sets the position of a file arrow to a specified location
ftell () Returns the current position of a file pointer
rewind () Sets the file pointer at the commencement of a file

In this tutorial, you volition learn-

  • How to Create a File
  • How to Shut a file:
  • Writing to a File
  • fputc() Function:
  • fputs () Part:
  • fprintf()Role:
  • Reading information from a File
  • Interactive File Read and Write with getc and putc

How to Create a File

Whenever you lot desire to work with a file, the commencement step is to create a file. A file is cypher merely space in a memory where information is stored.

To create a file in a 'C' program following syntax is used,

FILE *fp; fp = fopen ("file_name", "manner");          

In the above syntax, the file is a data structure which is defined in the standard library.

fopen is a standard function which is used to open a file.

  • If the file is non nowadays on the organization, then it is created then opened.
  • If a file is already nowadays on the organisation, then it is directly opened using this function.

fp is a file pointer which points to the blazon file.

Whenever yous open or create a file, you have to specify what you lot are going to exercise with the file. A file in 'C' programming tin can be created or opened for reading/writing purposes. A mode is used to specify whether you lot want to open a file for whatever of the beneath-given purposes. Following are the unlike types of modes in 'C' programming which tin exist used while working with a file.

File Way Description
r Open a file for reading. If a file is in reading way, then no information is deleted if a file is already nowadays on a system.
w Open a file for writing. If a file is in writing mode, then a new file is created if a file doesn't be at all. If a file is already present on a organization, so all the information inside the file is truncated, and it is opened for writing purposes.
a Open a file in
append way. If a file is in append mode, so the file is opened. The content within the file doesn't change.
r+ open for reading and writing from beginning
w+ open for reading and writing, overwriting a file
a+ open for reading and writing, appending to file

In the given syntax, the filename and the mode are specified as strings hence they must always exist enclosed within double quotes.

Example:

#include <stdio.h> int primary() { FILE *fp; fp  = fopen ("data.txt", "w"); }          

Output:

File is created in the same binder where you lot have saved your lawmaking.

Y'all tin specify the path where you want to create your file

#include <stdio.h> int main() { FILE *fp; fp  = fopen ("D://data.txt", "w"); }

How to Close a file

I should e'er close a file whenever the operations on file are over. Information technology means the contents and links to the file are terminated. This prevents adventitious harm to the file.

'C' provides the fclose function to perform file closing performance. The syntax of fclose is every bit follows,

fclose (file_pointer);          

Example:

FILE *fp; fp  = fopen ("data.txt", "r"); fclose (fp);          

The fclose part takes a file pointer every bit an statement. The file associated with the file pointer is then airtight with the help of fclose function. Information technology returns 0 if close was successful and EOF (end of file) if there is an mistake has occurred while file closing.

After closing the file, the same file arrow can as well be used with other files.

In 'C' programming, files are automatically close when the program is terminated. Closing a file manually by writing fclose function is a good programming practice.

Writing to a File

In C, when y'all write to a file, newline characters '\n' must be explicitly added.

The stdio library offers the necessary functions to write to a file:

  • fputc(char, file_pointer): Information technology writes a character to the file pointed to by file_pointer.
  • fputs(str, file_pointer): It writes a string to the file pointed to by file_pointer.
  • fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed to by file_pointer. The string can optionally include format specifiers and a list of variables variable_lists.

The program below shows how to perform writing to a file:

fputc() Role:

#include <stdio.h> int principal() {         int i;         FILE * fptr;         char fn[50];         char str[] = "Guru99 Rocks\n";         fptr = fopen("fputc_test.txt", "w"); // "west" defines "writing mode"         for (i = 0; str[i] != '\northward'; i++) {             /* write to file using fputc() part */             fputc(str[i], fptr);         }         fclose(fptr);         return 0;     }

Output:

The above programme writes a single grapheme into the fputc_test.txt file until it reaches the side by side line symbol "\n" which indicates that the sentence was successfully written. The process is to have each character of the assortment and write it into the file.

  1. In the in a higher place program, we accept created and opened a file called fputc_test.txt in a write mode and declare our string which volition be written into the file.
  2. We do a character by character write operation using for loop and put each character in our file until the "\n" character is encountered then the file is closed using the fclose part.

fputs () Function:

#include <stdio.h> int main() {         FILE * fp;         fp = fopen("fputs_test.txt", "w+");         fputs("This is Guru99 Tutorial on fputs,", fp);         fputs("Nosotros don't demand to use for loop\northward", fp);         fputs("Easier than fputc function\n", fp);         fclose(fp);         return (0);     }

OUTPUT:

  1. In the above program, we accept created and opened a file called fputs_test.txt in a write mode.
  2. After we do a write functioning using fputs() function by writing 3 different strings
  3. Then the file is closed using the fclose function.

fprintf()Function:

#include <stdio.h>     int chief() {         FILE *fptr;         fptr = fopen("fprintf_test.txt", "due west"); // "w" defines "writing mode"         /* write to file */         fprintf(fptr, "Learning C with Guru99\north");         fclose(fptr);         return 0;     }

OUTPUT:

  1. In the above programme we have created and opened a file chosen fprintf_test.txt in a write mode.
  2. After a write functioning is performed using fprintf() function past writing a string, then the file is closed using the fclose function.

Reading data from a File

In that location are three different functions dedicated to reading data from a file

  • fgetc(file_pointer): Information technology returns the next character from the file pointed to by the file pointer. When the terminate of the file has been reached, the EOF is sent dorsum.
  • fgets(buffer, n, file_pointer): Information technology reads n-one characters from the file and stores the string in a buffer in which the NULL character '\0' is appended every bit the last character.
  • fscanf(file_pointer, conversion_specifiers, variable_adresses): It is used to parse and clarify data. Information technology reads characters from the file and assigns the input to a list of variable pointers variable_adresses using conversion specifiers. Keep in mind that as with scanf, fscanf stops reading a string when space or newline is encountered.

The following program demonstrates reading from fputs_test.txt file using fgets(),fscanf() and fgetc () functions respectively :

#include <stdio.h> int main() {         FILE * file_pointer;         char buffer[30], c;          file_pointer = fopen("fprintf_test.txt", "r");         printf("----read a line----\n");         fgets(buffer, fifty, file_pointer);         printf("%s\northward", buffer);          printf("----read and parse data----\n");         file_pointer = fopen("fprintf_test.txt", "r"); //reset the pointer         char str1[10], str2[2], str3[20], str4[ii];         fscanf(file_pointer, "%s %southward %s %s", str1, str2, str3, str4);         printf("Read String1 |%s|\n", str1);         printf("Read String2 |%s|\n", str2);         printf("Read String3 |%s|\n", str3);         printf("Read String4 |%s|\northward", str4);          printf("----read the unabridged file----\n");          file_pointer = fopen("fprintf_test.txt", "r"); //reset the arrow         while ((c = getc(file_pointer)) != EOF) printf("%c", c);          fclose(file_pointer);         render 0;     }

Result:

----read a line---- Learning C with Guru99  ----read and parse information---- Read String1 |Learning| Read String2 |C| Read String3 |with| Read String4 |Guru99| ----read the unabridged file---- Learning C with Guru99

  1. In the higher up programme, we have opened the file called "fprintf_test.txt" which was previously written using fprintf() function, and it contains "Learning C with Guru99" cord. We read information technology using the fgets() function which reads line by line where the buffer size must be enough to handle the entire line.
  2. Nosotros reopen the file to reset the pointer file to point at the beginning of the file. Create various strings variables to handle each word separately. Print the variables to run across their contents. The fscanf() is mainly used to extract and parse data from a file.
  3. Reopen the file to reset the pointer file to signal at the beginning of the file. Read data and print it from the file character past character using getc() function until the EOF statement is encountered
  4. Later performing a reading performance file using different variants, we over again closed the file using the fclose function.

Interactive File Read and Write with getc and putc

These are the simplest file operations. Getc stands for go character, and putc stands for put character. These two functions are used to handle only a unmarried character at a fourth dimension.

Following plan demonstrates the file handling functions in 'C' programming:

#include <stdio.h> int principal() {         FILE * fp;         char c;         printf("File Handling\due north");         //open a file         fp = fopen("demo.txt", "w");         //writing operation         while ((c = getchar()) != EOF) {             putc(c, fp);         }         //close file         fclose(fp);         printf("Information Entered:\north");         //reading         fp = fopen("demo.txt", "r");         while ((c = getc(fp)) != EOF) {             printf("%c", c);         }         fclose(fp);         return 0;     }          

Output:

  1. In the higher up program nosotros accept created and opened a file called demo in a write fashion.
  2. After a write operation is performed, then the file is closed using the fclose function.
  3. We have again opened a file which now contains data in a reading mode. A while loop volition execute until the eof is institute. Once the end of file is constitute the operation will be terminated and data volition exist displayed using printf part.
  4. Afterwards performing a reading operation file is again closed using the fclose function.

Summary

  • A file is a infinite in a retentiveness where data is stored.
  • 'C' programming provides various functions to deal with a file.
  • A machinery of manipulating with the files is called every bit file management.
  • A file must be opened before performing operations on it.
  • A file can exist opened in a read, write or an suspend way.
  • Getc and putc functions are used to read and write a unmarried character.
  • The office fscanf() permits to read and parse data from a file
  • We can read (using the getc part) an unabridged file by looping to cover all the file until the EOF is encountered
  • Nosotros tin can write to a file later creating its name, by using the role fprintf() and information technology must accept the newline character at the end of the string text.

pinessweeng.blogspot.com

Source: https://www.guru99.com/c-file-input-output.html

0 Response to "To Test to See if a File Is Not Read Only, Use the Following Vbscript Code _____."

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel