C programing help with file precessing

This is a discussion on C programing help with file precessing within the C Programming forums, part of the General Programming Boards category; Hello, I have a test file which looks like following ------------------------------------------------- file has fruits :: apple orange banana | 23 ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    C programing help with file precessing

    Hello,
    I have a test file which looks like following

    -------------------------------------------------
    file has fruits :: apple orange banana |
    23 16 7 |
    -------------------------------------------------
    means it has 23 apples, 16 oranges and 7 banana. What I want is to,
    store the fruit names and their respective numbers in a matrix or fruit names in an 1D array and numbers in another 1-D array so that I can pull particular info when ever I need.

    Can you guys help or at least suggest how should I proceed?

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Hey,

    in my opinion:

    1) make a file

    Code:
    FILE *pfile;
    pfile = fopen("file.txt","w");
    2) make an 2-dim Array (using malloc)

    3) access the file and store the information into the array.

    I did not wrote any details but i you want further help tell me...

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Brack,
    I took long time to reply because I was rehearsing my C and was trying to do myself. The main problem I am running into is how to choose some particular portions of a the string and make a matrix out of it. What I mean is I want to make a matrix like

    apple 23
    banana 16
    orange 7

    but notice apple, banana and orange are embedded in the first line, so how to extract those from the whole string "file has fruits :: apple orange banana".

    Thanks.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Quote Originally Posted by sourabhsinha View Post
    What I want is to,
    store the fruit names and their respective numbers in a matrix
    Hash table - Wikipedia, the free encyclopedia

    or fruit names in an 1D array and numbers in another 1-D array so that I can pull particular info when ever I need.
    Parallel array - Wikipedia, the free encyclopedia

    Can you guys help or at least suggest how should I proceed?

    Thanks
    fgets() and sscanf() together work for a majority of simple files.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Depending how many items need processing, the best way might be to make a small struct...

    Code:
    struct Fruit
      { char Name[24];  // name of fruit
         int   Count;         // how many
       } 
    
    Fruit Bowl[100];
    Read the names on the first pass of the file
    Read the numbers on the second

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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