gpt4 book ai didi

C - 构建后打开文本文件 - OpenWRT - opkg

转载 作者:行者123 更新时间:2023-11-30 19:35:59 26 4
gpt4 key购买 nike

我的 C 程序中有以下几行:

int main(int argc, char **argv) {    
int i=0, p=0;

FILE* fp;
fp = fopen("jacina.txt", "w+");
fscanf (fp, "%d", &i);

if (ftruncate(fp, 0) == -1) {
perror("Could not truncate")
};

p = i+10;
fprintf(fp, "%d", p);
}

在 OpenWRT(来自 Ubuntu)中将此代码构建为 OPKG 后,我如何读取和写入位于该 OPKG 所在任何磁盘位置的文本文件?

最佳答案

Your code doesn't make any sense. To write the input given by user to a file:

Create a file first. Take input from user (say any string) and write it to the file with the help of file descriptor (fp) and close the file so that all buffers get flushed.

FILE *fp;
char comment[100] = {0};
fp=fopen("tempfile.txt","w");

if (fp == NULL)
{
printf("Error opening file!\n");
exit(1);
}

printf("Enter String: ");
gets(comment);
fwrite(comment, sizeof(comment), 1, fp) ;

fclose(fp);

fprintf() too can be used instead to write data into a file.Similarly to read from a file you can use fgets() or fread() to store the contents of the file in a buffer and display the contents of the file. Hope it helps.

关于C - 构建后打开文本文件 - OpenWRT - opkg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41896389/

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