using fgets to store something into an array silently?

This is a discussion on using fgets to store something into an array silently? within the C Programming forums, part of the General Programming Boards category; I have the following piece of code: Code: void getString(char array[], int size) { printf("Please enter some text"); fgets(array, size, ...

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    using fgets to store something into an array silently?

    I have the following piece of code:

    Code:
    void getString(char array[], int size)
    {
       printf("Please enter some text");
       fgets(array, size, stdin);
    }
    I have a few other methods, arrayToLinkList and printLinkList. Basically when i pass the array to these methods it copies it into a linked list and another method prints it out.

    My question is i have a file, i can print out the contents, i know how many lines are in it so reading the file and having a reference to it is no problem. How exactly do i extract the contents and store it in an array using fgets? Whenever i modify the above method it keeps asking for input.
    Last edited by Axel; 10-15-2005 at 08:15 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Replace stdin with the handle of the file you opened.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Thanks seems to work slightly. Just one more question if i want to selectively select text that is in the text file i.e.:

    Customer Name | Total Earnings

    John | 50,000

    and put it in my link list element i.e. char[] customerName (which is in a struct) how would i do that? At the moment it gets everything from the text file which isn't want i was planning to do.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Sure, you get each line from the file using fgets()
    Then you chop up the line using strtok, strcmp, strstr, sscanf or whaever else you like to extract the bits of information you want to keep.
    Then you copy those bits to wherever you want to store, say your array, list, tree or whatever.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  5. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM

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