CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Fluent UDF and Scheme Programming (https://www.cfd-online.com/Forums/fluent-udf/)
-   -   HOW CAN I USE "ftell" ???? (https://www.cfd-online.com/Forums/fluent-udf/70147-how-can-i-use-ftell.html)

enry November 16, 2009 05:24

HOW CAN I USE "ftell" ????
 
Hi everybody,
I wanna know the end position of a file. So I write this routine:



#include "udf.h"

DEFINE_ON_DEMAND(example)
{
FILE *fp;
long len;

fp=fopen("moment.txt","rb");
if(fp == NULL)
printf("ERROR!!!!");

len=ftell(fp); /*get position at end (length)*/
fclose(fp);
}




Fluent report:

error: ftell : undeclared variable.




WHY ?!?! CAN ANYBODY HELP ME?!
thanks.

coglione November 16, 2009 10:18

If you add
#include <stdio.h>
does this help?

cheers

enry November 16, 2009 10:31

I have to read the number in a file .txt; the file is like the following:


12.3
43.2
123.4
122.3



HOW CAN I READ THE NUMBER TILL THE END?

ps. In Fluent you have to include only the file header udf.h : #include "udf.h"
pps. sei italiano?

coglione November 16, 2009 11:05

hi enrico,

as far as i know "udf.h" will include some standard IO-functions, but not the more sophisticated ones. Try to include "std.io" and see what happens.
Sorry i am not Italian, my synonym originates from an Italian girl i once knew, but something went wrong as you may perceive easily.

cheers

dmoroian November 25, 2009 08:27

Quote:

Originally Posted by enry (Post 236459)
I have to read the number in a file .txt; the file is like the following:


12.3
43.2
123.4
122.3



HOW CAN I READ THE NUMBER TILL THE END?

ps. In Fluent you have to include only the file header udf.h : #include "udf.h"
pps. sei italiano?

Hi Enrico,
The following code should do the trick:
Code:

#include "udf.h"

DEFINE_ON_DEMAND(example)
{
#if !PARALLEL
  FILE *fp;
  real bubu;

  fp=fopen("moment.txt","r");
  if(fp == NULL)
      Message("ERROR!!!!\n");

  while(!feof(pf))
  {
      fscanf(pf,"%g",&bubu);
  }

  fclose(fp);
#endif
}

Note: it should be run in serial.

enry November 26, 2009 05:16

Thanks a lot for your reply.
Do you know how can I read only the last number on a .txt file?

dmoroian November 26, 2009 05:21

It reads the last number
 
:) Well, my example this is what actually does: it reads the last number from the file. It reads all of them, but only the last remains after it exits the "while" loop.

enry November 26, 2009 06:01

Oh yes! of course!!! ;)

I compile the following text on fluent, but it reports an error:



#include "udf.h"

DEFINE_ON_DEMAND(example)
{
#if !PARALLEL
FILE *fp;
double bubu;

fp=fopen("moment.txt","r");
if(fp == NULL)
Message("ERROR!!!!\n");

while(!feof(fp))
{
fscanf(fp,"%lf",&bubu);
}

fclose(fp);
#endif

}


Error: example.c: line 13: feof: undeclared variable

I compile it as INTERPRETED. I try also as COMPILED, but it doesn't work.
Do you know if I have to add some header files when I use FEOF ?
An other question: what's the meaning of #if !PARALLEL ?

Thank in advance.

dmoroian November 26, 2009 06:15

Hi Enrico,
The function feof() is declared in the header stdio.h, which I think is already included by udf.h, but just in case add it:
Code:

#define "udf.h"
#define<stdio.h>

The compiler directive
Code:

#if !PARALLEL ...#endif
specifies that the code works only serial (not parallel).

enry November 26, 2009 06:46

Thanks a lot for your quikly replies!
IT WORKS (whitout including stdio.h ), but only as compiled!

Thanks! ;)


All times are GMT -4. The time now is 00:48.