Thread: Need help opening a user provided file

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    18

    Question Need help opening a user provided file

    Hi everyone,

    This is my first real attempt at file i/o stuff in C. The user should be able to choose whether to import the provided file or their own file, and I was able to open the provided file.

    The problem arises when they choose to import their own file (when the argument "provided" is false). They don't even get to enter the filename before a segmentation fault occurs. What's wrong with the way I did this?

    Code:
      void import(bool provided) {
      String filename;
      FILE *f = fopen(provided? "Lab6-Data.csv": fgets(filename, 50, stdin), "r");
      putchar(fgetc(f));
      fclose(f);
    }
    P.S. I used a typedef to rename 'char *' to String, I am using C.
    Last edited by C-UL8R; 1 Week Ago at 11:06 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,557
    My first question is: What is a String?

    Why are you trying to write to a file that was opened in read mode?

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    18
    I edited in regards to "String". I am trying to read it and print a character from it to main.c, not write to it.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,557
    P.S. I used a typedef to rename 'char *' to String, I am using C.
    Okay so where did you allocate memory for that pointer?

    Also please don't edit your post to answer questions, post the new information in another post in the topic.

  5. #5
    Registered User
    Join Date
    Oct 2020
    Posts
    18
    I don't really know much about memory but I declared the pointer "filename" inside the function with "String filename;". I also tried declaring it like this: "char filename[50];" but that didn't fix anything. Initializing it as an array also didn't help.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,920
    Post code that either compiles or post the errors you get when you try to compile the code you post!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    3,920
    Add a test to verify that "f" is not NULL.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,920
    Write your own function that you use in place of fgets (your function calls fgets and gets rid of the trailing newline).

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    198
    You should also be checking the FILE* returned by fopen(). Otherwise, the program will crash if the file doesn't exist.

    Reading the file will require a loop.

    Code:
    for(;;)
    {
     int c = fgetc(f);
     if(c == EOF)
      break;
    /*
     ...now process the next character...
    */
    }

  10. #10
    Registered User
    Join Date
    Sep 2020
    Posts
    39
    Consider sth. like this:
    Code:
    bool import(bool provided)
    {
        char file_name[256] = { 0 };
        FILE* src = NULL;
    
    
        if (provided)
        {
            src = fopen("Lab6-Data.csv", "r");
            if (src == NULL)
                return false;
        }
        else
        {
            printf("Enter filename: ");
            if (fgets(file_name, sizeof(file_name), stdin) == NULL)
                return false;
            char* cp = strchr(file_name, '\n'); // check for trailing '\n'
            if (cp != NULL)
                *cp = '\0';
        }
        src = fopen(file_name, "r");
        if (src == NULL)
            return false;
    
    
        int ch = 0;
        while ((ch = fgetc(src)) != EOF)
            putc(ch, stdout);
        fclose(src);
    
    
        return true;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem opening input file entered by user
    By Opiumyeti in forum C Programming
    Replies: 12
    Last Post: 11-11-2017, 09:34 AM
  2. Replies: 4
    Last Post: 10-29-2014, 04:02 AM
  3. Replies: 1
    Last Post: 04-12-2011, 02:22 AM
  4. opening a user selected file
    By SymDePro in forum C Programming
    Replies: 10
    Last Post: 07-14-2009, 01:35 PM
  5. Opening a user-defined file
    By mr_diss in forum C Programming
    Replies: 8
    Last Post: 02-28-2005, 03:47 PM

Tags for this Thread