Help with file pointer

This is a discussion on Help with file pointer within the C Programming forums, part of the General Programming Boards category; I have the code below to open a file, append some text and then display the contents of that file ...

  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
    478
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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
    Katy, Texas
    Posts
    2,309
    Code:
    if (fptr == NULL);
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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
    Katy, Texas
    Posts
    2,309
    You should also close the file after appending, prior to opening for read access.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    Your question has been answered twice above.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    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. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08: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

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