Thread: Read and display data from files

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    38

    Read and display data from files

    Hi, im stuck with this part of the program where I have to read all the data from a file (structure by structure) and display it in the list. The way I approched this is by first finding the total number of structures in the file, then looping through all of them with a FOR loop, reading the values of each structure with an fread function and finally, displaying the values one by one in a list... Here's what i currently have:

    Code:
    #include <stdio.h>
    
    typedef struct {
    	char name[50];
    //	float price;
    //	int stock;
    }product;
    void get_product_from_database(FILE*, product*);
    int total_products(FILE*);
    
    int main() {
    	product p;
    	FILE *file_sales;
    	int totalproducts;
    
    	file_sales = fopen("./sales.txt", "rb+");
    
    	totalproducts = total_products(file_sales);
    	printf("*****************************************\n");
    	printf("*      PRODUCTS CURRENTLY IN STOCK      *\n");
    	for (int i = 0; i < totalproducts; i++)
    	{
    		get_product_from_database(file_sales, &p);
    		printf("* %d. %s                                *\n", (i + 1), p.name);
    	}
    }
    
    int total_products(FILE *file_sales) {
    	int file_size;
    	fseek(file_sales, 0, SEEK_END);
    	file_size = ftell(file_sales);
    	return file_size / sizeof(product);
    }
    void get_product_from_database(FILE *file_sales, product *p) {
    	fread(p, sizeof(product), 1, file_sales);
    }
    I only want to display the name of the product. I get a list, but it doesnt display the names, just some weird chars... What am I doing wrong here?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Check for NULL after fopen. If fopen fails and you call fseek, fread or some other function, your app WILL crash.
    You're not closing the file either.
    Last edited by Elysia; 11-30-2007 at 01:00 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Actually, the main problem is that total_products() sets the file pointer to the end of the file. Make sure you rewind the file in total_products().
    Code:
    int total_products(FILE *file_sales) {
    	int file_size;
    	fseek(file_sales, 0, SEEK_END);
    	file_size = ftell(file_sales);
    	rewind(file_sales);
    	return file_size / sizeof(product);
    }
    fseek(fp, 0, SEEK_SET) is the same as rewind(fp).

    ftell() returns a long, not an int. Not that it matters in most cases.
    Code:
    printf("* &#37;d. %s                                *\n", (i + 1), p.name);
    You could do that more easily and flexibly with something like this:
    Code:
    printf("* %4d %-70s *\n");
    %4d will make the number have at least four digits, padded with spaces. (%04d padds with zeros.) %-70s will make the string have at least 70 characters, padded with spaces, and left-aligned. (%70s is right-aligned.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Easy to miss -_-
    And people don't catch these errors which are so easy to catch with a debugger...

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You are fseek'ing to the end of the file in the total_products function but you then enter a loop where you end up calling fread again and again without reseting the where you are in the file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    38
    Ok, thank you... I got it working...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. data read zeros?
    By Question in forum C++ Programming
    Replies: 2
    Last Post: 03-12-2003, 01:15 PM
  4. Client-Server Data Read Problem
    By bob2509 in forum C Programming
    Replies: 8
    Last Post: 11-06-2002, 11:47 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM