fopen

This is a discussion on fopen within the C Programming forums, part of the General Programming Boards category; where should i save my data2.txt file so Xcode can open it. i dont know why Xcode tells me the ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    19

    fopen

    where should i save my data2.txt file so Xcode can open it.

    i dont know why Xcode tells me the program is fine but in quincy 2005 i get errors

    this program has to get 50 integers numbers and then put in two arrays and each array 25 integer and then compare the two arrays

    Name:  error.jpg
Views: 179
Size:  51.7 KB






    Code:
    #include<stdio.h>
    
    /* This function returns 0 (TRUE) when two integers are equal
     and it returns -1 (FALSE) when they are not equal*/
    int comparason_function(int x1, int x2)
    {
        if(x1==x2)
            return 0; // RETURN TRUE
        else if(x1!=x2)
            return -1; // RETURN FALSE
        return 0;
    }
    
    int main()
    {
        FILE* file;
        char* filename;
        int ii;
        int jj;
        
        int array1[25];
        int array2[25];
        
        filename = "data2.txt";
        file = fopen (filename, "r");
        
        printf("THIS PROGRAM IS DONE BY REZA MOLOUDI. ALL RIGHTS ARE RESEVED\n");
        
        
        /* SAVE ALL the first 25 integers in array1*/
        for (jj=0; jj<25 ; jj++)
        {
            fscanf (file, "%d", &array1[jj]);
            //a[j] = i;
        }
        
        
        /* SAVE ALL THE SECOND 25 INTEGERS IN array2*/
        for (jj=0; jj<25 ; jj++)
        {
            fscanf (file, "%d", &array2[jj]);
        }
        
        /* BY CALLING THE comparision_function we figure out which intergers at which places are
         equal*/
        
        for(ii=0 ; ii<25 ; ii++)
        {
            if (comparason_function(array1[ii],array2[ii])==0)
                printf("A %d is found in Cell #%d \n", array1[ii], ii);
        }
        
        
        return 0;
    }
    Last edited by r3zaneo; 11-16-2012 at 10:09 AM. Reason: add more details

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    The way your code is written, it requires the .txt file to be in the same folder as your workspace.

    Where you get the errors i guess that your program can not simply find the file, so variable file is NULL.

    Actually it is a good style, to always check if the filepointer returned by fopen is different to NULL. It might be NULL, if (for example) the file does not exist, if you do not have enough permissions on the file etc. .
    It is the same way of thinking when you always check if malloc returns NULL to you

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    these are the errors i get

    Name:  error.jpg
Views: 96
Size:  51.7 KB

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Try to convert all your comments with double slashes to the traditional ones with asterisks.
    I mean replace this
    Code:
    // a comment
    to this
    Code:
    /* a comment */

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    19
    my problem is this line

    Code:
    //a[j] = i;

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    We put it like
    Code:
    /* a[j] = i; */
    and still had problem?Post the errors compiler says and i suggest you to post the new code rather update the old posts

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen
    By rivkyfried1 in forum C Programming
    Replies: 2
    Last Post: 08-19-2010, 08:20 PM
  2. Help with fopen!!
    By Sailors in forum C Programming
    Replies: 7
    Last Post: 08-06-2007, 06:18 PM
  3. fopen
    By TeQno in forum C Programming
    Replies: 1
    Last Post: 05-22-2003, 11:24 AM
  4. fopen() c:\...\
    By ipe in forum C Programming
    Replies: 6
    Last Post: 03-15-2003, 09:27 AM
  5. fopen help
    By plivermo in forum C Programming
    Replies: 1
    Last Post: 08-31-2001, 07:35 PM

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