Using fwrite with a text output

This is a discussion on Using fwrite with a text output within the C Programming forums, part of the General Programming Boards category; Here is my question: I have the following code: Code: void Plot(int nx, int ny, double x[350], double y[350], double ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Using fwrite with a text output

    Here is my question:

    I have the following code:

    Code:
     void Plot(int nx, int ny, double x[350], double y[350], double C_new[350][350], int iplot){
        int i, j;
        char fname[50];
        FILE *fp;
        sprintf(fname,"Unsteady Advection Upwind Without Diffusion_%d.PLT",iplot);
        fp=fopen(fname, "wb");
            fprintf(fp, "TITLE= Unsteady Advection Upwind Without Diffusion_%d\n", iplot);
            fprintf(fp, "VARIABLES=\"X\" \"Y\" \"C\" \n");
            fprintf(fp, "ZONE I=%d, ", nx);
            fprintf(fp, " J=%d, \n", ny);
            fprintf(fp, "DATAPACKING=POINT \n");
            for(i=1; i<=nx; i++){
                for(j=1; j<=ny; j++){
                 fprintf(fp, "%f %f %f\n", x[i], y[j], C_new[i][j]);
                }}
        fclose(fp);
    
        }
    I am then taking the generated file and importing it into a plotting program (Techplot360).

    It would speed up the importing process if I created a binary file instead of a like above.

    I know that I need to use fwrite but I am unsure how to handle lines like
    Code:
    fprintf(fp, "TITLE= ABCDEFG_%d\n", iplot);
    and

    Code:
    fprintf(fp, "%f %f %f\n", x[i], y[j], T_new[i][j]);
    Any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    I though this would work but it is not giving the data from x, y, or T. My blank line returns do not work.

    Code:
    /*Define plot Function*/
        void Plot(int nx, int ny, double x[350], double y[350], double  double T_new[350][350], int iplot){
        char fname[50];
        char title[100];
        char var[100];
        char zone1[100];
        char zone2[100];
        char blank[5];
        sprintf(title,"TITLE= Unsteady Advection Upwind Without Diffusion_%d", iplot);
        sprintf(var, "VARIABLES=\"X\" \"Y\" \"C\"");
        sprintf(zone1, "ZONE I=%d, ", nx);
        sprintf(zone2, " J=%d, ", ny);
        sprintf(blank, "\n");
    
        FILE *fp;
        sprintf(fname,"Unsteady Advection Upwind Without Diffusion_%d.PLT",iplot);
        fp=fopen(fname, "wb");
            fwrite(title, sizeof(title[0]),sizeof(title)/sizeof(title[0]),fp);
            fwrite(blank,sizeof(blank[0]),sizeof(blank)/sizeof(blank[0]),fp);
            fwrite(var, sizeof(var[0]),sizeof(var)/sizeof(var[0]),fp);
            fwrite(blank,sizeof(blank[0]),sizeof(blank)/sizeof(blank[0]),fp);
            fwrite(zone1, sizeof(zone1[0]),sizeof(zone1)/sizeof(zone1[0]),fp);
            fwrite(blank,sizeof(blank[0]),sizeof(blank)/sizeof(blank[0]),fp);
            fwrite(zone2, sizeof(zone2[0]),sizeof(zone2)/sizeof(zone2[0]),fp);
            fwrite(blank,sizeof(blank[0]),sizeof(blank)/sizeof(blank[0]),fp);
            fwrite(x,sizeof(x[0]),sizeof(x)/sizeof(x[0]),fp);
            fwrite(blank,sizeof(blank[0]),sizeof(blank)/sizeof(blank[0]),fp);
            fwrite(y,sizeof(y[0]),sizeof(y)/sizeof(y[0]),fp);
            fwrite(blank,sizeof(blank[0]),sizeof(blank)/sizeof(blank[0]),fp);
            fwrite(T_new,sizeof(T_new[0][0]),sizeof(T_new)/sizeof(T_new[0][0]),fp);
        fclose(fp);
        }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Read two paragraphs from Binary File I/O, (middle of the page), and give it a try.
    C File I/O Tutorial - Cprogramming.com

    It's best not to mix binary and text I/O, imo. Use one or the other, and of course, binary files should always be just a bit smaller and with no newline translation to make, just a bit faster, too.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > I am then taking the generated file and importing it into a plotting program (Techplot360).

    > It would speed up the importing process if I created a binary file instead of a like above.
    Does Techplot360 actually support the reading of two different file formats (one text, one binary)?

    If it does, then you need to READ the file format specification for the binary format instead of making guesses.

    FWIW, this is largely a pointless optimisation. File I/O is so slow compared to processor speeds that the processor can easily perform any "text to int/float" conversions long before the disk has managed to load the next sector of the file from disk.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > I am then taking the generated file and importing it into a plotting program (Techplot360).

    > It would speed up the importing process if I created a binary file instead of a like above.
    Does Techplot360 actually support the reading of two different file formats (one text, one binary)?

    If it does, then you need to READ the file format specification for the binary format instead of making guesses.

    FWIW, this is largely a pointless optimisation. File I/O is so slow compared to processor speeds that the processor can easily perform any "text to int/float" conversions long before the disk has managed to load the next sector of the file from disk.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    333
    You need to define a binary format. Binary format files can contain embedded ascii strings. You just have to be careful to specify how they are terminated (fixed width, newline, nul, newline + nul). To write a string fwrite(str, strlen(str)+1, 1, fp);

    You also have to be careful about whether your integers are big endian or little endian. Floating point numbers are a whole different issue, and too complicated to discuss here, for now just fread and fwrite in the machine format.

    A binary file is a lot faster and easier to parse than a text file, but it's not human readable.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,778
    My blank line returns do not work.
    One of the differences between a binary file and a text file is the handling of the end of line character(s). With text files, using the formatted output methods like fprintf() the end of line character is converted to the proper values, depending on which operating system you happen to be using. The fwrite() method doesn't necessarily do this conversion.

    A binary file is a lot faster and easier to parse than a text file, but it's not human readable.
    Reading a binary file is only a lot faster if you are writing/reading a large buffer. If you are reading/writing individual variables there will be little if any actual speed advantage. In my opinion a binary file is no easier to parse than a text file, both have advantages and disadvantages. You have already mentioned one of the disadvantages of binary files.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to increase text in the output
    By ferronia in forum Windows Programming
    Replies: 4
    Last Post: 03-05-2013, 08:52 AM
  2. text output?
    By jorel43 in forum C++ Programming
    Replies: 7
    Last Post: 07-14-2010, 11:31 AM
  3. Text File Output
    By FJ8II in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2007, 05:03 AM
  4. text output in text editor
    By realjag in forum C++ Programming
    Replies: 5
    Last Post: 10-29-2005, 06:51 PM
  5. text output almost works
    By Shadow12345 in forum C++ Programming
    Replies: 6
    Last Post: 04-18-2002, 11:04 AM

Tags for this Thread


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