I want to input in the name of the file and then display the contents of the file (contains lines of numbers). The code I have used is:
#include <stdio.h>
#include <stdlib.h>
void main(int argc,char *argv[])
{
FILE *file;
file = fopen(argv[1],"r");
char line[100];
while(!feof(file)){
fgets(line,100,file);
puts(line);
}
fclose(file);
}
When I try to run the program in Code Blocks, it just crashes the program. I tried running in Xcode and I get the message Segmentation fault: 11 and then the program just quits. Can someone please help?
Ok so I tried doing it another way, but still no success:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main(int argc,char *argv[])
{
FILE *file;
char line[100];
file = fopen(argv[1],"r");
do {
char c = fgetc(file);
if (c == EOF) break;
memset(line,0,100);
while ((c != '\n') && (c != EOF)) {
c = fgetc(file);
}
printf("%s",line);
} while (true);
fclose(file);
}