Linux opens the file and writes a string, while reading the information about the corresponding file
In Linux, using the C language, use the fopen () function to open a file (if no file exists, create a new one such as Data/test.dat), and use
Fwrite () writes the corresponding string to it, wraps each input write, and finally reads the appropriate information using fread ().
the corresponding code is as follows: TEST.c
/* Test.c:create a File:write a string to the file */#include <stdio.h> #include <string.h> #include & lt;stdlib.h> #define MAX 128 int main () {FILE *FP = NULL;
Notice char *filepath = "Data/test.dat";
Char stringdes[] = "Hello World";
Stringdes[strlen (stringdes)] = ';
Char Buf[max];
/* Open a file:file *fopen (const char *path, const char *mode);
* * #if 1 fp = fopen (FilePath, "at+");
if (NULL = fp) {perror ("FP");
return-1; printf ("File Open success!
\ n "); #endif/* Write data block to a file:size_t fwrite (const void* buffer, size_t size, size_t count, file* Stream)
;
*/int i;
for (i = 0; I <= 2; i++) {fwrite (Stringdes,strlen (Stringdes), 1, FP);
Fwrite ("\ n", 1, 1, FP);
Fflush (FP);
printf ("File set start \ n"); Fseek (Fp,0,seek_set);
Set start File Fread (Buf,strlen (stringdes), 1,FP);
Buf[strlen (stringdes)] = '; printf ("buf =%s \ n", buf);
Fclose (FP); }
the contents of Test.dat are as follows:
root@ubuntu:~/openandwritefile/zywtest/data# Vim Test.dat
Hello World, Hello World, Hello World ~
~
~