Emptying Strings and fscanf

This is a discussion on Emptying Strings and fscanf within the C Programming forums, part of the General Programming Boards category; Hi! For my first question, how do I empty a string? I want to remove all its elements since I ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    Emptying Strings and fscanf

    Hi! For my first question, how do I empty a string? I want to remove all its elements since I will be reusing the string. We're required to create a program that will make the user input a string and then it should have the option of deleting the contents of the string.

    For my second problem, let's say that I have a text file named trial.txt with this written:

    john
    15

    sam
    17

    joe
    19

    I want to print just the numbers below the names. In my program, I'm currently looping this:
    Code:
    fscanf(fp,"\n%d\n\n",age);
    printf("%d\n",age);
    It's supposed to print the ages one by one but all it prints are zeroes. How do I fix this?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    1) set the first element to '\0', eg. str[0] = '\0'

    2) you can't skip data with fscanf, but you can discard it using * (like, %*s will discard a string).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Fixed it! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using FSCANF to read mixed text/double files
    By cfdprogrammer in forum C Programming
    Replies: 3
    Last Post: 03-23-2009, 04:18 AM
  2. read mix file: fscanf fgets
    By cfdprogrammer in forum C Programming
    Replies: 1
    Last Post: 03-20-2009, 12:38 PM
  3. fscanf Issue
    By Maser in forum C Programming
    Replies: 3
    Last Post: 04-23-2007, 08:42 PM
  4. fscanf's auto-allocation for strings
    By @nthony in forum C Programming
    Replies: 12
    Last Post: 07-03-2006, 07:47 AM
  5. Inputting strings from a file
    By Mystic_Skies in forum C Programming
    Replies: 7
    Last Post: 11-17-2004, 04:08 PM

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