fscanf()

Reads a file.

Synopsis

int fscanf (file f, string format, <type1> &var1 [,<type2> &var2...]);

Parameters

Parameter Description
f File to be read
format Format to be read
var1, var2, ... Target variables

Return value

In the event of errors, the function returns -1 or EOF otherwise, the number of read characters.

Error

If incorrect data types are used or if a file is not open.

Description

The function fscanf() reads under the specified format from the filefand saves the converted values in the following argumentsvar1,var2 and further. The format string can contain the following:

  • Blank spaces or tabs that are ignored.

  • Usual characters that must correspond to the next character after a space in the input stream (except from the percentage sign (%% is ignored and is interpreted as % )).

  • Conversion details that define the conversion of the next input field. They consist of a percentage sign, an optional number which defines the maximum field width and a conversion character.

An input field is a sequence of characters without "white spaces" such as blank spaces, tabulator, line feed). It either reaches to the next space character or until an expressly specified field width will be reached.The function fscanf() returns the number of converted and stored entries or in the event of errors or when the end of the file has been reached before the first conversion, EOF.

Table 1: Conversion characters for scanf commands.

Character Input data Data type
d Decimal integer int
u Unsigned integer uint
o Octal integer (with or without leading zero) int
x Hexadecimal integer (with or without leading 0x or 0X) int
l Prefix e.g. "%ld" or "%lu" for long and ulong values. long/ulong
s Series of characters without "white spaces" such as blank spaces, tabs and quotation marks. An end character ("/0") is appended to the string. string
e, f, g Floating point value. The input format allows for a sign, a sequence of characters that can also contain a decimal point and an exponent. This consists of E or e and an integer, optional with sign. float
[...] Stores in the argument the longest non-empty string of characters that are not within brackets but that are contained in the file in question (fscanf()) or the appropriate string (sscanf()). An end character ("/0") is appended to the string. For example, [ab] recognizes the longest string that consists of only "a"'s and "b"'s, [a-z,A-Z] the longest string of letters without umlauts and ß. string
[^...] Stores in the argument the longest non-empty string of characters that are not within brackets but that are contained in the file in question (fscanf()) or the [^...] appropriate string (sscanf()). An end character ("/0") is appended to the string. For example, [ab] recognizes the longest string that contains neither a´s nor b´s, [a-c,A-c] the longest string that contains the first three letters of the alphabet. string
% Recognizes %, no assignment

Example

The following example writes three float values into a text file and reads them from the file.

main()
{
  file f;
  int i;
  dyn_float value;
  value[1]=12.123;
  value[2]=8.0;
  value[3]=3.1;
  f=fopen("C:/TEMP/tfile.TXT","w+"); //creates a file for writing
  and reading
  for (i=1;i<=3;i++) fprintf(f,"%5.3f\n",value[i]);
  //writes the float values into the text file
  rewind(f); //back to the beginning of the file
  for (i=1;i<=3;i++)
  {
    fscanf (f,"%f",value[i]); //reads the float values from the
    file
    DebugN(value[i]);
  }
  fclose(f);//closes the file
}

Example

The following example writes the current day, month, year and a defined temperature into a text file and reads the data from the file.

main()
{
  file f;
  int err;
  time now = getCurrentTime(); //gets the current time
  float temp = -2.66; //Defines the temperature
  int tDay,tMonth,tYear;
  float tTemp;
  f=fopen("C:/TEMP/TEST.TXT","w"); //Creates a file for
  writing
  err=ferror(f); //searches for possible file errors
  fprintf(f,"Today is : %d %d %d, Temperature is %5.3f\n",
  day(now), month(now), year(now), temp); //writes "Today is" +
  the current date + "Temperature is" + the defined temperature
  -2.66 in to text file TEST.TXT
  fclose(f); //closes the text file
  f=fopen("C:/TEMP/TEST.TXT","r"); //opens the text file for
  reading
  fscanf(f,"Heute ist : %d %d %d, Temperatur %f",
  tDay,tMonth,tYear,tTemp);
  //reads the current date and the temperature from the text
  file
  DebugN(tDay,tMonth,tYear,tTemp);
  err=ferror(f);
  fclose(f);
}

The fscanf() is used like sprintf(). For more different examples of using fscanf(), see chapter sprintf().

Assignment

File functions

Availability

CTRL