Just for exercising some new things I've learned, I wrote this [useless] code which does actually the same thing "ls >> ..." does:
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
system("ls > list_of_files.txt");
FILE *f = fopen("list_of_files.txt", "r");
char *content;
fseek(f, 0L, SEEK_END);
long size = ftell(f);
rewind(f);
content = malloc(sizeof(char) * size);
fread(content, sizeof(char), size, f);
puts(content);
printf("%lu\n", size);
return 0;
}
however, then I thought that might also be possible to remove some lines by just straightly reading from the file and writing to the stdout. the code inside main:
Code:
system("ls -la > list_of_files.txt");
FILE *f = fopen("list_of_files.txt", "r");
fseek(f, 0L, SEEK_END);
long size = ftell(f);
rewind(f);
fread(stdout, sizeof(char), size, f);
which gives an error: segmentation fault(core dump). So I searched this error and it's when OS detects a program is trying to access a memory address that does not belong to it, so where have I done such a thing? Thanks in advance.