1

In this program i'm trying to read a double and a char array from a file and print out the lines that have a double value more than the one entered into the argument. It compiles fine but when I run it I get the error :Segmentation fault(core dumped)

This is the program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char **argv[]) {


if(argc != 2)
        printf("enter 2 arguments\n");
else {
        int r;
        double tDate = atof(*argv[1]);
        double date = 0;
        char event[] = "";
        FILE *fp = fopen("tickler.dat","r");

        if( fp == NULL ) {
                perror("Error while opening the file.\n");
                exit(EXIT_FAILURE);
        }

        while(r = fscanf(fp, "%lf %s\n", &date, event) != EOF) {

                if(date > tDate)
                        printf("%d - %s", date, event);
        }

        fclose(fp);
}
return 0;
}

This is the file, "Tickler.dat"

150410 DR_APPOINTMENT
150420 MATH_DUE
150426 MEETING
150511 PRINT_HW

Any help would be appreciated.

Deanie
  • 2,316
  • 2
  • 19
  • 35
holta73
  • 13
  • 4
  • 4
    How big is the array `event[]`? How many characters? – Drew Dormann Apr 26 '15 at 22:41
  • 1
    Also, good time to learn to use a debugger. Very helpful in this (and many other) situations. – kaylum Apr 26 '15 at 22:46
  • In addition to the above comments : According to [this reply](http://stackoverflow.com/a/10368369/3729391), you should be using %lf in fscanf, which you already are, and %f in printf. Refer to @Angel Angel's comment and my response to it. – Hassan Apr 26 '15 at 23:04

2 Answers2

2

this line int main (int argc, char **argv[]) is correct or

int main (int argc, char **argv) 
int main (int argc, char *argv[])
Angel Angel
  • 19,670
  • 29
  • 79
  • 105
1

As well as a number of other problems diagnosed in comments and other answers, this line is incorrect:

while(r = fscanf(fp, "%lf %s\n", &date, event) != EOF) {

You need:

while ((r = fscanf(fp, "%lf %s\n", &date, event)) != EOF) {

And the test should really be:

while ((r = fscanf(fp, "%lf %s\n", &date, event)) == 2) {

unless you plan to deal with, for example, an alphabetic character where you were expecting a number (so that r == 0 after the fscanf() returns).

OTOH, since you assign to but don't actually use r, you could simplify the code to:

while (fscanf(fp, "%lf %s\n", &date, event) == 2) {

If you give event a proper size (bigger than 1 which you currently have), then you should write:

char event[32];

while (fscanf(fp, "%lf %31s\n", &date, event) == 2) {

to avoid buffer overflows. Note that there's a difference of 1 in the size specified in the format string and the size declared. Alternatively, on a system that supports POSIX 2008, you could use:

char *event = 0;

while (fscanf(fp, "%lf ^ms\n", &date, &event) == 2) {
    …code using event…
    free(event);
}

The m tells fscanf() to allocate the memory, and the value passed is a char ** as shown. If the scan fails, there is nothing to release.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278