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.
解決済! 解決策の投稿を見る。
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.
I want to know please, where we can find the created file (on the computer or on the flash memory of the microcontroller)
The created file will be in the same folder as the source code, which creates the file.
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
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?
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.
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?
Do you ever fclose the file? If not, try fflush. The data is probably cached.
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;
}
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);
Thanks,