The program does not want to read the file

this is my structure

   typedef struct {
        char mmsi[10];
        char name[20];
        double latitude;
        double longitude;
        int course;
        double speed;


this is my function that doesn't want to work

void searchByLatLong(double latitude, double longitude){
        FILE * file;
        struct dirent *drnt;
        DIR * dir = opendir("./text");
        char *path = (char *)malloc(19);
        Vessel *vessel = (Vessel *)malloc(sizeof(Vessel));

        while((drnt = readdir(dir)) != NULL) {
            if(strcmp(drnt->d_name,".") && strcmp(drnt->d_name,"..")) {


                file=fopen(path, "r");
                fscanf(file," %s %[a-zA-Z0-9 ]19s %lf %lf %d %lf", &vessel->mmsi,&vessel->name,&vessel->latitude,&vessel->longitude,&vessel->course,&vessel->speed);

        //  if (mmsi+".txt" == drnt->d_name){

        seekdir(dir, telldir(dir)); 

    //  if(this->mmsi == mmsi){
        //  printVessel();
    //  }



When I try to load the txt file, it only loads the first two lines and then throws garbage out of memory after that. Loading data into other variables doesn't change anything; / This is an example txt file to be loaded:

RMS Titanic



source to share

2 answers

The problem is with your format string. Correct format string:

" %s %19[a-zA-Z0-9 ] %lf %lf %d %lf"


The width of the field before the conversion specifier. In addition, the sequence [...] is a conversion specifier, just like 's'. The problem you are seeing is what fscanf()

handles '3' because it matches the first %s

. It then processes "RMS Titanic" because it matches %[a-zA-Z0-9 ]

, but then processing stops because there is no "19" in the input. At this point, the rest of the arguments are uninitialized.

You should check the return value from fscanf()

. It will tell you how many conversions have actually been performed.



Thank you for posting an interesting question; I learned about fscanf()

and the designation []

it takes.

The notation []

indicates that the string is being read, so the appended to it s

is considered a literal character to be matched. Likewise, the width specifier you specified 19

should appear before []


Your current code will start working if you have a ship named, for example, "RMS Titanic19s".

Change fscanf to:

fscanf(file," %s %19[a-zA-Z0-9 ] %lf %lf %d %lf",


and your code will start working.

Note that I fixed some compilation warnings, lose &

of char []

participants mmsi

and name

- they already point to a buffer that you want to fill. You don't need to &

face them. The pedagogical alternative form &vessel->mmsi[0]

is the address of the first character mmsi




All Articles