Thread: f open struggles

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    13

    f open struggles

    I am trying to write a basic file reading/editing program and can't seem to figure out what's wrong with my fopen code. I think it has something to do with fgets or my if ( (fopen....) == NULL) line because I get the stderr every time. When I force the path it works but when the filename is entered by the user it goes to stderr. Here it is:

    Code:
    void read_file(void)
    {
            char display_buf[BUFSIZE];
            char filename_buf[FBUF];
            char *filename;
    
            getchar();
            puts("Enter name of text file to display.");
            fgets(filename_buf, FBUF, stdin);
    
            filename = (char *)malloc(strlen(filename_buf));
            if (filename == NULL)
            {
                    fprintf(stderr, "Memory allocation error");
                    exit (1);
            }
    
            strcpy(filename, filename_buf);
    
    
            printf("%s", filename);
            //getchar();
    
            if ( (fp = fopen(filename, "r")) == NULL)
            {
                    fprintf(stderr, "Error opening file.");
                    exit(1);
            }

  2. #2
    Registered User
    Join Date
    Jan 2019
    Posts
    13

    *comment

    It should be noted that some of this code was for troubleshooting purposes such as strcpy() and re-printing the filename to screen.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You need to strip the \n from the end of the line that fgets() returns to you.
    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.

  4. #4
    Registered User
    Join Date
    Jan 2019
    Posts
    13
    Boom. Thank you much!

  5. #5
    Registered User
    Join Date
    Jan 2019
    Posts
    13
    Just a quick follow up, is there a better (or more acceptable) way to do that than the following?

    strncpy(filename, filename_buf, strlen(filename_buf)-1);

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    One of these, usually
    Code:
    if ( p=strchr(buff,'\n') ) *p = '\0';
    if ( p=strrchr(buff,'\n') ) *p = '\0';
    buff[strcspn(buff,"\n")] = '\0';
    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. Replies: 9
    Last Post: 02-07-2017, 12:10 PM
  2. Homework Struggles
    By dolfaniss in forum C Programming
    Replies: 5
    Last Post: 05-04-2011, 01:05 PM
  3. Help With Struggles
    By dolfaniss in forum C Programming
    Replies: 4
    Last Post: 04-25-2011, 07:55 AM
  4. Open source, easy-to-use XMPP messenger: Open-IM
    By mkruk in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 06-22-2008, 04:10 PM
  5. Command to open the 'Open Dialog Box' found in most programs.
    By OmegaFirebolt in forum Windows Programming
    Replies: 5
    Last Post: 03-16-2003, 08:58 PM

Tags for this Thread