Thread: error on working with files

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    4

    error on working with files

    Hello guys. Sorry for disturbing but i do need some help.
    I maked a simple program that reads from a file which looks like this:
    Code:
    #include <stdio.h>
    
    int main(){
        FILE *fin;
        int var[10000][7],n,i,j;
        if((fin=fopen("var.in","r+"))==NULL)
    	exit(1);
        fscanf(fin,"%d",&n);
        printf ("%d\n",n);
        for(i=1;i<=n;i++){
    	for(j=1;j<7;j++){
    	    fscanf(fin,"%d",((var+i)+j));
    	    }
        }
        for(i=1;i<=n;i++){
    	for(j=1;j<7;j++){
    	    printf("%d ",*(*(var+i)+j));
    	    }
    	printf("\n");
        }	    
        return 0;
        fclose(fin);
    }
    and the file var.in looks like this:
    Code:
    2
    1 3 4 5 6 7
    6 4 6 4 3 2
    My program prints this:
    Code:
    2
    0 0 0 0 0 0
    0 0 0 0 0 0
    Could anyone help me, and explain why is this happening? instead of that 0's should be the values on the second and third line of the file.

    Thanks a lot!
    Regards,
    Victor

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well writing
    Code:
    int status = fscanf(fin,"%d", &var[i][j] );
    instead of
    Code:
    fscanf(fin,"%d",((var+i)+j));
    would help clarify things.

    Do the same on the printf, use array notation and not pointer arithmetic.

    Checking the return result of fscanf for errors as well would be a good idea.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
        return 0;
        fclose(fin);
    How often do you think that fclose() will be called?
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    4
    Ok, i had a pointer arithmetic problem indeed. Thanks Salem, and ofcourse thanks itsme86 .

    But why didn't my program worked with pointer notation?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But why didn't my program worked with pointer notation?
    Because you got it wrong.

    fscanf(fin,"%d",(*(var+i)+j));
    works for me

    > for(j=1;j<7;j++){
    You do know that arrays start at index 0 and not 1 right?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    4
    Damnn.... now i saw the mistake i have made. stupid me... sorry

    Yes, I do know that arrays start at index 0 and not at 1 but in my problem was convenient to start from 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  3. Working with DLL files...
    By Devil Panther in forum Windows Programming
    Replies: 8
    Last Post: 11-15-2004, 12:42 AM
  4. working with resource files
    By the Wookie in forum Windows Programming
    Replies: 4
    Last Post: 02-01-2003, 10:26 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM