This will read 5 lines into a file from stdin, then print all the lines except the second one.
I used an array because I'm not sure how to do dynamic memory allocation.
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 1024
void write_to_file(const char *s);
void print_file(const char *s);
int main(void){
const char file_name[] = "text";
write_to_file(file_name);
print_file(file_name);
return 0;
}
void write_to_file(const char *s){
FILE *in_file;
if((in_file = fopen(s, "w")) == NULL){
perror(s);
exit(EXIT_FAILURE);
}
char buffer[MAX_SIZE];
for(int i = 0; i < 5; ++i){
fgets(buffer, sizeof(buffer), stdin);
fputs(buffer, in_file);
}
fclose(in_file);
}
void print_file(const char *s){
FILE *in_file;
if((in_file = fopen(s, "r")) == NULL){
perror(s);
exit(EXIT_FAILURE);
}
int c;
int flag = 0;
while((c = fgetc(in_file)) != EOF){
printf("%c", c);
if((flag == 0) && (c == '\n')){
while((c = fgetc(in_file)) != '\n'){
/*skip the second line*/
}
flag = 1;
}
}
}