gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:11:28 25 4
gpt4 key购买 nike

我是编码新手,从技术上讲,我今年开始编码,需要大量练习。不管怎样,我试图修改文件中的变量,但浏览器或 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com