-1

I'm trying to write my results to a text file so I can use MATLAB for plotting and etc. and the code has no problem (i tried with just showing the results with fprint), but now that i'm trying to save the resutls to a text file, C creates the file names amiir.txt, but it is empty. Even if I use relative directories, the text file will be empty. I tried to create a text file and then run the code again, but it is empty! whats wrong? I am using mac os and i tried both XCode and CodeLight! thanks. P.S.: Here is a piece of my code:

FILE * fp;    /* open the file for writing*/
fp = fopen ("/Users/amirsmacbookpro/Documents/Heat_Transfer/Project/Debug/amiir.txt","w+");

anf after some calculations:

for(i=0;i<Total_Nodes;i++)//Error
    {
        if(Temps_Diff[i]>Calculated_Error) Calculated_Error=Temps_Diff[i];
    }
    printf(fp,"\n%.4f",Calculated_Error);
   // printf(fp,"\n\nROW\tColumn\tTemp\n");

    for(i=0;i<=a/l;i++)//Showing results
    {
        for(j=0;j<=i;j++)
        {
            printf(fp,"%d\t%d\t%.6f\n",i,j,Temperatures[i][j]);

        }
    }
}
fclose(fp);
  • 4
    Your calls to `printf` should generate warnings when building. Did you not read those warnings? – Some programmer dude Dec 17 '17 at 18:14
  • 2
    And you really should [get a good beginners books or two](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) as that should have taught what about **f**printf. – Some programmer dude Dec 17 '17 at 18:16
  • Thanks for your comment, actually it is my first time working with C and i'm not used to its warnings. We usually use MATLAB for our research topics and MATLAB tells you exactly whats wrong. – Mohammad Amir Moghaddam Dec 17 '17 at 18:21
  • 1
    Well, C compilers also do that. Hitting a `-Wall -Werror` for example wont even compile your program. You are lucky that `fp` is a pointer type so it just generated a warning and not an error. The arguments of `printf` are `const char*, ...`. Now you would argue that why it is allowed then. That is because certain pointer conversions have to happen. For example certain functions accept `void*` therefore it cannot be treated as error(it is not so simple though). – Shiv Dec 17 '17 at 18:27
  • @user902384 man, isn't C awesome? I can now understand what has been going on beneath the MATLAB's interface! wow, I have to learn this beautiful language! thanks. – Mohammad Amir Moghaddam Dec 17 '17 at 19:28

1 Answers1

2

You need to use fprintf instead of printf. The printf function just prints to the console, not to a file.

Note that your compiler should be complaining (perhaps with a warning?) about your use of printf. The printf function does not take a function pointer as the first argument. You may need to turn up your compiler warning level to see this warning.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • @MohammadAmirMoghaddam If this is the "correct" answer and it solved your problem, you should *accept* it, by pressing the checkmark next to the answer. Please take some time to read [the help pages](http://stackoverflow.com/help), and [take the SO tour](http://stackoverflow.com/tour). – Some programmer dude Dec 17 '17 at 18:21