Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

[C] Reading in from a file in command line and outputting line numbers

$
0
0
So I'm trying to read in from a file name I specify in command line and read certain words and output the line number those are on.
I think I'm really close, but I'm missing a couple things.
Whenever I try to run my program, by writing "./a.out find.c temp.txt" I get a segmentation fault. can someone tell me what I might be inputing wrong?

Code is as follows
#include<stdio.h>
#include<string.h>

int main(int argc, char *argv[])
{
size_t len;
char *line;
if(argc!=3 ) // argc should be two
{
printf("Usage: %s filename", argv[0]);
}
else{
FILE *file =fopen(argv[1],"r");
if (file==0){
printf("Could not open file\n");
}
while(fgets(line,sizeof(line),file)){
if (strstr(line,"brown"))
{
printf("%s",line);
}


}
}
return 0;
}
    


I think I'm fairly close to the answer, if someone coudl jsut point me in the right direction.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>