Thread: Using fcanf to get parts of a text file

  1. #1
    Registered User
    Join Date
    Oct 2012

    Using fcanf to get parts of a text file

    So I have a .txt file that is declared as fin, and I need to use fscanf to designate parts of the file to certain variables.

    The file looks like this:

    Farley: 46.5/7.1
    L. J. Cool: -39.2/27.25

    I need to create an fscanf statement that will read a single line, storing the name (including the space if present, but not the colons) in a character array name, and the two floats into the already declared variables x and y.

    How would I pull thiss off?

    I tried:
    fscanf(fin,"%s %f %f", name, x, y);
    But the exam proctor told me I was wrong without any please.

  2. #2
    Registered User
    Join Date
    May 2012
    Maybe this can help you.
    It's for scanf, but they will work for fscanf too.

    I don't know if this is the best solution, though.

    Edit: And you can also consider using the suggestion at the end of the linked page.
    Last edited by koplersky; 12-10-2012 at 09:35 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Firstly fscanf() takes pointers and fills them.
    Secondly you always should check the return value. This tells you how many fields were successfully converted.

    Thirdly, I would start with a line like

    singlestring 1.5/2.5

    get that working. Then look at the scanf() documentation as see if you can work out how to use a colon instead of a space as the field separator for the string.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 11-28-2011, 02:40 PM
  2. bad fcanf() format
    By mirekgn in forum C Programming
    Replies: 8
    Last Post: 04-10-2010, 03:53 AM
  3. Copy defined parts from text-file
    By tomas.ra in forum C Programming
    Replies: 5
    Last Post: 02-19-2006, 01:44 PM
  4. Overwriting Parts of a File
    By explosive in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2005, 09:19 AM
  5. reading certain parts of text files
    By Captain Penguin in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2002, 09:45 AM