fopen() wont work (i think)

This is a discussion on fopen() wont work (i think) within the C Programming forums, part of the General Programming Boards category; I am testing to see how fread() would work so i did a very simple programm Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    4

    fopen() wont work (i think)

    I am testing to see how fread() would work so i did a very simple programm

    Code:
    #include <stdio.h>
    #include <assert.h>
    
    int main(int argc, char** argv)
    {	
    	int array[20];
    	FILE *fp;
    	int i;
    	fp = fopen("/home/username/myfolder/array.txt","w");
    	assert(fp == NULL);
    	fread(array,sizeof(array),1,fp);
    	fclose(fp);
    	
    	for (i=0; i<20; i++)
    		printf("Element %d:%d",i+1,array[i]);
    	return 0;
    }
    I cant understand what I am doing wrong....

    As you understood i am in linux.The "executable" file is in the same directory with the array.txt whch has 20 integers inside..I have named my file test(so i have test.c,test.o and test).When i do ./test i get this result
    test: test.c:37: main: Assertion `fp == ((void *)0)' failed.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. If you open a file successfully, fp==NULL will be FALSE.
    2. If you open a file for writing ("w"), you can't read from it.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    That's because you're confused about assert,

    You're going to throw an assert if fp != NULL, you want: assert(fp != NULL);

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Don't use assert() to test situations like that. assert() should be used if you think a condition can never be true (and it only asserts in debug builds).
    There are many valid reasons why fopen() would fail, so in that case you shoulde use an if statement to handle it.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > fp = fopen("/home/username/myfolder/array.txt","w");
    But you're trying to READ from the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    43
    Here's a corrected version with some error checking:
    Code:
    #include <stdio.h>
    #include <assert.h>
    
    int main(int argc, char ** argv)
    {	
    	int i;
    	int array[20];
    	FILE *fp;
    
    	if((fp = fopen("/home/username/myfolder/array.txt","r")) == NULL)
    	{
    		printf("fopen() failed\n");
    		return 1;
    	}
    	if(fread(array,sizeof(array),1,fp) < 1)
    	{
    		printf("fread() failed\n");
    		fclose(fp);
    		return 1;
    	}
    	fclose(fp);
    	
    	for (i=0; i<20; i++)
    		printf("Element &#37;d:%d\n",i+1,array[i]);
    	return 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    Quote Originally Posted by mingerso View Post
    Here's a corrected version with some error checking:
    Code:
    #include <stdio.h>
    #include <assert.h>
    
    int main(int argc, char ** argv)
    {	
    	int i;
    	int array[20];
    	FILE *fp;
    
    	if((fp = fopen("/home/username/myfolder/array.txt","r")) == NULL)
    	{
    		printf("fopen() failed\n");
    		return 1;
    	}
    	if(fread(array,sizeof(array),1,fp) < 1)
    	{
    		printf("fread() failed\n");
    		fclose(fp);
    		return 1;
    	}
    	fclose(fp);
    	
    	for (i=0; i<20; i++)
    		printf("Element &#37;d:%d\n",i+1,array[i]);
    	return 0;
    }
    I get fread() failed.Now what?

    I tried wth putting numbers one next to another like 1 2 3 4..... or one under another
    1
    2
    3
    4
    .
    .
    .
    Last edited by apostolos_; 08-06-2008 at 04:04 AM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The function fread reads a BINARY value, not a text string representing the integer value. It would (most likely) work if you make all numbers 2 or 3 digits, but your values won't be what you expect.

    Edit: Make that 3 digits, as you are clearly using some form of Unix, judging by your directory path.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    I see....So in this kind of cases its better to use putw or getw or fscanf right?to use fread() as i understand means first use fwrite to write files in binary right?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by apostolos_ View Post
    I see....So in this kind of cases its better to use putw or getw or fscanf right?to use fread() as i understand means first use fwrite to write files in binary right?
    Correct, as far as fread()/fwrite() is a pair, and fprintf()/fscanf() & fgets() are the another pair [if you can make a pair of three things ;-)] - you choose to use one or the other, and very rarely combine the functions from the different groups in the same file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by apostolos_ View Post
    I see....So in this kind of cases its better to use putw or getw or fscanf right?to use fread() as i understand means first use fwrite to write files in binary right?
    yes - in general you will use fread for reading files created with fwrite (on the same architecture computers - because this solution could be not cross-platform due to different type sizes, endian issues or padding)

    files created with fprintf - could be read with fscanf

    but I would recomend fgets/sscanf pair for better error checking and error prune coding

    EDIT: beaten
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User
    Join Date
    Aug 2008
    Posts
    4
    Thx I used getw/putw and worked like a charm.....Thx for the immediate answers you are great......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. 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
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 03:48 AM
  4. fopen help.
    By chriscolden in forum C Programming
    Replies: 17
    Last Post: 01-13-2006, 05:27 AM
  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