-1

I have these lines in my C program:

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);
}

After building this code to OPKG in OpenWRT (from Ubuntu), how can I read and write to this textual file which is located on any disk location where is located this OPKG?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
banki
  • 3
  • 3

1 Answers1

0

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.

Community
  • 1
  • 1
user7375520
  • 273
  • 2
  • 15