-
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.
-
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.
-
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.
-
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.
-
>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.
-
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:");
}
-
>f = fopen("c:\\pkatest\test.txt", "w");
It looks like you're missing a backslash:
Code:
f = fopen("c:\\pkatest\\test.txt", "w");
-
-
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/
printct();
Code:
void printct()
{
FILE *f;
f = fopen("c:\\pkatest\\test.txt", "w");
fprintf(f, "Got Here:\nYes \nPlease Print!");
}
[/code]
-
> 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.
-
Thanks Salem, that solved the problem