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

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

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

跳至解决方案
2,727 次查看
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,707 次查看
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,676 次查看
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,644 次查看
caden013
Contributor III

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

0 项奖励
回复
2,723 次查看
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,722 次查看
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,708 次查看
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,704 次查看
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,695 次查看
converse
Senior Contributor V

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

0 项奖励
回复
2,692 次查看
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 项奖励
回复
817 次查看
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 项奖励
回复