simple function

This is a discussion on simple function within the C Programming forums, part of the General Programming Boards category; Hello, Can someone show me whats the easiest way to write a function which will scan data (numbers - double ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    1

    Post simple function

    Hello,

    Can someone show me whats the easiest way to write a function which will scan data (numbers - double type) from "test.txt" and add it into array. Before number is written into array there must be a loop which detect if current number is in array, then skip to next number. So I dont want to have 2 same records in array.

    Thanks a lot for help!

  2. #2
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    Here's how it should work (but you have to write the code yourself!):

    1. open file
    2. start a counter
    3. for each line in file:
    3a. scan into a char buffer using fgets
    3b. use sscanf to extract the value from the string and assign to a temp value
    (alternatively you can use fscanf instead of fgets + sscanf)
    3c. iterate through all existing values in the array (compare with the temp value), and if it's already in there, continue on with the next line, else store that temp value to the array and increment the counter.
    4. Profit!

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I'd open the file in main(), then submit your FILE* to a function which returns the next number, and submit that to your addnumber function, which also checks for duplicates. You can make the array global or submit it to the second function. So in pseudo pseudo-code:
    Code:
    FILE *fp = fopen(....);
    int n;
    //loop
    n = getnumber(fp);
    addnumber(n);
    //endloop
    Probably you want to have a way to indicate EOF in the getnumber() func, either with a *ptr parameter or a special return value (such as a negative number if the numbers are always positive. Then you can tie that to your loop condition.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

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