PDA

View Full Version : fscanf()



ImpactDNI
11-17-2002, 06:14 PM
I want to search an ASE file for certain words, But i'm not all that great at C++, importing and reading from files, so i need a little help with the basics.
Can anyone explain how fscanf() works to me? in SIMPLE words... I looked it up on cplusplus.com but thats way too hard for me to understand. I'm not that familiar with classes or objects. I'm just going to be working with good old fashioned matricies...
Thanks a lot.

mikael_aronsson
11-17-2002, 11:25 PM
Hi !

Sorry, I couldn't find fscanf in the OpenGL docs... http://www.opengl.org/discussion_boards/ubb/confused.gif

Anyway, have a look at: http://cplus.about.com/library/weekly/aa032302b.htm

And if that doesn't help, search for "fscanf tutorial" on google.

Mikael

nexusone
11-18-2002, 05:34 AM
First you have to open the file, before you can use the fscanf function. But fscanf uses the same format as printf in defining the order of data read in.

data file:
1, one
2, two
3, three
eof


eof = fscanf( file, "%d,%s \n", number, string);
you would keep calling fscanf until end of file is reached. eof = 0 no more data or data format not correct.


Help file on fscanf:

#include <stdio.h>
int fscanf(FILE * restrict stream,const char * restrict format, ...);

Description
The fscanf function reads input from the stream pointed to by stream, under control of the string pointed to by format that specifies the admissible input sequences and how they are to be converted for assignment, using subsequent arguments as pointers to the objects to receive the converted input. If there are insufficient arguments for the format, the behavior is undefined. If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored.

The format shall be a multibyte character sequence, beginning and ending in its initial shift state. The format is composed of zero or more directives: one or more white-space characters, an ordinary multibyte character (neither % nor a white-space character), or a conversion specification. Each conversion specification is introduced by the character %.

After the %, the following appear in sequence:
An optional assignment-suppressing character *.
An optional nonzero decimal integer that specifies the maximum field width (in characters).

An optional length modifier that specifies the size of the receiving object.
Aconversion specifier character that specifies the type of conversion to be applied.

The fscanf function executes each directive of the format in turn. If a directive fails, as detailed below, the function returns. Failures are described as input failures (due to the occurrence of an encoding error or the unavailability of input characters), or matching failures (due to inappropriate input).

A directive composed of white-space character(s) is executed by reading input up to the first non-white-space character (which remains unread), or until no more characters can be read.

A directive that is an ordinary multibyte character is executed by reading the next characters of the stream. If any of those characters differ from the ones composing the directive, the directive fails and the differing and subsequent characters remain unread.

A directive that is a conversion specification defines a set of matching input sequences, as described below for each specifier. A conversion specification is executed in the

following steps:
Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or nspecifier.225)

An input item is read from the stream, unless the specification includes an n specifier. An input item is defined as the longest sequence of input characters which does not exceed any specified field width and which is, or is a prefix of, a matching input sequence. The first character, if any, after the input item remains unread. If the length of the input item is zero, the execution of the directive fails; this condition is a matching failure unless end-of- file, an encoding error, or a read error prevented input from the stream, in which case it is an input failure.

Except in the case of a % specifier, the input item (or, in the case of a %n directive, the count of input characters) is converted to a type appropriate to the conversion specifier. If the input item is not a matching sequence, the execution of the directive fails: this condition is a matching failure. Unless assignment suppression was indicated by a *, the result of the conversion is placed in the object pointed to by the first argument following the format argument that has not already received a conversion result. If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined.

The length modifiers and their meanings are the same as those of fprintf.

Returns
The fscanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.


Originally posted by ImpactDNI:
I want to search an ASE file for certain words, But i'm not all that great at C++, importing and reading from files, so i need a little help with the basics.
Can anyone explain how fscanf() works to me? in SIMPLE words... I looked it up on cplusplus.com but thats way too hard for me to understand. I'm not that familiar with classes or objects. I'm just going to be working with good old fashioned matricies...
Thanks a lot.