-2

Im using fread() and fseek() togather to gather parts of a string. I'm not using fread() on the whole line though.

I'd take the whole line but to my knowledge you cannot use fseek() on a character array correct?

`int parse(const char *f, const struct stat *flightD, int type){ 
//file pointer
Airport_S *Air_Pts = (Airport_S *)malloc(sizeoftype(Airport_S));
FILE *fp;
//char need[10];
char airLineFile[2];
char chkAirPt[3];
fp = fopen(f, "r");
if(type == FTW_F){ // test for 'type' of FTW_F
        //check to see if the file opened successfully
    if(fp == NULL)
        printf("Cannot open file %s", f)
        return 1;
        while (!(FEOF)){
            //fgets(need,10,fp)
            //must return zero to parent funtion to continue tree traversal
            // ?? While current dir != originally called dir?
            //open the file, read it's contents and assess them
            fseek(fp, 5, SEEK_SET) //set FP to right before airport code
            chkAirPt = fread(chkAirPt,sizeof(char),3, fp)
            fseek(fp,0,SEEK_SET);
            //combine the airline abbreviation with '.txt'
            airLineFile = strcat(fread(airLineFile, sizeof(char), 2,                               fp),".txt");
            //if the struct has no values in it, populate it with this first one.
            if(Air_Pts->airport == NULL){
                //Set info for very first node
                Air_Pts->airPt=strcpy(Air_Pts->airport, chkAirPt);
                fseek(fp,0,SEEK_SET);
                Air_Pts->fltInfo->airLine=airLineFile;
                Air_Pts->fltInfo->next = NULL;
                Air_Pts->fltInfo->prev = NULL;
                Air_Pts->next = NULL;
                Air_Pts->prev = NULL;
                //what is the file going to do after this?
            }
            else if(strcmp(Air_Pts->airport, chkAirPt) == 0){
                if(strcmp(Air_Pts->fltInfo->airLine, airLineFile) == 0){
                    Air_Pts->fltInfo->occ++; 
                }
                else
                    Air_Pts->fltInfo = addAirline(Air_Pts->fltInfo);        
            }

            // some code
            return 0;
        else //anything other than a file -or- FTW_D
            return 1;
            }
}

}`

  • What do you mean by "not using fread() on the whole line"? fread doesn't know anything about "lines". It reads data. If you want to search through that data for a '\n', that's up to you. Also, using a global doesnt' fix the problem outlined in https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – William Pursell Nov 11 '19 at 21:37
  • Im only using fread() to read a couple characters of that first line If im not mistake, then i would like to go to the next line and do it all over again i need these characters: _ _ _ _ ! ! ! _ then go back to the beginning and get the first two: ! ! _ _ _ _ _ _. Then I need to go to the next line and do it all over again until I reach the end of file. – sellingItTrue Nov 11 '19 at 21:52
  • How do you know where the next line starts if you don't read the entire current line? – Andrew Henle Nov 11 '19 at 22:12
  • And what is `while (!(FEOF)){`? Please read [**Why is “while ( !feof (file) )” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – Andrew Henle Nov 11 '19 at 22:32
  • is it supposed to be -- while(!(EOF){} -- ? – sellingItTrue Nov 11 '19 at 23:15
  • IDK Im not sure the ins and the outs of 'fread()' , thats why im having trouble with it. I dont even understand if it takes the whole line how it knows to go to the new one. I know there is a new line character but what is there are other elements of the array that that line is read into do they just get thrown in the garbage? – sellingItTrue Nov 11 '19 at 23:18
  • _"you cannot use fseek() on a character array correct?"_, yes but so what? fseek operates on a file, what do _character arrays_ have to do with it? Simply read and discard character by character until you find the newline. It is not at all clear why you need fseek at all here. Also this code is not compilable. The line assigning airLineFile is incomplete. Better to give example file format and explain how you want to read it. – Clifford Nov 11 '19 at 23:28
  • i need to read a .txt file that looks like this **AA43 DTW2315 THEN OTHER CHARACTERS\n** line after line after line, and all I want to get is the **DTW** first, store it in an struct, then get the **AA** second then store that in another part of the struct then jump to the next line. – sellingItTrue Nov 11 '19 at 23:29
  • Which is why I was using `fseek()` I was finding the fifth character in the file regardless of what the prior characters are. Then I used `fseek()` again to put the file pointer to the begginning of the file then just read the first two characters, but Im stuck there, cuz I dont know how to jump to the next line to repeat the process. Should I just read the whole line in? Will that force a jump to the next line in the file? – sellingItTrue Nov 11 '19 at 23:37

1 Answers1

1

You are working too hard. Just read and discard the data you don't need. For example:

/* Sample input line: AA43 DTW2315 ... */
/* Read first two columns of each line of a text file into a struct */

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

struct data {
        char airport[32];
        char flight[32];
        struct data *next;
};

FILE * Fopen(const char *path, const char *mode);
void * xmalloc(size_t);

void
push(struct data **head, struct data new)
{
        struct data *t = xmalloc( sizeof *t);
        t->next = *head;
        strncpy(t->airport, new.airport, sizeof t->airport);
        strncpy(t->flight, new.flight, sizeof t->flight);
        *head = t;
}

int
main(int argc, char **argv)
{
        FILE *ifp = argc > 1 ? Fopen(argv[1], "r") : stdin;
        struct data *head = NULL;
        struct data this;
        int line = 1;
        int c = 0;

        while( (c = fscanf(ifp, "31%s %31s", this.airport, this.flight)) == 2) {
                push(&head, this);

                /* Discard until the end of line */
                while( (c = fgetc(ifp)) != EOF ) {
                        if( c == '\n') {
                                line += 1;
                                break;
                        }
                }
        }

        /* Print all the records in reverse order */
        for( ; head; head = head->next ) {
                printf(" %s: %s\n", head->airport, head->flight);
        }
        return 0;
}


FILE *
Fopen(const char *path, const char *mode)
{
        FILE *rv = fopen(path, mode);
        if( rv == NULL ) {
                perror(path);
                exit(EXIT_FAILURE);
        }
        return rv;
}

void *
xmalloc(size_t s)
{
        void *rv = malloc(s);
        if( rv == NULL ) {
                perror("malloc");
                exit(EXIT_FAILURE);
        }
        return rv;
}
William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • Be aware that [`strncy()` does not `NUL` terminate....](https://stackoverflow.com/questions/1453876/why-does-strncpy-not-null-terminate) – Andrew Henle Nov 12 '19 at 14:49