Like Tree1Likes
  • 1 Post By koplersky

Using fcanf to get parts of a text file

This is a discussion on Using fcanf to get parts of a text file within the C Programming forums, part of the General Programming Boards category; So I have a .txt file that is declared as fin, and I need to use fscanf to designate parts ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    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 reason...help please.

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    52
    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 08:35 PM.
    AndiPersti likes this.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    333
    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.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 11-28-2011, 01: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, 12:44 PM
  4. Overwriting Parts of a File
    By explosive in forum C++ Programming
    Replies: 3
    Last Post: 01-25-2005, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21