Thread: what the hell is the problem?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    what the hell is the problem?

    Hi all!

    im trying to do a very simple thing but it doesnt work:
    im trying to create a 40 elements array of ints with the numbers 1, 2 ,3,4, ... ,40
    afterwards writing it to a file
    and after that read the file again for the whole array
    for some reason i get to read only 26 elements instead of 40! what the?!?


    here's the code:

    Code:
    void main(int argc,char argv[]){
    	int i=0;
    	int arr1[40]={0}; //to write to file
    	int arr2[40]={0}; //to read from the file
    	FILE *f;
    	
    	for(i=0;i<40;i++){
    		arr1[i]=i+1;
    	}
    	f=fopen("c:\\1.txt","w+");
    	fwrite(&arr1,sizeof(int),40,f);
    	fclose(f);
    	f=fopen("c:\\1.txt","r+");
    	printf("freads reads %d elements",fread(arr2,sizeof(int),40,f));
    	fclose(f);
    }
    output: freads reads 26 elements



    thanks
    urbanleg

  2. #2
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    I tried your code in my linux machine . there is no problem ,the expected output came correctly .

    Code:
    #include<stdio.h>
    main(int argc,char argv[]){
            int i=0;
            int arr1[40]={0}; //to write to file
            int arr2[40]={0}; //to read from the file
            FILE *f;
    
            for(i=0;i<40;i++){
                    arr1[i]=i+1;
            }
            f=fopen("1.txt","w+"); //Changed it for linux
            fwrite(&arr1,sizeof(int),40,f);
            fclose(f);
            f=fopen("1.txt","r+");
            printf("freads reads %d elements\n",fread(arr2,sizeof(int),40,f));
            for(i=0;i<40;i++)
                    printf("%d ",arr1[i]); // Printing the read elements 
    
            fclose(f);
    }
    I got the following as the output ,

    Code:
    freads reads 40 elements
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You are opening the file for non-binary writing and reading. fwrite() and fread() do binary reading and writing. On some operating systems, presumably like yours, that sort of mismatch means a loss of data when reading or writing.

    Also, it's not related directly to your problem, but remove the ampersand from your call of fwrite().

    In future, try using a subject line that is descriptive of your actual technical concern, rather than of your frustration.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by grumpy View Post
    You are opening the file for non-binary writing and reading. fwrite() and fread() do binary reading and writing. On some operating systems, presumably like yours, that sort of mismatch means a loss of data when reading or writing.
    I'd say - fread has found some char that is interpreted as EOF in text mode...

    So opening file in binary mode should solve the problem...

    Also to OP I would suggest to check return values of fread and fwrite.

    As well as using %9s instead of %s while reading file name - to prevent buffer overrun.

    And note that buffer of 10 bytes is not big enough to store even regular 8.3 DOS name... So better to switch to buffer of MAX_PATH size for filename...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Quote Originally Posted by grumpy View Post
    You are opening the file for non-binary writing and reading. fwrite() and fread() do binary reading and writing. On some operating systems, presumably like yours, that sort of mismatch means a loss of data when reading or writing.

    Also, it's not related directly to your problem, but remove the ampersand from your call of fwrite().

    In future, try using a subject line that is descriptive of your actual technical concern, rather than of your frustration.
    thanks allot, it does work now

    and for the last part of your post- np

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM