Writing to a file (fprintf) in MCUXpresso IDE (11.3.1)

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Writing to a file (fprintf) in MCUXpresso IDE (11.3.1)

ソリューションへジャンプ
2,923件の閲覧回数
caden013
Contributor III

Hi all,

I am wanting to print contents to a file using the C function "fprintf," but I cannot get the code to work properly. Here is my code: 

FILE *analog_values = fopen("analog_values.txt", "w");
if (analog_values == NULL)
{
PRINTF("File could not be opened\n");
}

FPRINTF(analog_values, "%d\n", g_LpadcResultConfigStruct.convValue >> g_LpadcResultShift);

fclose(analog_values);

 

When I run this code, I receive "File could not be opened" in the terminal every time, and the rest of my code (not shown here) does not run. If anyone can help, I would greatly appreciate it.

Thanks in advance.

0 件の賞賛
返信
1 解決策
2,903件の閲覧回数
converse
Senior Contributor V

Suggest you use the lower case functions calls (printf, scan etc) as the UPPER CASE are macro's and their actual function depends on the header files used, and the setting of various #defines. Suggest you read the section of the User Guide on C/C++ support - this describes how to use the sim hosting libraries and the use of printf (and macros etc)

Make sure the correct project is selected in project explorer to ensure the library types are selectable.

元の投稿で解決策を見る

9 返答(返信)
2,872件の閲覧回数
MK001
Contributor I

I want to know please, where we can find the created file (on the computer or on the flash memory of the microcontroller)

0 件の賞賛
返信
2,840件の閲覧回数
caden013
Contributor III

The created file will be in the same folder as the source code, which creates the file.

0 件の賞賛
返信
2,919件の閲覧回数
converse
Senior Contributor V

1. Make sure you use fprintf instead of FPRINTF (to avoid the macros)

2. Make sure you are using the semihosting version of the library. Either Redlib (semihost), Newlib (semihost) or NewlibNano (semihost) from the Quick Settings

Screenshot 2021-06-08 at 16.02.54.png

 

2,918件の閲覧回数
caden013
Contributor III

Thanks for the quick response.

I should have mentioned that I used a macro to define FPRINTF as fprintf -- just to maintain the formatting like the other functions (PRINTF, GETCHAR, SCANF, etc.). 

From the quick settings, I can access the "Set library/header type" tab, but all the options are grey and inaccessible. How can I go about fixing this issue?

0 件の賞賛
返信
2,904件の閲覧回数
converse
Senior Contributor V

Suggest you use the lower case functions calls (printf, scan etc) as the UPPER CASE are macro's and their actual function depends on the header files used, and the setting of various #defines. Suggest you read the section of the User Guide on C/C++ support - this describes how to use the sim hosting libraries and the use of printf (and macros etc)

Make sure the correct project is selected in project explorer to ensure the library types are selectable.

2,900件の閲覧回数
caden013
Contributor III

Thank you -- that seemed to work. My code now creates a file -- exactly what I want it to do. However, the file is blank, and I can't seem to get anything to print into it. 

I am using the following line of code to print to my file: 

fprintf(analog_values, "%d\n", (g_LpadcResultConfigStruct.convValue) >> g_LpadcResultShift);

Is there something wrong with this line, or is the problem somewhere else?

0 件の賞賛
返信
2,891件の閲覧回数
converse
Senior Contributor V

Do you ever fclose the file? If not, try fflush. The data is probably cached.

0 件の賞賛
返信
2,888件の閲覧回数
caden013
Contributor III

I used the fflush function before and after the fprintf statement to no avail. Here is the code that I am using now:

FILE *analog_values = fopen("analog_values.txt", "w");

int x = 0;

while (x < 500)
{
LPADC_DoSoftwareTrigger(DEMO_LPADC_BASE, 1U); /* 1U is trigger0 mask. */
while (!g_LpadcConversionCompletedFlag)
{
}
PRINTF("ADC value: %d\r\n",
((g_LpadcResultConfigStruct.convValue) >> g_LpadcResultShift));
g_LpadcConversionCompletedFlag = false;

if (analog_values == NULL)
{
PRINTF("File could not be opened\n");
}

fprintf(analog_values, "%d\n", (g_LpadcResultConfigStruct.convValue) >> g_LpadcResultShift);
fflush(analog_values);

if ( (int)g_LpadcResultConfigStruct.convValue > 0 && (int)g_LpadcResultConfigStruct.convValue < 16384)
{
GPIO_PinWrite(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, 1);
GPIO_PinWrite(GPIO, BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN, 0);
GPIO_PinWrite(GPIO, BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN, 0);
}

else if ( (int)g_LpadcResultConfigStruct.convValue > 16384 && (int)g_LpadcResultConfigStruct.convValue < 32768)
{
GPIO_PinWrite(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, 0);
GPIO_PinWrite(GPIO, BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN, 1);
GPIO_PinWrite(GPIO, BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN, 0);
}

else if ( (int)g_LpadcResultConfigStruct.convValue > 32768 && (int)g_LpadcResultConfigStruct.convValue < 49152)
{
GPIO_PinWrite(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, 0);
GPIO_PinWrite(GPIO, BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN, 0);
GPIO_PinWrite(GPIO, BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN, 1);
}

else if ((int)g_LpadcResultConfigStruct.convValue > 49152 && (int)g_LpadcResultConfigStruct.convValue < 65536)
{
GPIO_PinWrite(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, 1);
GPIO_PinWrite(GPIO, BOARD_LED_GREEN_GPIO_PORT, BOARD_LED_GREEN_GPIO_PIN, 1);
GPIO_PinWrite(GPIO, BOARD_LED_BLUE_GPIO_PORT, BOARD_LED_BLUE_GPIO_PIN, 1);
}

else
{
GPIO_PinWrite(GPIO, BOARD_LED_RED_GPIO_PORT, BOARD_LED_RED_GPIO_PIN, 1);
}

x++;

}

fclose(analog_values);

return 0;
}

 

0 件の賞賛
返信
1,013件の閲覧回数
mjkimbravo
Contributor III

Hello,

I have a similar problem of yours, my code is as below (only parts). Both fprintf and fputs don't work. The file "fromIC.txt" is made well in the "doc" folder of my SDKs. Would you please teach me how to write .csv or .txt file from MCUXpresso IDE?. My MCU is QN9080, and library is Redlib(semihost). I also attach the error message.

Please help me;;

 

FILE* datas;

datas = fopen("fromIC.txt", "w");

int inter = 123;

char str[] = "string ccc";

// fprintf(datas, "integer: %d", inter);

fputs(str, datas);

fclose(datas);

 

 

mjkimbravo_0-1721377492798.png

 

 

mjkimbravo_1-1721377657345.png

 

 

Thanks,

 

タグ(4)
0 件の賞賛
返信