Thread: fopen not working properly?

  1. #1
    Registered User
    Join Date
    Jun 2010

    fopen not working properly?

    Still learning c programming. I compiled the following code:

    #include <stdio.h>
    main() {
      FILE *file;
      if ((file = fopen("test.txt", "r") ) == NULL) {
        printf("File could not be opened.\n");
          else {
             printf("File opened successfully.\n");
      return 0;
    When I run the program in cmd I get one line "File opened successfully" but do not see any of the contents of the file test.txt, located in the same directory as the program. Obviously the file test.txt was found, but why aren't its contents printed out in cmd? The file test.txt contains the text "This is your requested file.". Is it a problem with windows 7 maybe?

  2. #2
    Registered User
    Join Date
    Apr 2010
    How its possible since the only thing that you said is to open the file and print if it is ok. You have to read the data and then print

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    London, United Kingdom
    Ehh.. You just opened the file you haven't read any characters from it and you certainly haven't output-ed anything from the file anywhere. So what do you expect? The compiler isn't going to guess what you want to do:

    "Hey would you like a cake with your program too?"
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Thanks, I misunderstood the fopen function.

  5. #5
    Registered User
    Join Date
    Oct 2010
    man fopen

    If you'd like to read from the file, you have many options including fscanf and fread.

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Dublin, Ireland
    you can use
    fgetc - to get only characters from file and print them to screen
    fgets to read a line from file
    fread copy a block from the file..
    and others...

  7. #7
    Registered User
    Join Date
    Apr 2011
    Quote Originally Posted by darkmagic View Post
    Thanks, I misunderstood the fopen function.
    your not the only one

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Please don't bump old threads just to agree with someone.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Parallel Port IO ops not working properly
    By microtechno in forum Linux Programming
    Replies: 16
    Last Post: 06-08-2009, 12:33 PM
  2. Reducing rational numbers - code not working properly
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 07:42 AM
  3. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  4. MergeSort function not working with randomn number
    By Madshan in forum C++ Programming
    Replies: 5
    Last Post: 02-12-2005, 12:15 AM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM
Website Security Test