Thread: Help with file pointer

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Help with file pointer

    I have the code below to open a file, append some text and then display the contents of that file on the screen. I am new to programming and trying to learn on my own for the company I work for. Any help is greatly appreciated. I was expecting to have this statement - if (fptr == NULL);
    be false but when I execute the program I see the message - We are having problems. Any advice? I am also having some trouble understanding how the functions pass paramaters so any links would be helpful on that subject or array of structures which I was told I will need to understand well by the end of the month. Thanks.

    -Carl

    Code:
    #include<stdio.h>
    
    #define MAXLEN 10
    
    main()
    {
    
    FILE *fptr;
    
    char myline[MAXLEN];
    char filename [] = "data.txt";
    
    fptr = fopen(filename, "a");
    
    if (fptr == NULL);
    {
    printf("\nWe are having problems\n");
    }
    
    fprintf (fptr, "This is working  - testing append!\n");
    fprintf (fptr, "This is ridiclous!\n");
    
    
    fptr = fopen(filename, "r");
    
    while (fgets(myline, MAXLEN, fptr) != NULL)
    {
     printf("%s", myline);
    }
    fclose (fptr);
    
    return 0;
    }

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Ugh.. I wrote a long reply and then my internet connection decided to act up.

    Anyway - You can open the file in both read and write modes. Try "a+", for example.

    However, if you do want to reopen the file with different modes, use freopen() ( Find it on www.cppreference.com under Standard I/O)

    As to the fopen() call failing - the only possible reason is that the file doesn't exist(Names are case-sensitive, mind you) or that you don't have permissions to the file.

    By the way - it would be optimal to exit after you find out you can't open the file. Trying to write to a NULL file pointer will cause a segfault.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    I did that and it does exit

    Thanks for your reply. I did add exit(-1) and the program exits before taking any action. What is bothering to me is that the filename is correct becuse every time I run it I get new lines appended to the file data.txt. It just thinks fptr is null for some reason and displays the printf below it which I think should not happen based on what little I have learned.

    -Carl

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Code:
    if (fptr == NULL);
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    if (fptr == NULL);
    {
    printf("\nWe are having problems\n");
    }
    Do not add ; at the end of if statements. You should get a warning if your compiler warnings level is high enough. Hopefully anyway.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You should also close the file after appending, prior to opening for read access.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Program Changes

    Ok I corrected the code as was suggested and it is below. Thanks for the advice. It exits properly BUT my confusion is if I comment out the exit(-1) the file is being appended to properly so does anyone know why fptr == NULL? Thanks again.

    Code:
    #include<stdio.h>
    
    #define MAXLEN 10
    
    main()
    {
    
    FILE *fptr;
    
    char myline[MAXLEN];
    char filename [] = "data.txt";
    
    fptr = fopen(filename, "a");
    
    if (fptr == NULL);
    {
    printf("\nWe are having problems\n");
    exit(-1);
    }
    
    fprintf (fptr, "This is working  - testing append!\n");
    fprintf (fptr, "This is ridiclous!\n");
    
    fclose (fptr);
    
    fptr = fopen(filename, "r");
    
    while (fgets(myline, MAXLEN, fptr) != NULL)
    {
     printf("%s", myline);
    }
    fclose (fptr);
    
    return 0;
    }

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Your question has been answered twice above.
    Mainframe assembler programmer by trade. C coder when I can.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Sorry about that - I need to slow down

    Thanks everyone for the help! This site is great for us newbies.

    -Carl

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Elysia, I'm picturing you like a mechanic at a car shop... Guy pulls up with a tow truck and offloads his car, which is steaming and on fire, rods thrown through the hood, windows shattered out, tranny dangling on the ground...

    You point and say, "Your tire is flat."


  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The reason why all that other stuff is happening is because the tyre was flat
    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. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM