C 增量 ++ 意想不到的结果

问题描述 投票:0回答:2

我在尝试增加 int 变量的值时使用增量 ++ 时遇到问题。我将称为记录的变量的地址传递给函数 open(),然后以二进制模式打开文件,然后解析文件以查找文件中存在的结构记录元素的数量。问题是,当使用增量 ++ 作为

*records++;
时,结果不是预期的,例如我在 .dat 文件中有 4 条记录,它打印:

HELLO! 1
HELLO! 0
HELLO! -251258176
HELLO! 22074

回到主窗口打印:

Records in file: 0.

现在奇怪的是,我没有使用increment ++,而是使用了

*records += 1;
,结果正如预期的那样!

HELLO! 1
HELLO! 2
HELLO! 3
HELLO! 4

回到主线:

Records in file: 4.

我不明白问题出在哪里。部分代码如下:

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

#define true 1
#define false 0
#define size 100

typedef struct record {
    char firstName[size], lastName[size];
    int age;
    double grade;
} record;

int open(const char *filename, FILE **fp, int *records);

int main() {
    FILE *fp;
    char filename[] = "records.dat";
    int records = 0, opened = false, ch = 1;

    while(ch > 0 && ch < 5) {

        printf("Select your choice from the list below: \n");
        printf("1. Open file and get amount of records in it\n");
        printf("Enter your choice: ");
        scanf("%d", &ch);
        while(getchar() != '\n');

        switch(ch) {
            case 1:
                if(opened) {
                    printf("\nFile already open, try closing it first please.\n");
                    break;
                }

                /*printf("\nEnter .bin or .dat file name to open (or create one): ");
                fgets(filename, size, stdin);
                len = strlen(filename);

                if(filename[len - 1] == '\n') {
                    filename[len - 1] = '\0';
                } else if(len + 1 == size) {
                    while(getchar() != '\n');
                }*/

                opened = open(filename, &fp, &records);

                if(opened) {
                    printf("\nRecords in file: %d.\n", records);
                } else { // Failed to open AND create file
                    return 1;
                }

                break;

            default:
                printf("\nGoodbye!\n");
                if(opened) {
                    fclose(fp);
                }
                return 0;
                break;
        }
    }
}

int open(const char *filename, FILE **fp, int *records) {
    char temp[size];
    record r;
    *records = 0;

    printf("Attempting to open file...\n");
    *fp = fopen(filename, "rb+");
    if(*fp == NULL) {
        printf("File doesn't exit, attempting to create file...\n");
        *fp = fopen(filename, "wb+");
        if(*fp == NULL) {
            fprintf(stderr, "Couldn't open neither create %s, exiting...\n", filename);
            return false;
        }
        printf("File created.\n");
        return true;
    }
    printf("File found!\n");

    while(fread(&r, sizeof(r), 1, *fp) == 1) {
        *records++;
        //*records += 1;
        printf("HELLO! %d\n", *records);
    }

    return true;
}
c file-io increment
2个回答
0
投票

++
(增量)比
*
(解除引用)绑定得更紧密。基本上,您所做的是更改指针,而不是值。相当于:

*(records+=1);

尝试使用

(*records)++
来强制首先取消引用。

但大多数情况下,不要修改这样的参数——而是将其设为返回值:

int open(...) {
  int records=-1;
  while( read ...) {
    // do stuff
    records++;
  }
  return records;
}

并使用

-1
表示“错误”。

或者,如果必须修改参数:

int open(..., int* records) {
   int records_read = 0;
   while( read ... ) {
     // do stuff
     records_read++;
   }
   *records = records_read;
   return true;
}

0
投票

发生这种情况是因为运算符 '++' 的 priority 比 '' 具有更高的优先级,但 '+=' 的优先级比 '' 低

你必须做

(*records)++

*records++ == *(records++)

© www.soinside.com 2019 - 2024. All rights reserved.