fopen not working properly?

This is a discussion on fopen not working properly? within the C Programming forums, part of the General Programming Boards category; Still learning c programming. I compiled the following code: Code: #include <stdio.h> main() { FILE *file; if ((file = fopen("test.txt", ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    4

    fopen not working properly?

    Still learning c programming. I compiled the following code:


    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
    Posts
    79
    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
    Location
    London, United Kingdom
    Posts
    2,094
    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
    Posts
    4
    Thanks, I misunderstood the fopen function.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    107
    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
    Location
    Dublin, Ireland
    Posts
    629
    xD.
    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
    Posts
    37
    Quote Originally Posted by darkmagic View Post
    Thanks, I misunderstood the fopen function.
    your not the only one

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

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