reading file into 2 dimensional array, scanning array for 1 dimension?

This is a discussion on reading file into 2 dimensional array, scanning array for 1 dimension? within the C Programming forums, part of the General Programming Boards category; Hello, I'm super new to c programming. Having alittle trouble with something. I'm trying to read a file into a ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    1

    reading file into 2 dimensional array, scanning array for 1 dimension?

    Hello, I'm super new to c programming. Having alittle trouble with something.
    I'm trying to read a file into a 2 dimensional array. contents of the file are as such :
    coderoduct
    231231:canofsoop
    231231izza
    213124op
    list does not exceed 1000.
    I have a variable from another function (the name of product). I need to scan the array for the name and if it exist get the code#.
    this is what i have so far
    Code:
    void Search(char product, int *code) {
    char &type, char productname[1000], int codenumber[1000], int i;
    FILE *fp;
    fp = fopen("product.dat", "r");
    for (i = 0; i < 1000; i++)
    fscanf(fp, "%d:%c\n", codenumber[i], productname[i]);}
    ????
    ???
    rewind(fp);
    fclose(fp); }
    &type is the address of name of product from another function.
    after i scan the file i'm lost >.<
    how do i scan the productnames for the &type and get the code??
    any help at all will be much appreciated!
    im so confuseled!

  2. #2
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    &type stinks of C++.

    First of all fclose(fp) is in your loop, while fopen is outside, this will result in unwanted behavior. Also productname[i] is only a single character, you want it to be a whole string correct? Here is a simple reworking of your code:
    Code:
    int Search(char *product)
    {
        int code;
        char productname[1000];
        char codenumber[1000];
    
        open your file
    
        while(fgets(productname, 1000, fp) != NULL)
        {
            use strtok or scanf here to split your string into a product code and a name
            code = atoi(codenumber);
    
            /* do the two strings match? */
            if (strcmp(productname, product) == 0)
            {
                close your file
                return the product code here
            }
        }
    
        close yoru file
    
        /* at this point all products have been searched but no matches found */
        return an error code
    }
    That basic idea should work, although I'm pretty damn tired right now so you might run into a minor error.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 12-08-2008, 09:27 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Reading from a file into an array
    By fmsguy06 in forum C Programming
    Replies: 6
    Last Post: 10-18-2008, 09:25 PM
  4. Reading all the numbes from a file and storing in an array
    By derek tims in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2006, 03:01 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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