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;
    }Vessel;

      

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,"..")) {

                strcpy(path,"text/");
                strcat(path,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){
                    printf("%s\n%s\n%lf\n%lf\n%d\n%lf\n\n",vessel->mmsi,vessel->name,vessel->latitude,vessel->longitude,vessel->course,vessel->speed);
            //}


            fclose(file);
        }
        seekdir(dir, telldir(dir)); 

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

    }
    closedir(dir);
}

      

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:

3
RMS Titanic
22.222
33.333
4
5.9

      

+3


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.

+2


source


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",
    vessel->mmsi,vessel->name,&vessel->latitude,
    &vessel->longitude,&vessel->course,&vessel->speed);

      

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

.

0


source







All Articles