Read 32 Character String From Binary File C#

Basics of File Handling in C Programming


File Treatment is the storing of data in a file using a plan. In C programming language, the programs shop results, and other data of the program to a file using file handling in C. Likewise, we tin can extract/fetch data from a file to work with it in the program.

The operations that you tin perform on a File in C are −

  • Creating a new file

  • Opening an existing file

  • Reading data from an existing file

  • Writing information to a file

  • Moving data to a specific location on the file

  • Closing the file

Creating or opening file using fopen()

The fopen() function is used to create a new file or open up an existing file in C. The fopen function is defined in the stdio.h header file.

At present, lets run into the syntax for creation of a new file or opening a file

file = fopen("file_name", "mode")

This is a mutual syntax for both opening and creating a file in C.

Parameters

file_name − It is a string that specifies the name of the file that is to be opened or created using the fopen method. mode: It is a string (ordinarily a unmarried character ) that specifies the fashion in which the file is to be opened. There are various modes available to open a file in C, we will acquire about all of them later in this article.

When will a file exist created?

The fopen function volition create a new file when it will not find any file of the specified name in the specified location. Else, if the file is found it will be opened with the fashion specified.

Allow's run into can case which volition make the concept clear, Suppose nosotros are opening a file named hello.txt using the fopen function. The following will be the argument,

file = fopen("hello.txt", "westward")

This will search for a file named hullo.txt in the current directory. If the file exists, it will open the file otherwise it volition create a new file named "hullo.txt" and open it with write mode (specified using "w").

Now, permit'south see all types of modes that are available for the states to read or write a file in C, and see lawmaking snippets that volition show sample runs of the code.

Fashion = "r" − open up for reading, this fashion will open up the file for reading purpose simply, i.e. the contents can only be viewed, nothing else similar edits tin can exist washed to it.

This mode cannot create a new file and open() returns NULL, if we effort to create a new file using this mode.

Case

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("hello.txt", "r")){       printf("File opened successfully in read mode");    }    else    printf("The file is not present! cannot create a new file using r manner");    fclose(file);    return 0; }

Output

File opened successfully in read mode

Nosotros accept created a file named hello.txt in our current directory but if we try to access other file and then we will go "The file is not present! cannot create a new file using r style" as output.

Fashion = "rb" − open for reading in binary style, this fashion will open up the file for reading in binary fashion only, i.e. the contents tin can only be viewed and nothing else like edits can be washed to it.

This mode cannot create a new file and open() returns Goose egg, if we try to create a new file using this mode.

Example

#include <stdio.h> int primary(){    FILE * file;    if (file = fopen("program.txt", "rb")){       printf("File opened successfully in read way");    }    else    printf("The file is not present! cannot create a new file using rb mode");    fclose(file);    return 0; }

Output

The file is non nowadays! cannot create a new file using rb mode

Mode = "west" − open for writing only, this manner will open the file if present in the current directory for writing but i.due east. reading performance cannot be performed. If the file is not present in the current directory, the program will create a new file and open it for writing.

If we open a file that contains some text in it, the contents volition be overwritten.

Instance

 Live Demo

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("helo.txt", "westward")){       printf("File opened successfully in write mode or a new file is created");    }    else    printf("Error!");    fclose(file);    return 0; }

Output

File opened successfully in write mode or a new file is created

You can encounter here, nosotros have tried to open file "helo.txt" which is not present in the directory, yet the function returned the success bulletin because it has create a file named "helo.txt".

Style = "wb" − open for writing in binary manner, this fashion will open the file if nowadays in the electric current directory for writing in binary mode i.e. reading operation cannot be performed. If the file is not present in the electric current directory, the program will create a new file and open up it for writing in binary mode.

If nosotros open up a file that contains some text in information technology, the contents will be overwritten.

Example

 Alive Demo

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("hi.txt", "wb")){       printf("File opened successfully in write in binary way or a new file is created");    }    else    printf("Error!");    fclose(file);    return 0; }

Output

File opened successfully in write in binary mode or a new file is created

Mode = "a" − open for append only, this mode will open the file if present in the electric current directory for writing only i.e. reading operation cannot be performed. If the file is non present in the current directory, the program will create a new file and open it for writing. If we open a file that contains some text in it, the contents will not be overwritten; instead the new text will be added after the existing text in the file.

Example

 Alive Demo

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("hullo.txt", "a")){       printf("File opened successfully in append mode or a new file is created");    }    else    printf("Error!");    fclose(file);    return 0; }

Output

File opened successfully in append style or a new file is created

Mode = "ab" − open for suspend in binary, this mode volition open the file if present in the electric current directory for writing in binary only i.e. reading operation cannot be performed. If the file is not present in the current directory, the program will create a new file and open up it for writing in binary.

If nosotros open a file that contains some text in it, the contents will not be overwritten; instead the new text volition be added subsequently the existing text in the file.

Example

 Live Demo

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("how-do-you-do.txt", "ab")){       printf("File opened successfully in append in binary mode or a new file is created");    }    else    printf("Error!");    fclose(file);    return 0; }

Output

File opened successfully in suspend in binary mode or a new file is created

Mode = "r+" − open for reading and writing both, this mode will open the file for both reading and writing purposes i.due east. both read and write operations can be performed to the file.

This mode cannot create a new file and open() returns Cypher, if nosotros try to create a new file using this mode.

If nosotros open a file that contains some text in information technology and write something, the contents will be overwritten.

Example

#include <stdio.h> int principal(){    FILE * file;    if (file = fopen("hello.txt", "r+")){       printf("File opened successfully in read and write both");    }    else    printf("The file is non present! cannot create a new file using r+ style");    fclose(file);    return 0; }

Output

File opened successfully in read and write both

We have created a file named hello.txt in our current directory but if nosotros effort to access some other file and so we will get "The file is not present! cannot create a new file using r+ mode" equally output.

Mode = "rb+" − open for reading in binary mode, this style volition open the file for reading in binary mode simply, i.e. the contents can just exist viewed and nothing else like edits can be done to it.

This mode cannot create a new file and open() returns NULL, if we endeavour to create a new file using this mode.

If nosotros open a file that contains some text in it and write something, the contents will exist overwritten.

Example

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("program.txt", "rb+")){       printf("File opened successfully in read way");    }    else    printf("The file is non present! cannot create a new file using rb+ style");    fclose(file);    return 0; }

Output

The file is non nowadays! cannot create a new file using rb+ mode

Mode = "due west" − open up for writing and reading, this mode volition open the file if nowadays in the electric current directory for writing and reading operation both. If the file is not nowadays in the current directory, the program will create a new file and open information technology for reading and writing.

If nosotros open up a file that contains some text in it, the contents will exist overwritten.

Example

 Alive Demo

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("helo.txt", "w+")){       printf("File opened successfully in read-write mode or a new file is created");    }    else    printf("Error!");    fclose(file);    return 0; }

Output

File opened successfully in read-write style or a new file is created

You can see here, nosotros take tried to open file "helo.txt" which is not nowadays in the directory, nevertheless the function returned the success message because it has create a file named "helo.txt".

Mode = "wb+" : open for writing and reading in binary fashion, this way will open the file if nowadays in the current directory for writing and reading in

binary mode. If the file is not present in the current directory, the program will create a new file and open it for reading and writing in binary mode. If we open a file that contains some text in it, the contents will be overwritten.

Case

 Live Demo

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("hullo.txt", "wb+")){       printf("File opened successfully in read-write in binary mode or a new file is created");    }    else    printf("Error!");    fclose(file);    render 0; }

Output

File opened successfully in read-write in binary mode or a new file is created

Mode = "a+" − open for read and append, this fashion will open the file if nowadays in the electric current directory for both reading and writing. If the file is not present in the electric current directory, the programme volition create a new file and open it for reading and writing.

If nosotros open a file that contains some text in it, the contents will non be overwritten; instead the new text volition be added after the existing text in the file.

Instance

 Live Demo

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("hello.txt", "a+")){       printf("File opened successfully in read-append mode or a new file is created");    }    else    printf("Error!");    fclose(file);    render 0; }

Output

File opened successfully in read-append style or a new file is created

Manner = "ab+" − open up for read and append in binary, this mode will open up the file if present in the current directory for both reading and writing in binary. If the file is not present in the current directory, the plan volition create a new file and open information technology for reading and writing in binary. If we open a file that contains some text in it, the contents will not be overwritten; instead the new text will exist added after the existing text in the file.

Case

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("hello.txt", "ab+")){       printf("File opened successfully in read-append in binary mode or a new file is created");    }    else    printf("Error!");    fclose(file);    render 0; }

Output

File opened successfully in read-suspend mode or a new file is created

Reading Data for from an existing file

We can read content of a file in c using the fscanf() and fgets() and fgetc() functions. All are used to read contents of a file. Let's run across the working of each of the function −

fscanf()

The fscanf() function is used to read character set up i.east strings from the file. It returns the EOF, when all the content of the file are read by information technology.

Syntax

int fscanf(FILE *stream, const char *charPointer[])

Parameters

FILE *stream: the pointer to the opened file. const char *charPointer[]: string of character.

Example

#include <stdio.h> int main(){    FILE * file;    char str[500];    if (file = fopen("hullo.txt", "r")){          while(fscanf(file,"%s", str)!=EOF){          printf("%due south", str);       }    }    else    printf("Error!");    fclose(file);    return 0; }

Output

LearnprogrammingattutorialsPoint

fgets()

The fget() function in C is used to read cord from the stream.

Syntax

char* fgets(char *cord, int length, FILE *stream)

Parameter

char *string: Information technology is a string which will store the data from the string. int length: Information technology is an int which gives the length of string to be considered. FILE *stream: Information technology is the pointer to the opened file.

Example

#include <stdio.h> int master(){    FILE * file;    char str[500];    if (file = fopen("hello.txt", "r")){       printf("%s", fgets(str, 50, file));    }    fclose(file);    render 0; }

Output

Learn programming at tutorials Point

fgetc()

The fgetc() role in C is used to return a single character from the file. Information technology gets a character from the file and returns EOF at the end of file.

Syntax

char* fgetc(FILE *stream)

Parameter

FILE *stream: It is the pointer to the opened file.

Example

#include <stdio.h> int main(){    FILE * file;    char str;    if (file = fopen("hello.txt", "r")){       while((str=fgetc(file))!=EOF)       printf("%c",str);    }    fclose(file);    return 0; }

Output

Learn programming at tutorials Point

Writing Data to a file in C

We can write data to a file in C using the fprintf(), fputs(), fputc() functions. All are used to write contents to a file.

Allow's run into the working of each of the role −

fprintf()

The fprintf() part is used to write data to a file. It writes a set of characters in a file.

Syntax

int fprintf(FILE *stream, char *string[])

Parameters

FILE for *stream: It is the pointer to the opened file. char *string[]: It is the character array that we want to write in the file.

Example

#include <stdio.h> int principal(){    FILE * file;    if (file = fopen("hello.txt", "w")){       if(fprintf(file, "tutorials Point") >= 0)       printf("Write functioning successful");    }    fclose(file);    return 0; }

Output

Write operation successful

fputf()

The fputf() function in C can be used to write to a file. Information technology is used to write a line (character line) to the file.

Syntax

int fputs(const char *string, FILE *stream)

Parameters

Constant char *string[]: It is the character assortment that nosotros want to write in the file. FILE for *stream: Information technology is the pointer to the opened file.

Instance

 Live Demo

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("how-do-you-do.txt", "due west")){       if(fputs("tutorials Bespeak", file) >= 0)       printf("String written to the file successfully...");    }    fclose(file);    return 0; }

Output

String written to the file successfully…

fputc()

The fputc() part is used to write a unmarried graphic symbol to the file.

Syntax

int fputc(char grapheme , FILE *stream)

Parameters

char character : It is the grapheme that we desire to write in the file. FILE for *stream: Information technology is the arrow to the opened file.

Example

 Live Demo

#include <stdio.h> int main(){    FILE * file;    if (file = fopen("hello.txt", "w")){       fputc('T', file);    }    fclose(file);    return 0; }

Output

'T' is written to the file.

fclose()

The fclose() function is used to close the open up file. We should close the file after performing operations on it to save the operations that we have applied to it.

Syntax

fclose(FILE *stream)

Parameters

FILE for *stream: It is the pointer to the opened file.

Instance

 Live Demo

#include <stdio.h> int main(){    FILE * file;    char string[300];    if (file = fopen("hello.txt", "a+")){       while(fscanf(file,"%s", cord)!=EOF){          printf("%south", string);       }       fputs("Hello", file);    }    fclose(file);    return 0; }

Output

LearnprogrammingatTutorialsPoint

File contains

Learn programming at Tutorials PointHello

raja

Published on 17-Jul-2020 11:44:32

  • Related Questions & Answers
  • Basics of File Handling in C
  • Exception Handling Basics in C++
  • Basics of C++ Programming Language?
  • File Handling in C#
  • What are the nuts of a programming?
  • File Handling through C++ Classes
  • tellp() in file handling with C++
  • Set position with seekg() in C++ language file handling
  • How to create excel file in c++ programming
  • Basics of React.js Routing
  • File Operations in Rust Programming
  • File Handling in Coffee using FileReader and FileWriter
  • Microcomputer Basics
  • Microprocessor Nuts
  • Microcontroller Basics

mitchellwepid2002.blogspot.com

Source: https://www.tutorialspoint.com/basics-of-file-handling-in-c-programming

0 Response to "Read 32 Character String From Binary File C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel