fread doesnt work

This is a discussion on fread doesnt work within the C Programming forums, part of the General Programming Boards category; Code: #include <conio.h> #include <stdio.h> #include <dos.h> struct rec { int record; int cost; }; void main() { int i; ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    fread doesnt work

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <dos.h>
    struct rec {
           int record;
           int cost;
           };
    
    void main() {
         int i;
         FILE *fp;
         clrscr();
         struct rec store[100];
         fp=fopen("c:\hardware.dat","r");
         for(i=0;i<100;i++) {
    	 store[i].cost=i*50;
    	 store[i].record=i+1;
    	 fwrite(&store[i],sizeof(store),1,fp);
    	 }
    
        //read
    
         for(i=0;i<100;i++) {
    	fseek(fp,i,0);
    	fread(&store,sizeof(store),1,fp);
    	printf("%d	%d\n",store[i].record,store[i].cost);
    	}
    	fclose(fp);
    getch();
    }
    this function works great writes to file and read them well
    but if i add /* */ it doesnt read the array

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <dos.h>
    struct rec {
           int record;
           int cost;
           };
    
    void main() {
         int i;
         FILE *fp;
         clrscr();
         struct rec store[100];
         fp=fopen("c:\hardware.dat","r");
         /*for(i=0;i<100;i++) {
    	 store[i].cost=i*50;
    	 store[i].record=i+1;
    	 fwrite(&store[i],sizeof(store),1,fp);       */
    
         for(i=0;i<100;i++) {
    	fseek(fp,i,0);
    	fread(&store,sizeof(store),1,fp);
    	printf("%d	%d\n",store[i].record,store[i].cost);
    	}
    	fclose(fp);
    getch();
    }
    what should i do??

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > void main()
    Icky.

    > but if i add /* */ it doesnt...
    LOL

    > what should i do??
    The world is waiting. I must leave you now.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >clrscr();
    >struct rec store[100];
    You can't delcare variables after starting the function code. Swap these two around.

    >fp=fopen("c:\hardware.dat","r");
    Always check that the file opened before you use it.
    You opened with mode r, for reading structs you'll need rb.
    Also, how do you expect to write to a file when you only opened it for reading?

    >fseek(fp,i,0);
    Use the #define'd variables supplied with your compiler to make reading easier (SEEK_SET etc)

    >fread(&store,sizeof(store),1,fp);
    Which of the store elements are your reading in to? You didn't specify one.

    >void main()
    As already said..... don't void main.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using fread on stdin
    By nadroj in forum C Programming
    Replies: 29
    Last Post: 10-23-2008, 02:03 PM
  2. Another link from Microsoft about bug in fread
    By vart in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-06-2008, 11:56 AM
  3. How to load pixels of BMP into an array
    By brconner in forum Windows Programming
    Replies: 10
    Last Post: 06-02-2007, 04:30 AM
  4. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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