質問

I'm reading in from a file emp and it was reading when the file (which is the last file below -- which worked) was structured as 10 records with a header (being skipped by the fseek() of 0 in the code). But when it reads in with the 3x10 format (which is the middle file below, which failed to read properly) with headers for each block of 3 -- that's failing. I'm not sure why the conditional just within the loop isn't catching and the 2nd conditional within the loop isn't printing everything marked with a 0 upfront.

    int   nDeleteSwitch;
    char  sSSN[10], sName[21];
    float nSalary;
    char nextIn[3];

    printf("SSN        NAME                 SALARY\n");

    mioHashFile = fopen("emp", "r");
    fscanf (mioHashFile,"%d",&mnOverFlowRecords);
    fseek (mioHashFile, mnHeaderSize, 0); //Skip past the CRLF at end of overflow counter

    //int numHeadRec = mnOverFlowRecords/3;
    /* sequentially print all active records */
    //for(int i=0;i<(numHeadRec+mnOverFlowRecords);i++)
    for(int i=0;i<20+mnOverFlowRecords;i++)
    {
        if ((fscanf(mioHashFile,"%d",nextIn)== -1) || (fscanf(mioHashFile,"%d",nextIn)== 0) ||
            (fscanf(mioHashFile,"%d",nextIn)== 1))
        {
            fscanf(mioHashFile,"%d%s%s%f",&nDeleteSwitch,sSSN,sName,&nSalary);
            //printf("%d",nDeleteSwitch);
            if (nDeleteSwitch==0)printf("%-11s%-21s%-10.2f\n",sSSN,sName,nSalary);  // wtf why this isn't printing 
            else if (nDeleteSwitch == -1) printf("there's a -1 on row: %d",i);
        }
        else {continue;};
    }
        fclose(mioHashFile);
        printf("Print Table Complete\n");

And here we have emp file that it's refusing to read the 0 entries from:

   0
Overflow page: 0 0 -1 
 -1         x                    x      0.00
 -1         x                    x      0.00
  0         x                    x      0.00
Overflow page: 1 0 -1 
 -1         x                    x      0.00
 -1         x                    x      0.00
  0         x                    x      0.00
Overflow page: 2 0 -1 
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00
Overflow page: 3 0 -1 
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00
Overflow page: 4 0 -1 
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00
Overflow page: 5 0 -1 
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00
Overflow page: 6 0 -1 
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00
Overflow page: 7 0 -1 
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00
Overflow page: 8 0 -1 
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00
Overflow page: 9 0 -1 
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00

So it won't read that, but it'll read this:

   0
  0       123                  asd    789.00
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00
  0       345                  zxc    234.00
 -1         x                    x      0.00
 -1         x                    x      0.00
 -1         x                    x      0.00

There's a space right before the -1 and there'd be 2 spaces before the 0. As shown in the code, I'm trying to print anything with a 0 at the beginning, and skip over the 'header' lines at the front of each hash block. When I try to force it to print (like print < 3) then it just comes out as garbage symbols.

What it should be doing is printing all records that have a 0 at the beginning and skipping over the headers (because they don't have -1,0,1 at the beginning).

役に立ちましたか?

解決

Given the declaration:

char nextIn[3];

This code is wrong:

if ((fscanf(mioHashFile,"%d",nextIn) == -1) || (fscanf(mioHashFile,"%d",nextIn) == 0) ||
    (fscanf(mioHashFile,"%d",nextIn) == 1))
    {

You are passing an array of three characters and expecting fscanf() to treat it as an int. Your conditions are odd, too. EOF is not guaranteed to be -1 (though I don't recall a system where it was not). There wouldn't be much point in trying again after detecting EOF (not without other activity to clear the EOF marker).

When I try this fscanf(mioHashFile,"%s",nextIn); if (strcmp(nextIn, '-1') == 0) it still crashes and burns opulently due to type incompatibility. @Emmet also caught my error on the EOF (in that answer's code). How would you do it, and still maintain the scanf(), printf() formatting that I'm trying to use?

That requires some semi-real coding. I'd use fgets() and sscanf() rather than fscanf():

char line[4096];
int lineno = 0;
while (fgets(line, sizeof(line), mioHashFile) != 0)
{
    if (++lineno == 1)
        continue;  // Skip the offset line
    if (line[0] == 'O') // Overflow line - skip
        continue;
    if (sscanf(line, "%d %9s %20s %f", &nDeleteSwitch, sSSN, sName, &nSalary) != 4)
    {
        fprintf(stderr, "Failed to scan data from: %s", line);
        continue;
    }
    if (nDeleteSwitch == 0)
        printf("%-11s%-21s%-10.2f\n", sSSN, sName, nSalary);
    else if (nDeleteSwitch == -1)
        printf("there's a -1 on row: %d\n", lineno);
    else
        printf("The delete switch value is %d\n", nDeleteSwitch);
}

Note that %s skips leading white space and then stops scanning at the next white space.

他のヒント

Almost all of the detail you supply seems irrelevant to your stated objective of printing out lines that begin with a zero and not printing out the others. The following program, for example, prints out all of the lines that begin with a zero, but no others.

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

#define BUF_SZ (512)
#define INFILE "datafile.txt"

int main(void) {
    char    buf[BUF_SZ] = {0};
    FILE   *fp;
    long    first;
    char   *endp;

    /* Open input file successfully or bail */
    fp = fopen(INFILE, "r");
    if( fp == NULL ) {
        fprintf(stderr, "Failed to open '%s'\n", INFILE);
        exit(EXIT_FAILURE);
    }

    /* Read each line, convert beginning to long and print if it's 0 */
    while( NULL!=fgets(buf, BUF_SZ, fp) && !feof(fp) ) {
        first = strtol(buf, &endp, 10);
        if( first==0L && endp!=buf ) {
            fputs(buf, stdout);
        }
    }

    fclose(fp);
    return 0;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top