Thread: Problem Printing to File

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    80

    Problem Printing to File

    This is probably pretty easy, but I've been having a hard time and any help is greatly appreciated.

    I have a function named pkaden that needs to call a print function named
    printct. I'm not sure how to set up/pass the filename to printct:

    printct:
    Code:
    void printct(FILE *f, MOLECULE *m)
    
    {
       int i, j;
    
    
       fprintf(f,"N_Atoms:%3i   N_Bonds:%3i   N_Hydro:%3i   N_Charge:%3i\n",
               m->N_Atoms, m->N_Bonds, m->N_Hydro, m->Net_Charge);
    
       fprintf(f,"ATOMS:");
       for (i = 0; i < m->N_Atoms; i++)
          fprintf(f,"%4i", i);
       fprintf(f,"\nKIND :");
       for (i = 0; i < m->N_Atoms; i++)
          fprintf(f, "%4i", m->Atom[i].Atom_Kind);
       fprintf(f,"\nDEG  :");
       for (i = 0; i < m->N_Atoms; i++)
          fprintf(f, "%4i", m->Atom[i].Degree);
       fprintf(f,"\nCHRG :");
       for (i = 0; i < m->N_Atoms; i++)
          fprintf(f, "%4i", m->Atom[i].Charge);
       fprintf(f,"\nVAL  :");
       for (i = 0; i < m->N_Atoms; i++)
          fprintf(f, "%4i", m->Atom[i].Valence);
       fprintf(f,"\nHYDRO:");
       for (i = 0; i < m->N_Atoms; i++)
          fprintf(f, "%4i", m->Atom[i].Hydros+m->Atom[i].Qual_H);
       fprintf(f,"\nNAROM:");
       for (i = 0; i < m->N_Atoms; i++)
          fprintf(f,"%4i", m->Atom[i].N_Aroma);
       fprintf(f,"\nNQUAL:");
       for (i = 0; i < m->N_Atoms; i++)
          fprintf(f, "%4i", m->Atom[i].N_Qual);
       fprintf(f,"\nNATYP:");
       for (i = 0; i < m->N_Atoms; i++)
          fprintf(f, "%4i", m->Atom[i].N_ATypes);
       fprintf(f,"\nNeighbor Info\n");
       for (i = 0; i < m->N_Atoms; i++) {
          fprintf(f, "Atom:%3i\n--------\nQUAL#:", i);
          for (j = 0; j < max(m->Atom[i].N_Qual,m->Atom[i].N_ATypes); j++)
             fprintf(f,"%4i", j);
          fprintf(f,"\nQUAL :");
          for (j = 0; j < m->Atom[i].N_Qual; j++)
             fprintf(f,"%4i", m->Atom[i].Qual[j]);
          fprintf(f,"\nAQUAL:");
          for (j = 0; j < m->Atom[i].N_ATypes; j++)
             fprintf(f,"%4i", m->Atom[i].A_List[j]);
          fprintf(f, "\nNABOR:");
          for (j = 0; j < m->Atom[i].Degree; j++)
             fprintf(f,"%4i", j);
          fprintf(f,"\nWHO  :");
          for (j = 0; j < m->Atom[i].Degree; j++)
             fprintf(f,"%4i", m->Atom[i].Neighbor[j].Nabor_Num);
          fprintf(f, "\nBNDTY:");
          for (j = 0; j < m->Atom[i].Degree; j++)
             fprintf(f, "%4i", m->Atom[i].Neighbor[j].Bond_Kind);
          fprintf(f, "\nBND# :");
          for (j = 0; j < m->Atom[i].Degree; j++)
             fprintf(f, "%4i", m->Atom[i].Neighbor[j].B_Number);
          fprintf(f,"\n");
       }
       fprintf(f,"\n======================================================\n");
    }
    I've tried different ways to set up a filename and pass this from pkaden, but
    I am not able to get it the code to compile.

  2. #2
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    That's very messy, with poor indentation. Also, this should be in the 'C Programming' forum not 'C++ Programming'. Don't make a new thread, though.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Sorry about posting in the wrong area. The function that prints to the file was written by someone else in C. I saved it as a cpp file and I'm trying to call it from a C++ function.

    I won't make a new thread and thanks for getting back to me.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Well, it's pretty simple: you create a variable of type FILE * in pkaden, initialize it with fopen() and pass it to printct() as the first argument.
    Last edited by zx-1; 09-25-2006 at 03:42 PM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >I'm not sure how to set up/pass the filename to printct:
    If filename is a char array, then you could do it like this:
    Code:
    void printct(char *filename, MOLECULE *m)
    {
       //fopen file
    Then in function pkaden:
    Code:
    char filename[] = "name_of_file.ext";
    
    printct( filename, m );
    If filename is a C++ style string, then replace char * above with string.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Thanks for your reply.

    Its compiling, but not writing anything. Right now, I'm just trying to get any type of output, but it errors when I try running it:

    pkaden:
    Code:
    void printct(FILE *f);
    FILE *f;
    f = fopen("c:\\pkatest\test.txt", "w");
    printct(f);
    promtct:
    Code:
    void printct(FILE *f)
    {
      fprintf(f, "Got Here:");
    }

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >f = fopen("c:\\pkatest\test.txt", "w");
    It looks like you're missing a backslash:
    Code:
    f = fopen("c:\\pkatest\\test.txt", "w");

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Well what errors?
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Thanks for all of your help. I'm not getting any errors now, but its not writing anything to the file. My program must be finding it though because when I try to delete c:\\pkatest\test.txt, Windows won't let me because its being used by another program.

    I've simplified the code down as far as I could and changed my code to the following:

    pkadan/
    Code:
    printct();
    printct();
    Code:
    void printct()
    {
    FILE *f;
    f = fopen("c:\\pkatest\\test.txt", "w");
    fprintf(f, "Got Here:\nYes \nPlease Print!");
    }
    [/code]

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > f = fopen("c:\\pkatest\\test.txt", "w");
    Check
    if ( f != NULL )
    before trying to write to the file.

    And fclose(f) when you're done.
    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.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    80
    Thanks Salem, that solved the problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Problem reading file
    By coder_009 in forum C Programming
    Replies: 10
    Last Post: 01-15-2008, 01:22 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM