How to write a C Program to File Number Control in C Programming Language ?
Solution:
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- void main()
- {
- FILE *fp, *fc;
- int lineNum; //stores line number which should be edited.
- int lineCount = 0; //count number lines in source file.
- int charCount = 0;
- int ch; //temporary place to store character of source file(one at a time).
- int edited = 0; //0=false and 1=true
- int t; //temporary place to store input which you want to be replaced with error in text file.
- int *line;
- fp = fopen("source.txt", "r+");
- if (fp == NULL)
- {
- printf("\nError...cannot open/create files");
- }
- line = (int*)malloc(sizeof(int)*(lineCount+1));
- line[0] = 0;
- fseek(fp, 0, SEEK_SET);
- while (true)
- {
- ch = fgetc(fp);
- charCount++;
- if (ch == '\n') //counts number of lines
- {
- lineCount++;
- line = (int*)realloc(line, sizeof(int)*(lineCount + 1));
- line[lineCount] = charCount+1;
- }
- if (ch == EOF)
- {
- lineCount++;
- line = (int*)realloc(line, sizeof(int)*(lineCount + 1));
- line[lineCount] = charCount+1;
- break;
- }
- }
- for (int i = 0; i <= lineCount; i++)
- {
- printf("line[%d] byte offset is %d\n", i, line[i]);
- }
- if (fseek(fp, 7, SEEK_SET) != 0)
- printf("FSEEK ERROR!\n");
- else
- fprintf(fp, "YYY");
- printf("Number of lines in file: %d", lineCount);
- system("PAUSE");
- }
Learn More :
Number
- Find out the perfect number using c program
- Write a c program to find out H.C.F. of two numbers.
- Check the given number is armstrong number or not using c program.
- Write a c program to find largest among three numbers using conditional operator
- FIND OUT GENERIC ROOT OF A NUMBER - C PROGRAM.
- FIND PRIME FACTORS OF A NUMBER USING C PROGRAM
- How To Write a C program that generates two random numbers ?
- Write a C program to find maximum or equal between two numbers ?
- How to Write a C program to find maximum between two numbers ?
- Write a C program to perform math operations on two input whole numbers. The operations are:
- Write a C program to find maximum between three numbers ?
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C Program to Enter an ODD number between 1 and 49.
- C program acquires keyboard 10 numbers for each of these numbers to determine if it is a first issue, by printing immediately message should at the end, if none of the numbers you entered was a first issue, print an appropriate message.
- C program generates a random number and uses an algorithm to generate 9 other numbers.
- C Program to Find Random Number
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program to find LCM and HCF Of Two Number Using Recursion - 3
- C Program To Find LCM and HCF Of Two Number -1
- C Program To Find Reverse Of Any Digit Number
- C Program To Find The Frequency Of A Number
- C Program To Print Prime Numbers Upto The Number You Want
- C Program To Print Sum Of n Digit Number
- C Program To Reverse A Number
- C Program To Search A Number Inside The Array
File
- COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM
- C Program File Input & Output Example
- C Program To Destruc Self Execution File ?
- C Program To Store Students Record In A Text File
- C Program To Write Data In A File And Search Data From File
- Make a copy of file given in argv[1] to file given in argv[2]
- C Program to copy a file to another file
- C Function to read instructions from the file and obey them
- Client/Server C program to make client send the name of a file
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program to Read and Write FIFO
- C Program to opens and reads dictionary file specified in spellrc
- C Program to Recursively converts all file-names to lower-case
- C Program to Find the Size of File using File Handling Function
- C Program Recursive function that searches for a given file in a given folder
- C Program Read a char & print next char ( file to file )
- C Program to Input Student Details into a File For Two Subjects
- Identifies the architecture Windows PE (exe or dll) file was compiled C Program
- File Handling (console to file) in C Program