You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C语言修改从文件读取的第一个数字?

Hey there! Let's adjust your code to modify the first integer in numbers.dat while keeping the rest of the functionality intact. Here's a step-by-step breakdown and the revised code:

Key Changes Needed

Your original code uses read-only mode ("rb"), which won't let us write back to the file. We need to switch to a read-write binary mode, plus handle moving the file pointer correctly to overwrite the first value.

Revised Code

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

int main() {
    // Open file in read-write binary mode (rb+ preserves existing content)
    FILE *file = fopen("numbers.dat", "rb+");
    int number;

    if (file == NULL) {
        perror("Error opening file"); // Print meaningful error instead of silent exit
        exit(EXIT_FAILURE);
    }

    // Read the first integer first
    if (fread(&number, sizeof(int), 1, file) == 1) {
        // Modify the first number here—example: multiply by 2, or set to a fixed value
        int modified_num = number * 2; 
        printf("Original first number: %d\n", number);
        printf("Updated first number: %d\n", modified_num);

        // Move file pointer back to the start to overwrite the first value
        if (fseek(file, 0, SEEK_SET) != 0) {
            perror("Error moving file pointer");
            fclose(file);
            exit(EXIT_FAILURE);
        }

        // Write the modified number back to the file
        if (fwrite(&modified_num, sizeof(int), 1, file) != 1) {
            perror("Error writing to file");
            fclose(file);
            exit(EXIT_FAILURE);
        }

        // Reset pointer to start to print all numbers (including the updated one)
        fseek(file, 0, SEEK_SET);
    } else {
        printf("File is empty or failed to read first number\n");
    }

    // Print all numbers from the file (now with modified first value)
    printf("All numbers in file:\n");
    while (fread(&number, sizeof(int), 1, file)) {
        printf("%d ", number);
    }
    printf("\n");

    // Always close the file to avoid resource leaks
    fclose(file);
    return 0;
}

What's Different?

  • Read-Write Mode: We use "rb+" instead of "rb"—this lets us read and write to the file without erasing existing content (avoid "wb+" here, it would truncate the file).
  • File Pointer Management: After reading the first number, the pointer is at the second integer's position. We use fseek(file, 0, SEEK_SET) to jump back to the start so we can overwrite the first value.
  • Better Error Handling: Instead of silently exiting with exit(0), we use perror() to show exactly what went wrong (e.g., file not found, permission issues).
  • Proper File Closure: The original code didn't call fclose(file)—this fixes that to prevent resource leaks.

You can adjust the modified_num assignment to whatever value you need (e.g., set it to 999 directly instead of multiplying by 2).

内容的提问来源于stack exchange,提问作者berkcann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 22:52:52