简体   繁体   中英

Program doesnt want to read file

this is my struct

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

this is my function which doesnt 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 txt file it loads only two first strings then after it theres some rubbish from memory. Loading the data to another variables changes nothing ;/ This is a sample txt file which should be loaded:

3
RMS Titanic
22.222
33.333
4
5.9

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

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

The field width goes before the conversion specifier. Also, the [...] sequence is a conversion specifier, just like 's'. The problem you're seeing is that fscanf() processes the '3' because it matches the first %s . Then it processes the 'RMS Titanic' because it matches %[a-zA-Z0-9 ] but then processing stops because there is no '19s' in the input. At this point the remaining arguments are uninitialized.

You should check the return value from fscanf() . It will tell you how many conversions were actually performed.

Thanks for posting an interesting question; I learned about fscanf() and the [] notation it accepts.

The [] notation specifies that a string is being read, therefore, the s you have appended to it is considered a literal character that should match. Similarly, the width-specifier you have provided, 19 should appear prior to the [] .

Your current code would start working if you had a ship named, eg, "RMS Titanic19s".

Change your 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.

Notice I fixed some compile warnings by dropping the superfluous & from the char [] members mmsi and name -- these already point to the buffers you wish to fill. You don't need & in front of them. A pedagogical alternative form is &vessel->mmsi[0] -- the address of the first character of mmsi .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM