Thread: Reading from file problems

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    4

    Reading from file problems

    I am trying to read from a text file (attached) i want to read each value into a structure but for some reason when i read in the first value of the text file,an integer ID number, it doesnt read it in correctly it has some other number. The rest of the file reads in correctly apart from this ID number.

    If someone could have alook at the code and point out any errors i would be greatful!

    Thanks


    The Program Code


    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    const int hashSIZE = 50;
    const int emptyFLAG = 0;
    const int delStock = -1;
    const int notFound = -1;
    
    struct StockDetails
    {
      int intCatNumber;
      char strDescription[30];
      int intPrice;
      int intquantity;
    };
    
    struct StockTable
    {
      int count;
      StockDetails list[hashSIZE];
    };
    
    void createTable(StockTable&);
    void fillTable(StockTable&);
    void GetOption(StockTable&,int);
    
    inline int hashKey(int key){ return key % hashSIZE;}
    void DisplayMenu();
    void ClearScreen();
    void ClearCIN();
    
    
    
    void main()
    {
    	int intSelection;
    	StockTable p;
    
    	createTable(p);
       fillTable(p);
    	DisplayMenu();
    	cin>>intSelection;
    	// Check to see if the number is right
    	while (intSelection > 6)
    	 {
    	  	ClearCIN();
    		cout<<"Incorrect Selection Number, Please Select Again: ";
    		cin>>intSelection;
    	 }
    	 GetOption(p,intSelection);
    }
    
    
    void DisplayMenu()
      {
    
    	 ClearScreen();
    
    	 cout<<"\n\t1:\tView Stock Table\n\t2:\tSearch Stock Table\n";
    	 cout<<"\t3:\tAdd to Stock Table\n\t4:\tEdit Stock Item";
    	 cout<<"\n\t5:\tView Stock Item\n\t6:\tQuit";
    	 cout<<"\n\nPlease select one of the above options: ";
      };
    
    
    void GetOption(StockTable& p, int intSelection)
      {
    	  // Get the option the user has selected and go to
    	 // the right function
    	 switch (intSelection)
    	 {
    		case 1 :
    		  break;
    		case 2 :
    		  break;
    		case 3 :
    		  break;
    		case 4 :
    		  break;
    		case 5 :
    		  break;
    		case 6 :
    		  break;
    		default :
    		  // OK, if they see this message it means i have really
    		  // messed up in my coding somewhere
    		  cout<<"\n\nWell there is a serious F**K up somewhere because you\n";
    		  cout<<"got passed all my error checking :p\n\n";
    	 }
      }
    
    void createTable(StockTable& p)
    {
    	p.count = 0;
    	for(int i=0;i<hashSIZE;i++)
    	{
    		p.list[i].intCatNumber = 0;
    	}
    }
    
    
    void fillTable(StockTable& p)
    {
    	FILE* StockData;
    	int tmpAmount,i;
    	int tmpID;
    	char tmpDes[30];
    	float tmpPrice;
    
    	i = 0;
    	StockData = fopen("stock.txt","rt");
    
    	if(!StockData)
    	 {
    		cout<<"Error opening file...";
    		exit(1);
    	 }
    
    	 cout<<"Creating table...\n";
    	 while (fscanf(StockData, "%d\t%s\t%f\t%d", &tmpID, &tmpDes,&tmpPrice,&tmpAmount) == 4)
    	 {
    		p.list[i].intCatNumber = tmpID;
    		p.list[i].strDescription = tmpDes;
    		p.list[i].intPrice = tmpPrice;
    		p.list[i].intquantity = tmpAmount;
    		i++;
    	 }
    
    	fclose(StockData);
    }
    
    void ClearScreen()
    {
    	for (int i=0;i<30;i++)
    	{
    	 cout<<'\n';
    	}
    }
    
    
    void ClearCIN()
    {
      cin.clear();
      cin.ignore(1000,'\n');
    }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    One glaring error:

    Code:
      p.list[i].strDescription = tmpDes;
    Assignment to array is not legal. Didn't your compiler complain?

    try

    Code:
      strcpy(p.list[i].strDescription, tmpDes);
    Then, to debug your problem :

    place something like

    Code:
    cout << p.list[0].intCatNumber;
    some place after you have read the input file.

    Dave
    Last edited by Dave Evans; 03-22-2004 at 10:26 AM.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    More glancing.....

    >>while (fscanf(StockData, "%d\t%s\t%f\t%d", &tmpID, &tmpDes,&tmpPrice,&tmpAmount) == 4)

    "tmpDes" is already a pointer, no need for the "&".

    gg

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    4
    Thanks for the feedback.

    I realised after some debugging that i had to change the int intCatNumber to a long int.

    Again thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. I have some problems reading struc from file
    By samc2004 in forum C Programming
    Replies: 4
    Last Post: 12-18-2003, 02:47 PM