char* line;
FILE* source;
line=malloc(2000);segmentation fault on fgets.
source = fopen(argv[1]);
if (source != NULL)
{
fgets(line, 2000, source);
It is so discouraging that something that used to be simple for me to do for a living is now gone.
Conservative. Idaho. Software engineer. Historian. Trying to prevent Idiocracy from becoming a documentary.
Email complaints/requests about copyright infringement to clayton @ claytoncramer.com. Reminder: the last copyright troll that bothered me went bankrupt.
char* line;
FILE* source;
line=malloc(2000);segmentation fault on fgets.
source = fopen(argv[1]);
if (source != NULL)
{
fgets(line, 2000, source);
On linux fopen takes a mode argument, like "r", so maybe that's the problem?
ReplyDeleteHmm. this worked for me on Windows 10 with Visual Studio. (I skipped a bit, like closing the file).
ReplyDelete#include >stdio.h>
#include >stdlib.h>
int main(int argc, char *argv[])
{
char *line;
FILE *source;
line = malloc(2000);
source = fopen(argv[1], "r");
if (source != NULL)
{
fgets(line, 2000, source);
puts(line);
}
return 0;
}
It's been a long time but did you remember to #include ?
ReplyDeleteI also think you have to give a mode to fopen(), not just a filename.
ReplyDelete