简体   繁体   English

从读取文件中修改第一个数字 c 语言

[英]modify first number from a read file c language

int main()
{

 FILE *file=fopen("numbers.dat","rb");

    int number;

    if(file ==NULL)
        exit(0);
    while(fread(&number,sizeof(int),1,file))
        printf("%d",number);


return 0 ;

}

The code below opens a file in read/write bytes mode and writes a number to the beginning of the file without erasing its content, is that what you need?下面的代码以读/写字节模式打开一个文件,并将一个数字写入文件的开头而不擦除其内容,这是你需要的吗?

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

int main () {
   FILE * fp;
   int a = 12;

   fp = fopen ("titi", "rb+");
   fwrite(&a, sizeof(int), 1, fp);
   fclose(fp);
   return(0);
}

If you want to read it first, you might want to look at the fseek() function to place your file cursor back at the beginning of the file or open the file twice, once to read it and once to write to it.如果您想先阅读它,您可能需要查看fseek() function 将文件 cursor 放回文件的开头或打开文件两次,一次读取,一次写入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM