hi,
I need to write a function that prints "n" lines from a file (if n is bigger than the number of lines in the file than it just prints the whole file). I tried to write it but it doesn't work
Code:
void print_k_lines(char input_file[],int k)
{
char line[200];
int i =0, j =0, len;
FILE*file = fopen(input_file,"r");
if(file == NULL)
{
printf("fopen failed in print_k_lines\n");
return;
}
for(j =0; j <= k; j++)
{
len = strlen(line);
while(fgets(line, len, file)!= NULL)
{
printf("%s\n", line);
}
}
fclose(file);
}
I also need to write a function that reads a file and writes n lines on another file(same as above but the lines printed on another file).
this what i have done so far:
Code:
void copy_k_lines(char input_file[], char output_file[], int k)
{
int j, len;
char line[200];
FILE*in_file,*out_file;
in_file = fopen(input_file, "r");
out_file = fopen(output_file, "w");
if (in_file == NULL || out_file == NULL)
{
printf("fopen failed in print_k_lines\n");
return;
}
for (j = 0; j <= k; j++)
{
len = sizeof(line);
while (fgets(line, len, in_file) != NULL)
{
fputs = (line, out_file);
}
}
fclose(in_file);
fclose(out_file);
}
it just doesn't work
pls help