#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Bristol
    Posts
    13
    Rep Power
    0

    Entering filename from keyboard.


    Okay, so my project is pretty much finished. I need to put some notes throughout but otherwise there is only one sticking point.

    The assignment calls for the user to be able to input the file name from keyboard.

    I'll pop my coding in underneath:

    Code:
    int main()
    {
        FILE *file_ptr;
        double x,y,sigmax,sigmay, myarray[10000][4],a[10000],b[10000],c[10000],d[10000],e[10000],value1,value2,value3,value4,value5,coeff1,coeff2,err1,err2;
        int i;
    
        file_ptr=fopen("C:\\Data\\LinearTestData.txt","r"); //open file from computer
        if (file_ptr == NULL) printf("Error: File not found.\a\n");
        else
    then the code continues on. Putting the numbers in the file into a array and then doing some maths on them essentially.

    I want to be able to ask the user, at the start of the code, to choose which file they want to read the data from. Do I use a string, a gets?

    Thanks in advance,

    Sophie.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    No! You do not use that function. That function is dangerous: it cannot be executed safely and there is a perfectly reasonable alternative: fgets(). Just remember to remove the trailing '\n' from the buffer before calling fopen.

    You can also, maybe (depends on the requirements), use parameters to the program. In this way the user would pass the file to open as a command line parameter
    Code:
    int main(int argc, char **argv)
    {
        /* ... */
        if (argc == 2) fopen(argv[1], "r");
        /* ... */
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Bristol
    Posts
    13
    Rep Power
    0
    Okay, had a go with the gets but it's not working at the moment. Not sure what to put into the file pointer part?

    Here's the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        FILE *file_ptr;
        double x,y,sigmax,sigmay, myarray[20000][4],a[20000],b[20000],c[20000],d[20000],e[20000],value1,value2,value3,value4,value5,coeff1,coeff2,err1,err2;
        char Y, N, char1, char2, filename[1000];
        int i,j;
    
        Y='y';
        N='n';
    
        printf("Please input the address of your data file, using double backslashes, and incuding the file extension.\n");
        fgets(filename, 1000, stdin);
    
        file_ptr=fopen("?","r"); //open file from computer
        if (file_ptr == NULL) printf("Error: File not found.\a\n");
        else
    Anyone? It's literally the last piece of my project. :(
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    Huh? You read in the filename into the variable, filename. You then want to open that file by that filename. So you pass filename to fopen.

    How could there possibly be any question or confusion about something as boneheaded simple as that?
  8. #5
  9. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Eh, I bet with all those big stack-allocated arrays it's going to blow up at runtime anyway.

    fgets includes the newline character. You need to remove it.

    c Code:
    fgets(filename, 1000, stdin);
    if (filename[strlen(filename) - 1] == '\n')
      filename[strlen(filename) - 1] = '\0';
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Bristol
    Posts
    13
    Rep Power
    0
    @dwise1_aol

    Rude

    It runs it works fine, it was the new line that needed testing and removing. I figured, seeing as it didn't work as I expected, maybe it needed something different.

    Seriously though, no need to be nasty.

IMN logo majestic logo threadwatch logo seochat tools logo