Author: learnconline

Closing the File in C 2

Closing the File in C

Once we open a file, finished reading from the file, we need to close the file. This is done using the function fclose() through the statement: fclose(fp); Once we are done with closing the file, we can no longer read, write or perform any operation on the file unless we reopen it.  Note that to close the file, we don’t use the filename but the file pointer fp. On closing the file, the buffer associated with the file is removed from memory. While performing a write operation on a file, when we try to close the file using fclose(), three...

Reading from a File in C 2

Reading from a File in C

Once the file has been opened for reading using fopen(), as we have seen, the file’s contents are brought into buffer and a pointer is set up that points to the first character in the buffer. This pointer is one of the elements of the structure to which fp is pointing. To read the file’s contents from the memory, there exists a function called fgetc(). This has been used in our program as: ch = fgetc(fp); fgetc() performs the following operations: Reads the characters from the current pointer position Advances the pointer position so that it now points to the next character...

Opening a file in C 2

Opening a file in C

We can perform read and write operations on a file in C. But, before that we must open a file. To open a file in C, we have to call the function fopen() as shown below: FILE *fp; fp = fopen(“file1.C”,”r”); The above statement would open file named “file1.C” in “read” mode. It tells the computer that the file being opened would be used for reading purpose only. fopen() performs three important tasks when you open the file in “r” mode: Firstly, it searches the file to be opened on the disk Then it loads the file from the disk into...

Introduction to File Operations in C Programming language 4

Introduction to File Operations in C Programming language

As we all know, the operations we perform via. printf and scanf, reads and stores data in the memory. But this memory is limited for use. If in case we need to read and store large amount of data then this limited memory can’t be used. We need to find some ways to store this data permanently so that we can read and write to it at later point of time. This is where file operations comes into picture. C provides us with the facility to perform File operations in C programming language. File Operations in C Programming Language: There...

Do not use printf without s to print a string 0

Tip of the week#8:Do not use printf without %s to print a string

Since the printf() function takes strings as arguments, you might think that you do not need the format specifier “%s” while printing a string. Example: int main() { char string[30]=”Hello c programers”; printf(string); return 0; } However, this can be very dangerous. What if your string includes a format specifier like %s or %d? Because printf is a varargs function, it uses the format string to decide how many arguments it takes. If you provide one argument, but put in the format specifier, it will assume it has more arguments than it does, and read them off the stack. This...

Write if statements with braces 0

Tip of the week#6: Write if statements with braces

By putting braces around every block of code you write, you ensure that future edits won’t introduce bizarre bugs. If you have a one-line if statement: if(condition) execute(); you should still surround execute(); with braces as follows: if(condition) { execute(); } Now, if you want to go back to the code in future and add a second instruction if(condition) { execute(); execute2(); } you don’t have to worry about putting in the braces.

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet