Thread: Scan for a specific data

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Philippines
    Posts
    20

    Scan for a specific data

    Hello. I created a program that will create a file and print a list on it.
    Here is the code for my program:
    Code:
    int controlvalue(void) {
    FILE *controlvalue; controlvalue = fopen("something.txt", "a+"); /* Series of fprintfs */ fclose (controlvalue); return 1;
    }
    Here is an example list created by my program:
    Code:
    [Gifts]
    Candy=45 Chocolate=32 Toy=128 Robot=754 Doll=1492 Star=21 Phone=72 Skateboard=87 Frame=314 Days=365 Perfume=421
    I want to get the value of "Skateboard" on file. So I need to read 9 lines. On the 9th line, the gets() loop will stop. But, what if I only want to get the value of "Skateboard" as integer (87) and not a string? Also, is it possible to scan the value of Skateboard if it's located on a different (or unknown) line?
    Thanks in advance!
    Last edited by Bryan_James; 12-13-2014 at 08:24 AM. Reason: minor edit

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Use fgets() to read each line

    Use strstr() or strncmp() to work out if the line contains "Stateboard="

    Use sscanf to extract the info you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Output specific data from 2 txt files
    By plat in forum C++ Programming
    Replies: 1
    Last Post: 10-12-2012, 07:36 PM
  2. Read text file, scan and store data into variables?
    By wisdom30 in forum C Programming
    Replies: 8
    Last Post: 04-18-2011, 11:23 PM
  3. Getting Specific Data from a file
    By darknite135 in forum C++ Programming
    Replies: 4
    Last Post: 12-31-2007, 11:41 PM
  4. Entering specific data
    By fantim in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 01:47 PM
  5. Scan data from file
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-12-2002, 07:52 AM