Thread: Need help on file name access.

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

    Need help on file name access.

    Hi, I have some trouble in reading the filename. For some reason, my program wouldn't continue on after inputting the filename, even after pressing enter. I have purposely enter the a printf command with "Hello" for test, but it never show up. Whats worse is that I could even type on the next line after I press enter.

    Here is my code:

    Code:
       
    
    int main()
    {
    struct database_input data[99999];
    char inputconnectfilename[260];
    char *p;
    FILE *InputConnectionFile;
    
    printf("Please input the full filename of the network data file \n");
    printf("(Note: Must be in the same directory as this program)\n");
    fgets(inputconnectfilename, sizeof(inputconnectfilename), stdin);
    if ((p = strchr(inputconnectfilename, '\n')) != NULL)
          *p = '\0';
    
    
    
    if((InputConnectionFile = fopen(inputconnectfilename, "r")) == NULL)
    {
        perror("Failed to open the file");
        exit(1);
    }
    
    prinf("Hello");  /* use for testing*/
    ...
    and here is my output:
    [firyace]$ gcc -Wall projectmain.c -lm
    [firyace]$ ./a.out
    Please input the full filename of the network data file
    (Note: Must be in the same directory as this program)
    test.dat

    Please tell me what to do to fix this problem.

    Thanks!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Your printf("hello") has no newline on it.

  3. #3
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    okay.......
    the printf hello is not part of the program, its just a test to see if the program actually reaches that particular point. So whetther or not it has a newline it doesn't matter. The problem right now is that after test.dat and inputting enter I still can input text on the next line and the program cannot continue.

    Thanks for your reply anyways.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by firyace View Post
    okay.......
    the printf hello is not part of the program, its just a test to see if the program actually reaches that particular point. So whetther or not it has a newline it doesn't matter.
    That's where you are wrong. Add the newline and try again.

  5. #5
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    Hi, I did add the newline on printf("hello") to printf("hello \n") after your first post if thats what you mean, but the problem still remains and hasn't changed a bit..

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by firyace View Post
    Hi, I did add the newline on printf("hello") to printf("hello \n") after your first post if thats what you mean, but the problem still remains and hasn't changed a bit..
    I see nothing wrong with the code. You must be leaving something out.

  7. #7
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    well after printf("Hello"), I have 2 functions called, one is initializing an array of my data structure and the other one is copying the file with the filename typed onto the terminal from this code, but that was afterwards. This piece of code is the same as in my file, nothing is changed, only added ... to express the int main continues to go on.

    Btw, I'll show you that I could type text after I input my filename and press enter:

    [firyace]$ gcc -Wall projectmain.c -lm
    [firyace]$ ./a.out
    Please input the full filename of the network data file
    (Note: Must be in the same directory as this program)
    test.dat
    asdfsdfsdafasfasdasdfasdfasdf

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    #include<stdio.h>
    
    int main()
    {
        char inputconnectfilename[260];
        char *p;
        FILE *InputConnectionFile;
    
        printf("Please input the full filename of the network data file \n");
        printf("(Note: Must be in the same directory as this program)\n");
        fgets(inputconnectfilename, sizeof(inputconnectfilename), stdin);
        if ((p = strchr(inputconnectfilename, '\n')) != NULL)
             *p = '\0';
    
        if((InputConnectionFile = fopen(inputconnectfilename, "r")) == NULL)
        {
            perror("Failed to open the file");
            getchar();
            exit(1);
        }
    
        printf("Hello");  /* use for testing*/
        getchar();
        return 0;
    }
    /* my output
    Please input the full filename of the network data file
    (Note: Must be in the same directory as this program)
    test.txt
    Hello
    */
    First of all u need a proper indendation.

    Well, do u really need 99999 instances of your struct. It is too much. You will end up witj stack overflow. The program it self works fine. Try reducing the struct array size to 10 or something.

    If u really need a huge amount if arrya elements create them at the run time. using malloc.

    And prinf ot printf

    ssharish2005

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Another observation. How big is that database_input structure? An array of 99999 is very large to have as a local variable.

    Try putting a printf() directly before your call to fopen() and one directly after. The printf("Hello\n"); should serve that purpose for the second call, provided there really is NO code between the fopen() and the printf(). See if it's even getting to the point of opening the file.

    Also, why do you tell the user that the file must be in the same directory as the program? There's no reason it has to be.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > struct database_input data[99999];
    This is a hugh array (I'm guessing).
    Potentially at least, you could have used so much stack with this array that you have very little left for anything else.
    Which may be causing odd effects of things randomly working (or not).

    Logically at least, there is nothing wrong with the code posted.

    Segfaults are very hard to track down with program snippets, because the code posted is seldom the root cause of the problem, more the unwitting victim.
    We really need complete programs which demonstrate the problem.
    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.

  11. #11
    Engineer in research :(
    Join Date
    May 2007
    Location
    Calgary
    Posts
    56
    can malloc be able to create 99999 elements on the array or more?

    I am using 99999 instances because I need it. What the file contains are tables of coordinates, experimental data, and other categories that must be copied onto the struct. Often, the file contains as little as 48 lines to tables that has more than 100000 lines.

    for some reason though, after Hello, I could type text into it. I don't know why, but some how segmentation error or other stuff does not show up, its that it looks like an infinite loop somehow. I have to push ctrl-c to get the terminal command back.

    Thanks

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    malloc should be able to allocate whatever you've got virtual memory for.

    If it can't, it will return NULL.

    In any event, by the time malloc gets around to returning NULL, the equivalent array on the stack would have failed long ago.
    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.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by firyace View Post
    for some reason though, after Hello, I could type text into it.
    I'm not sure why that is surprising to you. The terminal is still able to input and display typed characters, even if they are not flowing into your program. Your program has nothing to do with the echoing of characters to the display.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM