✔ C-LANGUAGE CHAPTERS:-
1. C-Introduction
2. C-Constants
3. C-Basic Programs
4. C-Instructors
5. C-Operators
6. C-If Else Statement
7. C-Loop Contructs
8. C-Nesting Of Loops
9. C-Programming Examples
10. C-Functions
11. C-Functions Naming Convention
12. C-Pointers
13. C-Arrays
14. C-Arrays & Functions
15. C-Strings
16. C-Structures
17. C-Union
18. C-Storage Classes
19. C-PreProcessor
20. C-Header Files
21. C-R/W one Line
22. C-Accessing Random File
23. C-Important Point
24. C-Interview Questions
1. C-Introduction
2. C-Constants
3. C-Basic Programs
4. C-Instructors
5. C-Operators
6. C-If Else Statement
7. C-Loop Contructs
8. C-Nesting Of Loops
9. C-Programming Examples
10. C-Functions
11. C-Functions Naming Convention
12. C-Pointers
13. C-Arrays
14. C-Arrays & Functions
15. C-Strings
16. C-Structures
17. C-Union
18. C-Storage Classes
19. C-PreProcessor
20. C-Header Files
21. C-R/W one Line
22. C-Accessing Random File
23. C-Important Point
24. C-Interview Questions
Reading and writing one line at a time (C-RW One Line): -A line can be read from a opened file.
Two functions are used for reading a line:
fputs()
fgets()
Syntax: fputs(string, file pointer);
2. fgets(): - This function is used to read a sequence of characters until it reaches one of the following:- At the new line.
- Maximum number of characters which are specified by the programmer.
- At the end of the file(EOF).
It takes three arguments: First, string pointer where you want to receive string. Second, maximum number of characters to read. Third, file pointer.
Syntax: fgets(string pointer, characters, file pointer);
Eg: #include
Eg: #include
void main( )
{
FILE *name;
char string1[50];
name = fopen(“first.txt”, “r”);
while(fgets(string1, 60, name)!= NULL)
printf(“%s”, string1);
fclose(name);
}
- Reading and writing one block at a time: - Data can be accessed in a block. Block is an array. If you want to read a block then number of elements are need to be specify.
Function for reading/writing a block is:
- fread()
- fwrite()
- fread(): -This function is used to read one or more structures and it takes four arguments: First, pointer location. Second, each structure has a size and size is need to be specified here, which must be in bytes. Third, number of item to be read. Fourth, file pointer variable.
Syntax: fread( pointer location, size, number of items, file pointer variable)
Eg: #include
Eg: #include
void main( )
{
FILE *file;
struct student
{
char name[20];
int rollno;
};
struct student std;
file = fopen(“student.dat”, “r”);
if(file = = NULL)
{
printf(“Error..!!”);
exit( );
}
else
{
while(fread(&std, sizeof(std), 1, file) = = 1)
{
printf(“%s %d”, std.name, std.rollno);
}
}
fclose(file);
}
2. fwrite(): -This function is same as fread() it takes the same number of argument and syntax, but instead of reading elements from an array it writes element to the array.
Syntax: fwrite(pointer location, size, number of item, pointer variable);
We can read one word i.e. an integer value from a file also.
Two functions are used for it:
getw()Two functions are used for it:
putw()
1. getw(): - it is used to read integer value from specified file. It takes one argument i.e. Pointer to file.
Syntax: getw(file pointer);
2. putw(): - it is used to write integer value on specified file and takes two arguments.
Syntax: putw(integer value, file pointer);
First, integer value is to b written to file. Second, file pointer.
Now learn to Access Random files in C Programming.
C-Header Files: Previous Next: C-Accessing Random File