Thread: file to arrays and search

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    3

    Red face file to arrays and search

    Hi everyone!

    i got problem in this function


    Code:
    void DisplayItemInfoForAParticularItem(){	FILE* f = fopen("items.txt", "r");
        int ItemID,ItemQuantity,PricePerUnit, i = 0;
        int ItemI[10], ItemQ[10],Price[10];
        printf("Enter itemID: ");
    	scanf("%d",&ItemID);
        while( fscanf(f, "%d,", &ItemID) != EOF ) 
        {
            scanf("%d", &ItemI[i]);
    		i++;
        }
        while( fscanf(f, "\t%d,", &ItemQuantity) != EOF ) 
        {
            scanf("%d", &ItemQ[i]);
    		i++;
        }
         while( fscanf(f, "\t\t%d,", &PricePerUnit) != EOF ) 
        {
            scanf("%d", &Price[i]);
    		i++;
        }
    	
    	if(ItemID != ItemQ[10])
    	printf("Error");
    	
        fclose(f);
       return ;
    }
    Where is the problem ... thanks


    and this picture explain the function

    file to arrays and search-15121616111-png
    Last edited by Aymen1saleh; 12-08-2017 at 01:11 PM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You need a single loop, not three separate ones.

    Keep things simple. Read a whole line from the file by using fgets, and parse it with sscanf.

    If the item's ID is the one you want, display it. If not, move to the next.

    If you have no more items to read and no items were displayed, output the error.

    You don't need arrays at all here, just three variables for ID, Quantity and PricePerUnit.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    3
    Quote Originally Posted by GReaper View Post
    You need a single loop, not three separate ones.

    Keep things simple. Read a whole line from the file by using fgets, and parse it with sscanf.

    If the item's ID is the one you want, display it. If not, move to the next.

    If you have no more items to read and no items were displayed, output the error.

    You don't need arrays at all here, just three variables for ID, Quantity and PricePerUnit.

    Thanks ,,

    But i stopped here,, what i can do next ?
    Code:
    void DisplayItemInfoForAParticularItem(){	FILE* f = fopen("items.txt", "r");
        int ItemID,ItemQuantity,PricePerUnit, i = 0,j;
        int ItemI[10], ItemQ[10],Price[10];
        printf("Enter itemID: ");
    	scanf("%d",&ItemID);
        while(fscanf(f,"%d%d%d",ItemI[i],ItemQ[i],Price[i]) != EOF){
        	++i;
    	}
    	for(j=0;j<10;j++){
    		ItemI[j] = ItemID;
    		break;
    	}
    	
    	
        fclose(f);
       return ;
    }

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    fscanf does not work off of EOF it returns what it gets,
    Code:
    while ( fscanf (stream, "%d%d%d", stuff1,stuff2,stuff3) == 3 ) { do stuff }
    why didn't you make a function to pass your file ( stream ) into your function .. it is not exactly you per se' , But I see that a lot, doing everything in the function.it's what I'd call bad program design, and wonder what they are teaching you and the others that pay money to get taught this. When it looks to be no program design is even being introduced into the classes the ones in here are taking.

    anyways, just a quick short example
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct MyStrutter
    {
        char *N;
        int id;
    }mystrutter;
     
    int MyFunction1(FILE *f, char arrayA[])  
    {
        int i = 0;
        char buffer;
      
        while ( (buffer = fgetc(f)) != EOF )
        {
            arrayA[i] = buffer;
            i++;
        }    
        return i;
    }
    
    void MyFunction2(mystrutter strut1[], FILE *f)   
    {
        int i = 0;
         char name[30];
        memset(name, 0, sizeof(name));
    
        while ( fscanf (f, "%s%d",( strut1[i].N = strdup(name)), &strut1[i].id ) == 2) 
            i++;
    }
    
    int main (int argc, const char **argv)
    {
        if ( argc < 2)
        {
            printf("no file yo %s\n", argv[1]);
            return -1;
        }
        //Open file once in main, then pass it
        //to functions to use it.
        FILE *fp;
        if ( (fp = fopen(argv[1], "r")) == NULL)
        {
            printf("FILE NULL\n");
            return -1;
        }
        int size = 0;
        char arr[100];
        mystrutter strutter1[3];
        
        memset(arr,0,sizeof(arr));
        memset(strutter1,0,sizeof(mystrutter));
        
        //returns amount of elements it 
        //actually used.
        size = MyFunction1(fp,arr);
        
        //use that same amount in the loop
        for ( int i = 0; i < size; i++)
            printf("%c",arr[i]);
        
        //reset stream
        fseek(fp,0,SEEK_SET);
        
        printf("\n");
        //use same stream in a different function
        MyFunction2(strutter1, fp);
         //is not an int so used size_t because of method of getting
         //size of array. 
        for ( size_t i = 0; i <  sizeof(strutter1)/sizeof(strutter1[0]); i++)
            printf("%s %d\n",strutter1[i].N, strutter1[i].id);
          
        
        fclose(fp);
    return 0;
    }
    // file setup like this
    (char) Name {where name is more than one letter}  (int) Id
    bob 23
    sally 55
    Jill 292
    // or this
    bob
    23
    sally
    55
    Jill
    292
    Not that the function cannot be written the way you have it in here, and all of the other post I've seen others have the function open file and take in data. Because rule of thumb is functions are to do one thing. Mains function function is to deal with all of the other functions on top of what else it does.
    Last edited by userxbw; 12-09-2017 at 12:10 PM.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861

    If you read your assinment again closer.

    Your assignment does not mention anything about putting your item.txt file into an array. It says to search for the ItemID in the text file. then if not found display an appropriate error message, otherwise the item information is displayed.

    that requires a little more finesse then what you are doing.

    verbatim,

    "it then searches for this itemID in the items.txt text-file"

    not read the text file into an array then search etc...

    what does your actual data file look like , verbatim? Post it or upload it, if they gave you one to work with.
    Last edited by userxbw; 12-09-2017 at 12:24 PM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread