Pass a char to C file

조회 수: 2 (최근 30일)
Sandeep
Sandeep 2013년 1월 16일
I am using an embedded Matlab code for calling a C code, which read a text file. However I need to update the code below to pass a the text file name. I think I need to pass a char to the code, but I am not sure how to modify the c code below:
#include <stdio.h>
#include <stdlib.h>
#include "readerext4.h"
void readerext4(int32_T size, real_T *txpos, real_T *typos, real_T *tzpos, real_T *tvxpos, real_T *tvypos, real_T *tvzpos)
{
int32_T row;
FILE *cfPtr; // cfPtr = flowfield.dat file pointer
FILE *cfPtrDebug;
if ( cfPtr = fopen( "Flowfield.dat", "r" ))
cfPtrDebug = fopen( "Debug.txt", "w" );
{
for ( row = 0; row < size; row++ )
{
fscanf( cfPtr, "%lf", txpos+row);
fscanf( cfPtr, "%lf", typos+row);
fscanf( cfPtr, "%lf", tzpos+row);
fscanf( cfPtr, "%lf", tvxpos+row);
fscanf( cfPtr, "%lf", tvypos+row);
fscanf( cfPtr, "%lf", tvzpos+row);
fprintf( cfPtrDebug, "%d\n", row + 1 );
}
fclose( cfPtr );
fclose( cfPtrDebug );
}
}
So instead of hard coding the the text file name (Flowfield.dat), I would like to pass a char (e.g. filename = flowfield.dat) to the C code. Can someone help, please? Thanks.

채택된 답변

Jan
Jan 2013년 1월 16일
void readerext4(int32_T size, real_T *txpos, real_T *typos, real_T *tzpos,
real_T *tvxpos, real_T *tvypos, real_T *tvzpos,
const char* FileName) /* added */
{ int32_T row;
FILE *cfPtr; // cfPtr = flowfield.dat file pointer FILE *cfPtrDebug;
if ( cfPtr = fopen(FileName, "r" ))
And append the "const char* FileName" in the header file also:
void readerext4(int32_T size, real_T *txpos, real_T *typos, real_T *tzpos,
real_T *tvxpos, real_T *tvypos, real_T *tvzpos,
const char* FileName);
  댓글 수: 3
Jan
Jan 2013년 1월 17일
I do not work with Simulink, but I'd expect, that it accepts mxChar, which is an UTF16 encoded character, or a uint16_T from the view of the compiler.
Sandeep
Sandeep 2013년 1월 18일
Thanks a lot Jan.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Naming Conventions에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by