input filename to open a file

This is a discussion on input filename to open a file within the C Programming forums, part of the General Programming Boards category; Hi, I am new to this forum. I am required to make a conversion program to convert one file format ...

  1. #1
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56

    input filename to open a file

    Hi, I am new to this forum. I am required to make a conversion program to convert one file format to the next using the c language. What I want is to have the user type the name of the file for conversion on the terminal, and it will begin its process. Here is what I have so far:
    Code:
    char *inputconnectfilename;
    
    FILE *InputConnectionFile;
    printf("Please input the filename of the connection file (Note: Must be in the same directory of this program)\n");
    
    scanf("%s", inputconnectfilename);
    printf("intializing...   ");
    printf("%s", *inputconnectfilename);
    InputConnectionFile = fopen(inputconnectfilename, "r");
    
    if (InputConnectionFile == NULL)
    {	
    	printf("\nFailed to open the file.\n");
    	exit(1);
    
    }
    ...
    The code compiled without any errors. However, no matter what I type for the filename, a segmentation error is there and my "failed to open file" message appeared. Can somebody help me to find what was wrong with this code, and if possible, ways to fix it?

    Thanks.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well you haven't allocated any space on the heap for the filename to fit in...

    I'd also use the stack, and fgets()

    consider:

    Code:
    char fileName[260];
    FILE * fp;
    
    fgets(fileName, sizeof(fileName), stdin);
    
    if((fp = fopen(fileName, "r")) == NULL)
    {
        perror("Failed to open the file);
        exit(1);
    }
    
    ...

  3. #3
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    Thanks for your quick reply!

    for some reason I have implement your code like this:
    Code:
    char inputconnectfilename[260];
    FILE *InputConnectionFile;
    
    printf("Please input the filename of the connection file (Note: Must be in the same directory of this program)\n");
    fgets(inputconnectfilename, sizeof(inputconnectfilename), stdin);
    
    
    
    printf("intializing...   ");
    
    if((InputConnectionFile = fopen(inputconnectfilename, "r")) == NULL)
    {
        perror("Failed to open the file");
        exit(1);
    }
    it compiled without any errors, however, there is another error came up:
    [firyace@localhost Desktop]$ gcc -o a projectmain.c
    [firyace@localhost Desktop]$ ./a
    Please input the filename of the connection file (Note: Must be in the same dire ctory of this program)
    projectmain.c
    Failed to open the file: No such file or directory
    intializing...
    [firyace@localhost Desktop]$
    it is weird in a sense that I have the real file (the exact file for the source code) inside the directory, and perror gave me "No such file or directory". Also, I have purposely printed a message right before perror, yet the word "initializing..." came after the Failure message. Did I do something wrong? Please help.
    Thanks.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    You're almost there - you just need to remove the newline from the end of the buffer fgets() returns.
    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.

  5. #5
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    Thankyou very much, you guys are awesome!

    Now this part of the program works!

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  2. File I/O with 2 input and 1 output file
    By Sandy in forum C Programming
    Replies: 1
    Last Post: 04-19-2003, 01:06 PM
  3. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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