0

So this is just a simple read from file loop that I can't seem to figure what is going wrong. I've tried this a number of ways but I can't seem to get through the loop. I've tried a number of ways of getting into the (originally using getc, but with getc the whole loop would just be ignored.

For some reason it's just not working out for me. The way shown below, program freezes as soon as it gets to the while condition. Maybe someone can explain why this is so since the file pointer should be pointing to the beginning of the file. The file 'input.txt' is just a simple list of data that I should be reading into a struct then passing to another function to input into my data structure.

I cannot however seem to even get to the other function. Spent a few hours trying to go over different ways of solving this problem. I'm sure there's an obvious solution that I'm not seeing.

void readWrite(char *inFile)
{
    FILE *fp;
    FILE *fpBin;
    char c;
    stuBucket temprec;
    long address;

    //open binary file to initialize hash file
    if ((fpBin = fopen(BINARYFILE, "w+b")) == NULL)
    {
        printf("Could not open binary file %s.\n", BINARYFILE);
        return;
    }
    fwrite(fpBin, sizeof(struct student), 50, fpBin); //Write entire hash table to disk
    if (fp = fopen(inFile, "r") == NULL)     //open text file to read in from
    {
        printf("Could not open input file %s.\n", inFile);
        return;
    }
    printf("Input File %s Open.\n", inFile);

problem in while loop below

    while(!feof(fp))
    {
        printf("hello");
        fscanf(fp, "%d %s %s %f\n", temprec.stuID, temprec.lastName,
                                     temprec.firstName, &temprec.amount);
        writeRecord(temprec);
        insert(temprec, fpBin); //hash data to disk
        printf("insert done\n");
    }
    fclose(fp);
    fclose(fpBin);
}//end readFromFile

alternate while that gets skipped

c = getc(fp);
while(c != EOF)
{
    ungetc(c, fp);
    printf("hello");
    fscanf(fp, "%d %s %s %f\n", temprec.stuID, temprec.lastName,
                                 temprec.firstName, &temprec.amount);
    writeRecord(temprec);
    insert(temprec, fpBin); //hash data to disk
    printf("insert done\n");
    c = getc(fp);
}

contents of input.txt

6745 SMITH ANNA 7769.87
5675 JOHNSON SHEILA 23.91
1235 WILLIAMS JANE 93.12
2341 JONES BARBARA 74.23
8624 BROWN YELENA 56.75
9162 DAVIS SUSAN 902.34
7146 MILLER ALISON 8934.12
2328 WILSON ROYCE 123.09
1622 MOORE TONI 83.65
1832 TAYLOR JOAN 293.18
3271 GARCIA ROBERT 43.72
4717 MARTINEZ JHON 85.11
9345 ROBINSON ANDRE 15.67
1623 CLARK VICTOR 83.45
5673 HALL MARC 93.13
6275 ALLEN RAY 958.34
5392 HERNANDEZ MICHAEL 23.45
lloyd
  • 1,089
  • 1
  • 17
  • 39
  • Please edit the question and add a few lines of an example `inFile'. – Mahonri Moriancumer May 18 '14 at 21:17
  • 4
    1) Change `char c;` to `int c;` 2) feof() does not do what you want, better avoid it for the time being. – wildplasser May 18 '14 at 21:21
  • @MahonriMoriancumer done. Just a reminder to all: I can't even get into the loop. That's the main problem, not the contents of the actual loop-- while it may have errors I can take care of that. – lloyd May 18 '14 at 22:37
  • 1
    See [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) for why you don't write the loop the way you've tried to do so. And `getc()` returns an `int`, not a `char`; you will run into problems sooner or later if you don't fix that. – Jonathan Leffler May 18 '14 at 22:46

2 Answers2

2

One of your problems is:

if (fp = fopen(inFile, "r") == NULL)

It should (of course) be:

if ((fp = fopen(inFile, "r")) == NULL)

This then matches the line where you open the binary file.

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

In this case, fscanf may leave some space characters at the end of file. Always check how many values were successfully read by fscanf:

if (fscanf(fp, "%d %s %s %d\n", ...) != 4) break;
nullptr
  • 11,008
  • 1
  • 23
  • 18