Hot!fopen , fprintf , fclose.

Author
zekageri
Starting Member
  • Total Posts : 85
  • Reward points : 0
  • Joined: 2018/01/15 06:20:15
  • Location: 0
  • Status: offline
2018/03/14 03:18:41 (permalink)
0

fopen , fprintf , fclose.

Hello guys!
Iam at a beginner stage in C programming. I would like to write a code which is open a txt file and write something to it. I'am using MPLAB X ide (latest) and an XC8 compiler ( latest ) . When i write these words : fopen , fprintf , fclose FILE , the compiler says   : undefined identifier "FILE" and such.
I included the stdio.h standard library where these commands are declared and the compiler doesn't like it anyway.
 
Here is my code:
 
#1

9 Replies Related Threads

    zekageri
    Starting Member
    • Total Posts : 85
    • Reward points : 0
    • Joined: 2018/01/15 06:20:15
    • Location: 0
    • Status: offline
    Re: fopen , fprintf , fclose. 2018/03/14 03:19:58 (permalink)
    0
    Here is a code  i can't post with it:
     

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

    #define _HOSTED

    void main (void) {
                
       FILE * fp;

       fp = fopen ("file.txt", "w+");
       fprintf(fp, "%s %s %s %d", "We", "are", "in", 2018);
       
       fclose(fp);
       
       return(0);
    }

    #2
    rodims
    Super Member
    • Total Posts : 1377
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: fopen , fprintf , fclose. 2018/03/14 03:26:35 (permalink)
    +1 (1)
    When i write these words : fopen , fprintf , fclose FILE , the compiler says   : undefined identifier "FILE" and such.

     
    and such ...
     
    Please don't try to describe what your compiler outputs.
    Post it literally by copy and paste
     
    ... XC8 compiler ... latest

    This does not mean anything. Please specify your version exactly.
    post edited by rodims - 2018/03/14 03:27:44
    #3
    zekageri
    Starting Member
    • Total Posts : 85
    • Reward points : 0
    • Joined: 2018/01/15 06:20:15
    • Location: 0
    • Status: offline
    Re: fopen , fprintf , fclose. 2018/03/14 03:31:37 (permalink)
    0
    Iam using XC8 1.45 and MPLAB X ide v4.15. The error is this.
     
     
    make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
    make[1]: Entering directory 'C:/Users/Hitec/MPLABXProjects/fileproba.X'
    make -f nbproject/Makefile-default.mk dist/default/production/fileproba.X.production.hex
    make[2]: Entering directory 'C:/Users/Hitec/MPLABXProjects/fileproba.X'
    "C:\Program Files (x86)\Microchip\xc8\v1.45\bin\xc8.exe" --pass1 --chip=18F97J60 -Q -G --double=24 --float=24 --emi=wordwrite --opt=+asm,-asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=pro -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,-plib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" -obuild/default/production/newmain.p1 newmain.c
    newmain.c:8: error: (192) undefined identifier "FILE"
    newmain.c:8: error: (192) undefined identifier "fp"
    newmain.c:10: warning: (361) function declared implicit int
    newmain.c:11: warning: (361) function declared implicit int
    newmain.c:13: warning: (361) function declared implicit int
    newmain.c:15: error: (204) void function can't return a value
    (908) exit status = 1
    nbproject/Makefile-default.mk:106: recipe for target 'build/default/production/newmain.p1' failed
    make[2]: Leaving directory 'C:/Users/Hitec/MPLABXProjects/fileproba.X'
    nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failed
    make[1]: Leaving directory 'C:/Users/Hitec/MPLABXProjects/fileproba.X'
    nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
    make[2]: *** [build/default/production/newmain.p1] Error 1
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2
    BUILD FAILED (exit value 2, total time: 1s)
     
    #4
    mbrowning
    Just a Member
    • Total Posts : 1154
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: online
    Re: fopen , fprintf , fclose. 2018/03/14 05:32:28 (permalink)
    +1 (1)
    These functions don't make sense on an 8bit microcontroller. Embedded code is different from PC code. Your main routine, for example, should never exit as there is no operating system to exit to.
     
    Looking at stdio.h, these function prototypes, and FILE, are only defined if "_HOSTED" is defined, which on a PIC16/18 is basically never. Probably only if you run an RTOS with a file system.
     
    So don't use FILE, fopen, etc.

    Oh well - there's always next year
    #5
    qɥb
    Monolothic Member
    • Total Posts : 3332
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: fopen , fprintf , fclose. 2018/03/14 05:33:08 (permalink)
    +1 (1)
    You can't use any of those file based commands, because your PIC does not contain a file operating system.
    Where exactly do you expect the files to be saved?
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #6
    zekageri
    Starting Member
    • Total Posts : 85
    • Reward points : 0
    • Joined: 2018/01/15 06:20:15
    • Location: 0
    • Status: offline
    Re: fopen , fprintf , fclose. 2018/03/14 05:53:30 (permalink)
    0
    In a demo code , there is a txt file and i thought i can do the same , add an existing txt file to the project and write to it.
    #7
    NKurzman
    A Guy on the Net
    • Total Posts : 16665
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: fopen , fprintf , fclose. 2018/03/14 06:25:14 (permalink)
    +1 (1)
    “It” there is no hard drive on the chip. It would need to be in the RAM or the flash. This is possible on small 8 bit chips, but not common.
    #8
    zekageri
    Starting Member
    • Total Posts : 85
    • Reward points : 0
    • Joined: 2018/01/15 06:20:15
    • Location: 0
    • Status: offline
    Re: fopen , fprintf , fclose. 2018/03/14 06:29:29 (permalink)
    0
    There is a web for this , so maybe i save texts to the RAM and print it to the website. Maybe that is the way. Or if i write to a txt file trought RS232 maybe?
     
    The thing is i want to log and count who and when logged into the web.
    post edited by zekageri - 2018/03/14 06:32:29
    #9
    rodims
    Super Member
    • Total Posts : 1377
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: fopen , fprintf , fclose. 2018/03/14 07:00:09 (permalink)
    +2 (2)
    As already told here, you cannot simply write to a file. But even if you could, this seems not to help you with your application.  Which example code are you referring to and what does "there is a web for this" mean ? Does it have anything to do with Microchip embedded devices ?
     
    Or if i write to a txt file trought RS232 maybe?

     
    Your XC8 1.45 compiler manual says:
     
    The printf() function is a formatted output routine, operating on stdout. It relies on
    the putch() function to determine the destination of the standard output stream.
     
    And also the documentation for putch (DS50002053H-page 401)

    The putch() function is provided as an empty stub which can be completed as each

    project requires. It must be defined if you intend to use the printf() function. Typically this function will accept one byte of data and send this to a peripheral which is
    associated with stdout.
     
    You might use fprintf with stdout (or similar) as parameter, which then would be the same as printf, but since this would not have any benefit, just look at printf for now.  (also I have no idea bout Microchips implementation).
     
    This allows you to reroute your output through e.g. UART, or do whatever you want with that output.
    You also will find threads in the forum dealing with rerouting printf.
    But then the next question would be, WHY do you you want to use the printf family ?

    So I still doubt that this is what you really need, you would have to explain a bit what you want to do
     
     
     
    #10
    Jump to:
    © 2018 APG vNext Commercial Version 4.5