c/c + +, MATLAB read and write files

Reference: Rookie tutorial

Open file

use   fopen( )   Function to create a new file or open an existing file. This call initializes the type   FILE   An object of type   FILE   Contains all the necessary information to control the flow. The following is the prototype of this function call:

FILE *fopen( const char * filename, const char * mode );

Here, filename   Is a string used to name the file and access mode   mode   The value of can be one of the following values:

patterndescribe
rOpen an existing text file to allow reading of the file.
wOpen a text file to allow writing to the file. If the file does not exist, a new file is created. Here, your program writes from the beginning of the file. If the file exists, it will be truncated to zero length and written again.
aOpen a text file and write the file in append mode. If the file does not exist, a new file is created. Here, your program will add content to the existing file content.
r+Open a text file to allow reading and writing files.
w+Open a text file to allow reading and writing files. If the file already exists, the file is truncated to zero length, and if the file does not exist, a new file is created.
a+Open a text file to allow reading and writing files. If the file does not exist, a new file is created. Reading starts at the beginning of the file, and writing can only be in append mode.

If you are dealing with binary files, you need to use the following access mode to replace the above access mode:

"rb", "wb", "ab", "rb+", "r+b", "wb+", "w+b", "ab+", "a+b"

Close file

Close the file and use the fclose() function. The prototype of the function is as follows:

 int fclose( FILE *fp );

1. Read and write files in C language

1.1 writing documents in C language

Write txt file

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

int saveDataToTXT(double y[], int length, const char* filePath) {
	int i = 0;

	FILE *fp;
	fp = fopen(filePath, "wb"); //Data export
	if (fp == NULL)
	{
		printf("cant open the file");
		exit(0);
	}

	for (i = 0; i<length; i++) {
		fprintf(fp, "%.2f\n", y[i]);	
	}
	fclose(fp);
	return 0;
}

int main()
{
	const char* filePath = "test.txt";
	double a[10] = { 0,1.0,2,3,4,5,6,7,8,9 };
	int length = 10;
	saveDataToTXT(a, length, filePath);

	system("pause");
    return 0;
}

Save to bin file

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

int saveDataToBin(int y[], int length, const char* filePath) {
	int i = 0, j = 0;										
	FILE *fpwrite;
	fpwrite = fopen(filePath, "wb"); //Data export
	if (fpwrite == NULL)
	{
		printf("cant open the file fpwrite");
		exit(0);
	}
	
	int nwrite = 0;
	nwrite = fwrite(y, sizeof(int), length, fpwrite);
	if (nwrite == 0 && filePath != NULL) {
		printf("write failed!");
	}
	fclose(fpwrite);

	return 0;
}

int main()
{
	const char* filePath = "test.bin";
	int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
	int length = 10;
	saveDataToBin(a, length, filePath);

	system("pause");
    return 0;
}

1.2 reading documents in C language

Read txt file, read bin file

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

//Read bin file
void read_bin(const char *path, int *buf, int size)  
{  
    FILE *infile;  
      
    if((infile=fopen(path,"rb"))==NULL)  
    {  
        printf( "\nCan not open the path: %s \n", path);  
        exit(-1);  
    }  
    fread(buf, sizeof(int), size, infile);
    fclose(infile);  
} 

//Read txt file
int readTXT(const char *filePath, int buf[], int length)
{
	FILE *fpRead = fopen(filePath, "rb"); //Read data
	if(fpRead == NULL)
	{
		printf("cant open the file fpRead");
		exit(0);
	}

	
	for(int i = 0; i < length; i++)
	{
		fscanf(fpRead,"%d ", &buf[i]);	
		printf("%d\n",buf[i]);		
	}
	return 0;
}

int main()
{
	int i=0;
	const char* filePath = "test.bin";
	int a[10] = { 1,1,2,3,4,5,6,7,8,9 };
	int b[10];
	int length = 10;
	//saveDataToTXT(a, length, filePath);
	//readTXT(filePath, b, length);

	saveDataToBin(a, length, filePath);
	read_bin(filePath, b, 10);
	for (i = 0; i < 10; i++)
	{
		printf("b[%d]=%d\n", i, b[i]);
	}
	
	system("pause");
    return 0;
}

2 c + + read and write files

C + + file and stream rookie tutorial

three   MATLAB read and write files

3.1 MATLAB file reading

Read bin file

fid  = fopen('rx_data.bin');

if fid==-1
    fprintf('file open error!\n');
    return;
end

//The data type stored in the bin file is int16
data = fread(fid, 900, 'int16');//Read 900 int16 data,
//data = fread(fid, 'int16'); // One time read

  Read txt file

data=load('data.txt');

3.2 MATLAB writing documents

Output to txt file

fid = fopen('char.txt','a+'); 
for i=1:10
    fprintf(fid,'%.2f\n',data(i));
end

fprintf(fid,'-----------------------------------------------\n'); 

fclose(fid);

Tags: C C++ MATLAB

Posted on Sun, 10 Oct 2021 06:22:42 -0400 by wednesday