C-fopen生成后的文本文件-OpenWRT-opkg

C-fopen生成后的文本文件-OpenWRT-opkg,c,linux,raspberry-pi,embedded-linux,openwrt,C,Linux,Raspberry Pi,Embedded Linux,Openwrt,我的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

我的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所在的任何磁盘位置上的文本文件

你的代码没有任何意义。要将用户提供的输入写入文件,请执行以下操作:

首先创建一个文件。从用户处获取输入(比如任何字符串),并在文件描述符(fp)的帮助下将其写入文件,然后关闭文件,以便刷新所有缓冲区

fprintf()也可以用来将数据写入文件。 与读取文件类似,可以使用fgets()或fread()将文件内容存储在缓冲区中并显示文件内容。希望能有帮助


你的代码没有意义。您正在打开一个文件以写入/追加数据,然后
fscanf
读取文件数据…是的,但这只是我代码的一部分。您使用opkg做什么?使用fgets(comment,100,stdin);而不是获取(评论);因为gets已被弃用
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);