Thread: Function/Text File Question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4

    Question Function/Text File Question

    I have declared a text file (named textfile) in my void main.
    But I want to be able to write in that text file from a function.
    I tryed:
    Code:
    WriteTextFunction(textfile);
    And for the function:
    Code:
    void WriteTextFunction(textfile)
    {
    	fprintf(textfile,"Text...");
    }
    Can anyone help me on that ?

    Thanks.

  2. #2
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by SPIRIT6666
    I have declared a text file (named textfile) in my void main.
    But I want to be able to write in that text file from a function.
    I tryed:
    Code:
    WriteTextFunction(textfile);
    And for the function:
    Code:
    void WriteTextFunction(textfile)
    {
    	fprintf(textfile,"Text...");
    }
    Can anyone help me on that ?

    Thanks.

    First off, you shouldn't be using void main. The main function returns and int, so it should be declared like this:

    int main(void)

    if you're not using argc and argv.

    Second, you don't show your declaration of textfile, so I'm not sure how you've declared it. But making a lot of assumptions from your attempted usage, you need to do something like this:

    Code:
       FILE *textfile;
    
       textfile = fopen("myfile.txt","w");
    
        /* then later.... */
       fprintf(textfile,"Text....");

    I have omitted error checking on the fopen (which you should do) and I have not closed the file after finishing with it (which you should also do).

    This should be enough to get you started in the right direction.
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    void WriteTextFunction(textfile)
    {
    	fprintf(textfile,"Text...");
    }
    You need a type for textfile:
    Code:
    void WriteTextFunction(FILE *textfile)
    {
    	fprintf(textfile,"Text...");
    }
    And maybe a function prototype:
    Code:
    void WriteTextFunction(FILE *textfile);
    First off, you shouldn't be using void main. The main function returns and int, so it should be declared like this:

    int main(void)

    if you're not using argc and argv.
    Sort of. Here's the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284376

    File I/O FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    I probably should have mentioned that I code to C99, so as it says in the FAQ, the only two acceptable alternatives are
    Code:
    int main(void)
    int main(int argc, char *argv[])
    which was what I was getting at when I said use

    Code:
    int main(void)
    if you're not using argc/argv....

    Or did I misunderstand what you were getting at?
    Mr. Blonde: You ever listen to K-Billy's "Super Sounds of the Seventies" weekend? It's my personal favorite.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    I tryed the code that dwks said, and it work perfectly now.
    Thanks alot fgw_three and dwks for the help.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by fgw_three
    I probably should have mentioned that I code to C99, so as it says in the FAQ, the only two acceptable alternatives are
    Code:
    int main(void)
    int main(int argc, char *argv[])
    which was what I was getting at when I said use

    Code:
    int main(void)
    if you're not using argc/argv....

    Or did I misunderstand what you were getting at?
    That's what I would have said a while ago too, until Prelude/Narue indicated why
    Code:
    int main()
    is okay, too, somewhere on Daniweb. I don't remember the full explanation, but I'll find it if you want.

    In that respect, the FAQ is actually wrong when it says
    (C) The difference between int main() and int main(void)

    A common misconception for C programmers, is to assume that a function prototyped as follows takes no arguments:

    int foo();

    In fact, this function is deemed to take an unknown number of arguments. Using the keyword void within the brackets is the correct way to tell the compiler that the function takes NO arguments.
    That's true as far as it goes, but it doesn't apply to the function declaration
    Code:
    int main() {
    , only to prototypes.

    Not that I like int main() -- I always use int main(void) -- but it is okay by the standard.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM