c - 如何修改 C 上文件中的变量?

标签 c database file

我是编码新手,从技术上讲,我今年开始编码,需要大量练习。不管怎样,我试图修改文件中的变量,但浏览器或 CPU 没有找到它。当我运行代码时,它可以写入变量并将其传递到文件并读取它,但它无法修改文件内部的变量。我尝试使用一段时间和一个如果,但我的知识不能比这些更远。

问题出在删除功能上,它可以读取文件或存档,但无法到达“文件末尾”,因为显然它不存在。该代码主要读取一串数字,并在 scanf 上输入 -1 时停止。它读取所有数字(包括 -1 两次),然后修改最后一个数字,将其转换为零。问题是最后一个函数无限重复

#include<stdio.h>
void Create(FILE *p){
p=fopen("c:Example", "rb");
if(!p){
    p=fopen("c:Example", "wb");
    printf("El texto fue creado \n");
    } else {
    printf("El texto ya existe \n");
    }
}
void Add(FILE *p){
int a;
p=fopen("c:Example", "ab");
if(p==NULL){
    printf("El texto no se encontro \n");
    } else {
    scanf("%d", &a);
    while(a!=-1){
        fwrite(&a, sizeof(int), 1, p);
        scanf("%d", &a);
        }
    fwrite(&a, sizeof(int), 1, p);
    }
fclose(p);
}
void Read(FILE *p){
int a;
p=fopen("c:Example", "rb");
if(p==NULL){
    printf("El texto no existe \n");
    } else {
    fread(&a, sizeof(int), 1, p);
    printf("%d \n", a);
    while(a!=-1){
        fread(&a, sizeof(int), 1, p);
        printf("%d \n", a);
        }
    }
fclose(p);
}
void Delete(FILE *p){
int a;
p=fopen("c:Example", "wb");
if(p==NULL){
    printf("El texto no existe \n");
    } else {
    fread(&a, sizeof(int), 1, p);
    printf("(1)\n");
    while(a!=-1){
        fread(&a, sizeof(int), 1, p);
        printf("(2)\n");
        }
    a=0;
    printf("(3)\n");
    }
fclose(p);
}

int main(){
FILE *p;
Create(p);
printf("\n");
Add(p);
printf("\n");
Read(p);
printf("\n");
Delete(p);
printf("\n");
Read(p);
printf("\n");
return 0;
}

最佳答案

解决该问题的方法可以是读取数字并生成一个新文件,其中最后一个数字为 -1,替换为 0。然后删除旧文件并将临时文件重命名为旧文件。我写了一个解决方案,试图不改变你的太多代码。我建议您更好地理解 fread()fwrite() 函数的工作原理。在 scanf 函数之后,我使用 getchar() 函数从缓冲区中删除按 Enter 时产生的新行。还要尝试使用空格和缩进来保持代码整洁。

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

void Create (FILE *p)
{

    if((p = fopen ("c:Example", "wb")) == NULL) // Check if file opening doesn't fail
    {
        printf ("Can't open file\n");
        exit (-1);
    }

    printf ("File exists\n");

    fclose (p); //Close file pointer
}

 void Add(FILE *p)
{
    int a = 0; // Initialize variable

    p = fopen("c:Example", "ab");

    if (p == NULL) // File opening failed
    {
        printf("El texto no se encontro \n");
        exit (-1); // Program has to terminate
    } 
    else 
    {

        while(a != -1)
        {
            printf ("Please insert number\n"); //Tell user what to do
            scanf ("%d", &a);
            getchar();
            fwrite(&a, sizeof(int), 1, p);
        }
    }

    fclose(p);
}

void Read(FILE *p)
{
    int a = 0; // Initialize variable
    p = fopen("c:Example", "rb");

    if(p == NULL) // Error opening file
    {
        printf("El texto no existe \n");
        exit (-1); // Program has to termimate
    } 
    else 
    {
        while(a != -1)
        {
                fread(&a, sizeof(int), 1, p);
                printf("I've read %d\n", a);
        }
    }

    fclose(p);
 }

 void Delete(FILE *p)
 {
     int a = 0;
     p = fopen("c:Example", "r"); //Open for read

     if(p == NULL) // Can't open file
     {
         printf("El texto no existe \n");
         exit (-1); //Exit the program
     } 

     else 
     {
        FILE *temp;
        if ((temp = fopen ("temp", "ab")) == NULL) //Create a temp file in append mode
         {
             printf ("Error creating temp file\n");
             fclose (p);
             exit (-1);
         }

         while (a != -1)
         {
             fread(&a, sizeof(int), 1, p);

             if (a != -1)
             {
                  fwrite (&a, sizeof(int), 1, temp); // write to temp only if is not -1
             }
         }

         a = 0;
         fwrite (&a, sizeof (int), 1, temp); //Write 0 to last position

         fclose (temp);
         fclose (p);
         remove ("c:Example"); //Remove old file
         rename ("temp", "c:Example"); //Rename new file
         }
  }

  void Read_new (FILE * p)
  {
     int a = -1 // Initialize a as -1;
     p = fopen ("c:Example", "rb");

     while (a != 0)
     {
          fread (&a, sizeof (int), 1, p);
          printf ("I've read %d\n", a);
     }

    fclose (p);
 }

 int main()
 {
    FILE *p;
    Create(p);
    Add(p);
    Read(p);
    Delete(p);
    Read_new(p);
    return 0;
 }

关于c - 如何修改 C 上文件中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58599862/

相关文章:

c - 在 Windows 上扩展 OpenCV 1.1 中 GUI 功能的最简单方法?

c - 为什么下面的C代码会跳过read()系统调用去执行下一个write()系统调用?

c - 网络传输对 cpu 的影响

database - 如何表示数据库中两个项目之间的关系?

file - 实现文件系统的基础

c - 为什么 (5+10)/2 是 7.0 而不是 7.5? C编程

c# - 使用强类型数据集在关系中插入行

android - 文件资源管理器在 Eclipse 中始终为空

ruby-on-rails - 如何使用 RSpec 测试日志内容?

php - PDO 在另一个数据库的表中插入查询不起作用