How to use fgets to read a file line by line - c

I'm new at programming so there are some basics and maybe common sense that I don't know. I have a question about how to use fgets right. Based on the explanation of fgets, it seems that fgets should stop whenever it reads n-1 characters, hit the EOF or hit a newline character.
For example, I create a text file like below:
red 100
yellow 400
blue 300
green 500
purple 1000
...
The color and the integer is separated by a tab. When I create this text file, I need to hit enter at the end of each line to start a new line. In this case, hitting enter equals to add a newline character, '\n', is that right?
If it is right that there is a '\n' at the end of each line, I run the fgets code as below:
fgets(string, 100, fp);
Since the characters contain in each line is much less than 100, the fgets should hit the newline character before reach the maxlength limit and it should stop and return a NULL. Is that correct?
If my understanding above are not right, there is no '\n' at the end of each line, or fgets does not stop at the end of each line, what is the number of maxlength (i.e., the N in the fgets(string, N, stream) function) should I pick to make sure that the file input properly due to my ultimate goal is to parsing each line and store each line into a structure. By the way, there are 100 lines in the file.

#include <stdio.h>
int main()
{
char str[150],str2[100][150];
int i=0,j=0,value[100];
FILE* fp;
fp = fopen("file.txt", "r");
while (fgets(str,150, fp)) {
i++;
printf("%3d: %s\n", i, str);
/** if you want to split value and string*/
sscanf(str,"%s %d",&str2[j],&value[j]);
printf("%d %s\n",value[j],str2[j]);
j++;
}
fclose(fp);
return 0;
}
You can use sscanf() to easily split values and fgets() to read file.

fp = fopen("sample.txt", "r");
while (1) {
if (fgets(line,150, fp) == NULL) break;
i++;
printf("%3d: %s", i, line);
}
printf("%d\n",i);

// hello.c
//
// Usage:
//
// gcc -Wall hello.c && ./a.out /tmp/somefile.txt
#include <stdlib.h> // for perror, ...
#include <stdio.h> // for printf, ...
#include <assert.h> // for assert
#include <sys/time.h> // for gettimeofday
static inline long long int nowUs () {
long long int now;
struct timeval timer_us;
if (gettimeofday(&timer_us, NULL) == 0) {
now = ((long long int) timer_us.tv_sec) * 1000000ll +
(long long int) timer_us.tv_usec;
}
else now = -1ll;
return now;
}
int main (const int argc, const char * argv[]) {
assert(2 == argc);
long long int started = nowUs();
size_t count = 0;
char msg[128], * fgets_rv;
FILE * fp = fopen(argv[1], "r");
while ((fgets_rv = fgets(msg, sizeof(msg), fp))) {
assert(fgets_rv == msg);
count++;
}
if (ferror(fp))
perror(argv[1]);
else if (feof(fp)) {
printf("Read %zu lines of file '%s' in %lldµs\n",
count, argv[1], nowUs() - started);
}
else {
printf("UNEXPECTED\n");
}
fclose(fp);
return 0;
}
Sample output:
alec#mba ~/process/sandbox $ gcc -Wall hello.c && ./a.out /tmp/bigfile.t02
Read 100000 lines of file '/tmp/bigfile.t02' in 16521µs

Related

Printing the first 10 line of a file in C

I'm new to programming in C. And I'm trying to print the first 10 lines of a text file. When I run my program with a text file containing 11 lines of text, only the first line is displayed. I'm not sure why it does that, but I suspect there is something wrong in my while loop. Can someone please help me?
#include <stdio.h>
int main(int argc, char *argv[]){
FILE *myfile;
char content;
int max = 0;
// Open file
myfile = fopen(argv[1], "r");
if (myfile == NULL){
printf("Cannot open file \n");
exit(0);
}
// Read the first 10 lines from file
content = fgetc(myfile);
while (content != EOF){
max++;
if (max > 10)
break;
printf ("%c", content);
content = fgetc(myfile);
}
fclose(myfile);
return 0;
}
You have been already advised to use fgets. However, if your file has lines of unknown length, you may still want to use fgetc. Just make sure you count only newlines, not all characters:
int max = 0;
int content;
while ((content = fgetc(myfile)) != EOF && max < 10){
if (content == '\n') max++;
putchar(content);
}
fgetc() returns the next character in the file, not the next line. You probably want to use fgets() instead, which reads up to the next newline character into a buffer. Your code should probably end up with something like:
// allocate 1K for a buffer to read
char *buff = malloc(1024);
// iterate through file until we are out of data or we read 10 lines
while(fgets(buff, 1024, myfile) != NULL && max++ < 10) {
printf("%s\n", buff);
}
free(buff);
// close your file, finish up...
Read more about fgets() here: https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm
fgetc function reads the next character not the next ine. for reading the number of lines you should use fgets function. this function reads the full string till the end of the one line and stores it in a string.
your code Shuld be as:-
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *myfile;
char content[200];
int max = 0;
// Open file
myfile = fopen(argv[1], "r");
if (myfile == NULL)
{
printf("Cannot open file \n");
exit(0);
}
// Read the first 10 lines from file
fgets(content, 200, myfile);
while (content != EOF)
{
max++;
if (max > 10)
break;
printf("%s", content);
fgets(content, 200, myfile);
}
fclose(myfile);
return 0;
}

How do I read a file line by line in C from the terminal?

I have read that I can use fopen to read a file line by line, but I want to access the file from the terminal as such.
This is what I have tried:
$ ./myprogram < input.txt > output.txt
I'm not sure if there's a way to do it with scanf or another way?
Here, if you think about what you are doing, you are simply reading continually from stdin and writing the same bytes to stdout until you receive an EOF. While you can use a character oriented approach (e.g. getchar), a read with a fixed length buffer will dramatically cut down the number of reads and writes you have.
Simply declare a buffer of comfortable size, 1024 (or use the default BUFSIZ provided, generally 8192 on Linux and 512 on windoze). Then repeatedly call fgets reading a buffers worth of characters at a time and writing them back to stdout with fputs. That's about as simple as it gets.
#include <stdio.h>
#define BUFSZ 1024
int main (void) {
char buf[BUFSZ] = "";
while (fgets (buf, BUFSZ, stdin))
fputs (buf, stdout);
return 0;
}
Ideally, you would want a buffer size just longer than the longest line, although it really doesn't matter what size it is. You can read each line all at once, or in multiple calls to fgets. The only difference is the number of function calls made.
#include <stdio.h>
#define BUFSIZE 1024
int main(int argc, char *argv[])
{
char *line = (char *)malloc(BUFSIZE);
if (!line)
{
printf("malloc buffer failed...\n");
return 1;
}
memset(line, 0, sizeof(line));
FILE *fp;
FILE *writefp;
int c;
int count = 0;
int count_buf = BUFSIZE;
char scanf_answer;
if (argc != 3)
{
printf("./myprogram <input.txt> output.txt\n");
return 1;
}
fp = fopen(argv[1], "r");
for (;;)
{
c = getc(fp);
if (c == '\n')
{
printf("%s\n", line);
printf("<Did you want to write this line to [%s]?>", argv[2]);
scanf("%c", &scanf_answer);
if (scanf_answer == 'Y' || scanf_answer == 'y')
{
writefp = fopen(argv[2], "a+");
fprintf(writefp, "%s\n", line);
fclose(writefp);
}
memset(line, 0, sizeof(line));
}
else if (c == EOF)
{
printf("%s\n", line);
printf("<Did you want to write this line to [%s]?>", argv[2]);
scanf("%c", &scanf_answer);
if (scanf_answer == 'Y' || scanf_answer == 'y')
{
writefp = fopen(argv[2], "a+");
fprintf(writefp, "%s\n", line);
fclose(writefp);
}
printf("End of file\n");
break;
}
if (count >= count_buf)
{
line = realloc(line, BUFSIZE);
count_buf += BUFSIZE;
if (!line)
{
printf("realloc buffer failed...\s");
return 1;
}
count = 0;
}
sprintf(line,"%c%c", line, c);
++count;
}
fclose(fp);
return 0;
}
This code will print each line, you decide each line to write to the output.txt, and in the file end, it will print End of file
$ ./myprogram < input.txt > output.txt
The command you posted uses a shell feature called IO redirection to produce input on stdin from one file and redirect output to stdout to the other file.
To take lines as input to your program is super easy even for lines of arbitrary length if you can use POSIX getline(). Please consult the manpage (linked below) for details.
Here's an example:
#include <stdio.h>
#include <stdlib.h>
int main() {
// this is the buffer data is read to (including trailing newline)
char *buffer = 0;
// this will be set to the size of the buffer
size_t buffer_size = 0;
// this will be set to the number of bytes read
ssize_t bytes_read;
while ((bytes_read = getline(&buffer, &buffer_size, stdin)) != -1) {
// do something with line
printf("%s", buffer);
// the same buffer will be reused in the next loop iteration
}
// free buffer eventually
free(buffer);
return 0;
}
Possible output:
$ gcc test.c && ./a.out < test.c
#include <stdio.h>
#include <stdlib.h>
int main() {
[...]
Note that scanf() is for taking formatted input, which reading lines is not. I suggest you get to learn more about the different approaches to IO (on streams) here:
https://www.gnu.org/software/libc/manual/html_node/I_002fO-on-Streams.html#I_002fO-on-Streams
For reference:
http://man7.org/linux/man-pages/man3/getline.3.html

Change the logic of text file reading program to suspend output after 4 lines

So what I'm basically doing here is using a command line argument to open a file but only open it 4 lines at a time, then a prompt to print out add'l lines. I can get the file to print out but I cannot figure out how to get it to only print out a few lines at a time. This is where I'm at....thoughts?
#include <stdio.h>
int main(int argc, char *argv[])
{
char line[1000];
FILE *pt;
pt = fopen(argv[1], "r");
if(pt == NULL) return -1;
printf(argv[1], line);
while(fgets(line, 1000, pt) != NULL)
printf("%s", line);
fclose(pt);
return 0;
}
I start from strange line of your code, and then I will try to answer the question.
Statement
printf(argv[1], line);
make me curious - what you what to print, actually?
Here line is not initialized, and argv[1] can hardly be used as format line.
So I suppose it should be just
printf(argv[1]);
or
printf("Filename is %s\n", argv[1]);
As for reading from a file with name provided as argv[1] your code looks able to work, I mean your code read line by line till the end of file and prints these lines at the screen.
If you want to change this logic, e.g. read only 4 first line, add condition with counter, e.g.:
int cnt;
for (cnt = 0; cnt < 4; cnt++) // repeat reading 4 times
{
if (fgets(line, 1000, pt) != NULL)
printf("%s", line);
else
break; // stop when reading fails
}
or (I prefer this version)
int cnt = 0;
while (fgets(line, 1000, pt) != NULL && cnt < 4)
{
printf("%s", line);
cnt++;
}
Such changes allows to stop reading (as well as output), so only 4 or less lines will be shown at console screen.
Finally, for case when you want to show file by groups of 4 (or other constant value), consider the following snippet:
#include <stdio.h>
#define MAX_LINES_TO_PRINT 4
int main(int argc, char *argv[])
{
char line[1000];
FILE *pt;
pt = fopen(argv[1], "r");
if (pt == NULL) return -1;
printf("Filename is %s\n", argv[1]);
int cnt = 0;
while (fgets(line, 1000, pt) != NULL)
{
printf("%s", line);
cnt++;
if (cnt % MAX_LINES_TO_PRINT == 0)
{
int answer;
printf("[%d lines printed] Continue? (Y/N) : ", cnt);
answer = getchar(); // get user's response
while (getchar() != '\n'); // clean input buffer after getchar
if (toupper(answer) == 'N')
{
break; // stop reading the file
}
}
}
fclose(pt);
return 0;
}
Try this program with your file and ask question if something is unclear.
Changing the value in the line #define MAX_LINES_TO_PRINT 4 you can regulate maximum number of lines printed at once (before the next request to continue), e.g. #define MAX_LINES_TO_PRINT 15 make your program printing up to 15 lines.

Read Line By Line Until Integer is Found C

Trying to create a program that takes in a text file and reads it line by line. It then finds the two integers that are on each line and adds them together. It then outputs the new line with the original string and total to a new text file. I need help adding the two integers, getting them from each line, and then putting the new line to a text file.
input text file
good morning hello 34 127
ann 20 45
10 11
fun program and you find the same 90 120
news paper said that 56 11
how do you like 20 5
line number 90 34
Outputs first like would look like: and then continue on
good morning hello 161
Code:
int processTextFile(char * inputFileName, char * outputFileName)
{
FILE *fp = fopen(inputFileName, "r");//open file to to read
char buff[1024];
char *p, *p1;
int num;
while (fgets(buff, 1024, fp)!=NULL)
{
printf("%s\n", buff);
while(scanf(buff, "%*[^0-9]%d", &num)== 1)
printf("%d\n", num);
//fscanf(fp, "%s", buff);
}
return 0;
}
EDIT!!!!::
So now that I've been able to accomplish this. How would I sort it by the number produced? for example:
Time is money 52
here I am 3
21
Would output to a new text file in order like
here I am 3
21
Time is money 52
My version using strcspn() is supposed to work with stdin for input and stdout for output. (so you can do executable <textfile >newtextfile)
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char line[1000];
while (fgets(line, sizeof line, stdin)) {
char *ptr;
size_t x = strcspn(line, "0123456789");
if (line[x]) {
errno = 0;
int n1 = strtol(line + x, &ptr, 10);
if (*ptr && !errno) {
errno = 0;
int n2 = strtol(ptr, &ptr, 10);
if (*ptr && !errno) {
int n3 = n1 + n2;
printf("%.*s%d\n", (int)x, line, n3);
} else {
printf("%s", line); // line includes ENTER
}
} else {
printf("%s", line); // line includes ENTER
}
} else {
printf("%s", line); // line includes ENTER
}
}
return 0;
}
The same version without the error checking
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char line[1000];
while (fgets(line, sizeof line, stdin)) {
char *ptr;
size_t x = strcspn(line, "0123456789");
int n1 = strtol(line + x, &ptr, 10);
int n2 = strtol(ptr, &ptr, 10);
int n3 = n1 + n2;
printf("%.*s%d\n", (int)x, line, n3);
}
return 0;
}
The approach should be:
open two files, one for input, one for output.
use sscanf() to read the input buffer.
scan the leading string, and then two number.
if previous sscanf() fails, only check for two number.
if either of the above scanning is success, print the sum to the output file.
A sample code, should look like
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *fpin = fopen("ipfile", "r");//open file to to read
if (!fpin)
{
printf("Error in ipfile opening\n");
exit (-1);
}
FILE *fpout = fopen("opfile", "w");//open file to to write
if (!fpout)
{
printf("Error in opfile opening\n");
exit (-1);
}
char buff[1024] = {0};
char str[1024] = {0};
int num1 =0, num2= 0;
while (fgets(buff, 1024, fpin)!=NULL)
{
memset(str, 0, sizeof(str));
//printf("%s\n", buff);
if(sscanf(buff, "%[^0-9]%d %d", str, &num1, &num2)== 3)
fprintf(fpout, "%s %d\n", str, (num1+num2));
else if (sscanf(buff, "%d %d", &num1, &num2)== 2)
fprintf(fpout, "%d\n", (num1+num2));
}
return 0;
}
Note:
The above procedure, is a kind of workaround. If the data pattern in the file changes, lot of changes will be required to maintain a code like that. Instead of usinf sscnaf(), for a better and roubust approach, you should
read the line from file
start tokenizing the input buffer (strtok()) and check for ints as tokens (strtol()).
save the returned tokens and ints seperately.
once the strtok() returns NULL, you print the string tokens and the sum of the ints to the o/p file.
I intend not to change your code completely, so I just added some snippets for improvement.
int processTextFile(char *inputFileName, char *outputFileName) {
FILE *fp = fopen(inputFileName, "r");
FILE *out = fopen(outputFileName, "w");
char line[1024];
if (!fp) {
perror(inputFileName);
return;
}
while (fgets(line, sizeof(line), fp) != NULL) {
int num1 = 0, num2 = 0;
char textPart[1024] = "";
if ( !sscanf(line, "%[a-zA-Z' ']%d%d", textPart, &num1, &num2) {
sscanf(line, "%d%d", &num1, &num2);
}
fprintf(out, "%s %d\n", textPart, num1 + num2);
}
fclose(fp);
fclose(out);
}
Explanation:
I scanned the text file, extracted the text part and the two ints. Since I noticed that the ints are placed at the end of each line, I just used sscanf() for that matter.
sscanf(line, "%[a-zA-Z' ']%d%d", textPart, &num1, &num2);
Here, "%[a-zA-Z' ']%d%d" format specifiers means to get only alphabets and spaces.
Since it will only get letters and spaces, the line "10 11" in your input file won't be put to num1 and num2. Because the code inspects first for a string containing letters and spaces. Since 10 and 11 are not of the qualified types, then the line is just skipped.
That's why I added an if-else statement, which checks if sscanf wrote anything to memory. If sscanf returned 0, then it means that no text part is present. Just digits. So the program will scan the two digits.
if ( !sscanf(line, "%[a-zA-Z' ']%d%d", textPart, &num1, &num2) ) {
sscanf(line, "%d%d", &num1, &num2);
}
I also added file checking for input file. It checks if file doesn't exist or can't be opened by the filestream.
if (!fp) {
perror(inputFileName);
return;
}
Here is the content of output file after execution:
good morning hello 161
ann 65
21
fun program and you find the same 210
news paper said that 67
how do you like 25
line number 124

Get the length of each line in file with C and write in output file

I am a biology student and I am trying to learn perl, python and C and also use the scripts in my work. So, I have a file as follows:
>sequence1
ATCGATCGATCG
>sequence2
AAAATTTT
>sequence3
CCCCGGGG
The output should look like this, that is the name of each sequence and the count of characters in each line and printing the total number of sequences in the end of the file.
sequence1 12
sequence2 8
sequence3 8
Total number of sequences = 3
I could make the perl and python scripts work, this is the python script as an example:
#!/usr/bin/python
import sys
my_file = open(sys.argv[1]) #open the file
my_output = open(sys.argv[2], "w") #open output file
total_sequence_counts = 0
for line in my_file:
if line.startswith(">"):
sequence_name = line.rstrip('\n').replace(">","")
total_sequence_counts += 1
continue
dna_length = len(line.rstrip('\n'))
my_output.write(sequence_name + " " + str(dna_length) + '\n')
my_output.write("Total number of sequences = " + str(total_sequence_counts) + '\n')
Now, I want to write the same script in C, this is what I have achieved so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
input = FILE *fopen(const char *filename, "r");
output = FILE *fopen(const char *filename, "w");
double total_sequence_counts = 0;
char sequence_name[];
char line [4095]; // set a temporary line length
char buffer = (char *) malloc (sizeof(line) +1); // allocate some memory
while (fgets(line, sizeof(line), filename) != NULL) { // read until new line character is not found in line
buffer = realloc(*buffer, strlen(line) + strlen(buffer) + 1); // realloc buffer to adjust buffer size
if (buffer == NULL) { // print error message if memory allocation fails
printf("\n Memory error");
return 0;
}
if (line[0] == ">") {
sequence_name = strcpy(sequence_name, &line[1]);
total_sequence_counts += 1
}
else {
double length = strlen(line);
fprintf(output, "%s \t %ld", sequence_name, length);
}
fprintf(output, "%s \t %ld", "Total number of sequences = ", total_sequence_counts);
}
int fclose(FILE *input); // when you are done working with a file, you should close it using this function.
return 0;
int fclose(FILE *output);
return 0;
}
But this code, of course is full of mistakes, my problem is that despite studying a lot, I still can't properly understand and use the memory allocation and pointers so I know I especially have mistakes in that part. It would be great if you could comment on my code and see how it can turn into a script that actually work. By the way, in my actual data, the length of each line is not defined so I need to use malloc and realloc for that purpose.
For a simple program like this, where you look at short lines one at a time, you shouldn't worry about dynamic memory allocation. It is probably good enough to use local buffers of a reasonable size.
Another thing is that C isn't particularly suited for quick-and-dirty string processing. For example, there isn't a strstrip function in the standard library. You usually end up implementing such behaviour yourself.
An example implementation looks like this:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXLEN 80 /* Maximum line length, including null terminator */
int main(int argc, char *argv[])
{
FILE *in;
FILE *out;
char line[MAXLEN]; /* Current line buffer */
char ref[MAXLEN] = ""; /* Sequence reference buffer */
int nseq = 0; /* Sequence counter */
if (argc != 3) {
fprintf(stderr, "Usage: %s infile outfile\n", argv[0]);
exit(1);
}
in = fopen(argv[1], "r");
if (in == NULL) {
fprintf(stderr, "Couldn't open %s.\n", argv[1]);
exit(1);
}
out = fopen(argv[2], "w");
if (in == NULL) {
fprintf(stderr, "Couldn't open %s for writing.\n", argv[2]);
exit(1);
}
while (fgets(line, sizeof(line), in)) {
int len = strlen(line);
/* Strip whitespace from end */
while (len > 0 && isspace(line[len - 1])) len--;
line[len] = '\0';
if (line[0] == '>') {
/* First char is '>': copy from second char in line */
strcpy(ref, line + 1);
} else {
/* Other lines are sequences */
fprintf(out, "%s: %d\n", ref, len);
nseq++;
}
}
fprintf(out, "Total number of sequences. %d\n", nseq);
fclose(in);
fclose(out);
return 0;
}
A lot of code is about enforcing arguments and opening and closing files. (You could cut out a lot of code if you used stdin and stdout with file redirections.)
The core is the big while loop. Things to note:
fgets returns NULL on error or when the end of file is reached.
The first lines determine the length of the line and then remove white-space from the end.
It is not enough to decrement length, at the end the stripped string must be terminated with the null character '\0'
When you check the first character in the line, you should check against a char, not a string. In C, single and double quotes are not interchangeable. ">" is a string literal of two characters, '>' and the terminating '\0'.
When dealing with countable entities like chars in a string, use integer types, not floating-point numbers. (I've used (signed) int here, but because there can't be a negative number of chars in a line, it might have been better to have used an unsigned type.)
The notation line + 1 is equivalent to &line[1].
The code I've shown doesn't check that there is always one reference per sequence. I'll leave this as exercide to the reader.
For a beginner, this can be quite a lot to keep track of. For small text-processing tasks like yours, Python and Perl are definitely better suited.
Edit: The solution above won't work for long sequences; it is restricted to MAXLEN characters. But you don't need dynamic allocation if you only need the length, not the contents of the sequences.
Here's an updated version that doesn't read lines, but read characters instead. In '>' context, it stored the reference. Otherwise it just keeps a count:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h> /* for isspace() */
#define MAXLEN 80 /* Maximum line length, including null terminator */
int main(int argc, char *argv[])
{
FILE *in;
FILE *out;
int nseq = 0; /* Sequence counter */
char ref[MAXLEN]; /* Reference name */
in = fopen(argv[1], "r");
out = fopen(argv[2], "w");
/* Snip: Argument and file checking as above */
while (1) {
int c = getc(in);
if (c == EOF) break;
if (c == '>') {
int n = 0;
c = fgetc(in);
while (c != EOF && c != '\n') {
if (n < sizeof(ref) - 1) ref[n++] = c;
c = fgetc(in);
}
ref[n] = '\0';
} else {
int len = 0;
int n = 0;
while (c != EOF && c != '\n') {
n++;
if (!isspace(c)) len = n;
c = fgetc(in);
}
fprintf(out, "%s: %d\n", ref, len);
nseq++;
}
}
fprintf(out, "Total number of sequences. %d\n", nseq);
fclose(in);
fclose(out);
return 0;
}
Notes:
fgetc reads a single byte from a file and returns this byte or EOF when the file has ended. In this implementation, that's the only reading function used.
Storing a reference string is implemented via fgetc here too. You could probably use fgets after skipping the initial angle bracket, too.
The counting just reads bytes without storing them. n is the total count, len is the count up to the last non-space. (Your lines probably consist only of ACGT without any trailing space, so you could skip the test for space and use n instead of len.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]){
FILE *my_file = fopen(argv[1], "r");
FILE *my_output = fopen(argv[2], "w");
int total_sequence_coutns = 0;
char *sequence_name;
int dna_length;
char *line = NULL;
size_t size = 0;
while(-1 != getline(&line, &size, my_file)){
if(line[0] == '>'){
sequence_name = strdup(strtok(line, ">\n"));
total_sequence_coutns +=1;
continue;
}
dna_length = strlen(strtok(line, "\n"));
fprintf(my_output, "%s %d\n", sequence_name, dna_length);
free(sequence_name);
}
fprintf(my_output, "Total number of sequences = %d\n", total_sequence_coutns);
fclose(my_file);
fclose(my_output);
free(line);
return (0);
}

Resources