File output and functions

This is a discussion on File output and functions within the C Programming forums, part of the General Programming Boards category; Hi guys, i'm here bothering you again... I have a problem and i know i'm messing up the pointers (as ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    17

    File output and functions

    Hi guys, i'm here bothering you again...

    I have a problem and i know i'm messing up the pointers (as usual) :oP

    the thing is... i have a function that opens a file, this function calls another function that goes through a list and find out what will be "printed" in that file, then this function calls another function that will really "prints" inside the file...

    the thing is that i'm getting files with size 0 and it's probably because well, i need to pass the file variable by reference right??? and i think i'm messing up the * chars :oP

    could someone give me just an example how i do it???

    and btw, i'm using fprintf because i need to print 2 strings at the same time :o)

    my fprintf is this

    Code:
    fprintf (*arq,"\"%s\" %s\n", X->elem.toelem->nomelem->tpselem,X->elem.toelem->fonelem->tpselem);break;
    its inside a switch that checks what kind of element it is :o) and the first string needs to be "printed" inside ".

    could someone help me???

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >fprintf (*arq,......
    The first of fprintf() is a FILE*. Now, normally you wouldn't pass one of those around by reference, only by value (unless you actually wanted to assign it a new value). So, I'm guessing your use is incorrect.

    So, it might look like this:
    Code:
    void func1(void)
    {
        FILE *fp;
        
        fp = fopen("myfile.txt", "w");
        /* Do some error checking */
        
        func2(fp, "this text");
        
        fclose(fp);
    }
    
    void func2(FILE *myfp, char Message[])
    {
        func3(myfp, Message);
    }
    
    void func3(FILE *anotherfp, char Message[])
    {
        fprintf(anotherfp, "%s", Message);
    }
    Without seeing more of your code its hard to tell what's wrong.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    17

    Wink It's ok now

    thanks hammer!!! :o)

    and well, as usual i found out the problem is my compiler settings :o(

    i had a file with the same name of the one i'm using as output, and it was always 0... then i started changing everything for it :o/ I have just found out that my turbo C it's saving the files in it's home directory... (as you told me i was right at my first attempt i started to look for a compiler problem:oP) and well, now if you can help me with this question i would be VERY HAPPY (although i know perhaps i should ask it in the DOS board)... (I have messed up C concepts a lot of times just because my compiler doesn't work as it should and right code sometimes don't work :o/)

    I'm using Turbo C 2.01 for DOS (that i HATE but i need to use because i'm doing a homework for my college) and well, i have tried to find out how i can configure it but i didn't find how and where... my preprocessors directives like for example:

    Code:
    #define "list.h"
    don't work because it still look for the file in the include directory... as i have found out now it's looking for the files in the home directory too :o(

    do you know where and which parameters i must use to make it look for the files where my .c files are????

    sorry for bothering you and thanks a lot for your help :o)

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Never used it myself, but try reading through this lot.

    You might also lookup the -I switch on the compiler.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. Searching Binary Files for a Pattern
    By CaptainMorgan in forum C Programming
    Replies: 16
    Last Post: 06-17-2007, 07:04 PM
  3. Writing to single file in multiple functions
    By alvision in forum C Programming
    Replies: 12
    Last Post: 08-22-2004, 09:15 PM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21