Need help on file name access.

This is a discussion on Need help on file name access. within the C Programming forums, part of the General Programming Boards category; Hi, I have some trouble in reading the filename. For some reason, my program wouldn't continue on after inputting the ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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,682
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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
    32,823
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    32,823
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    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, 11: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, 04:53 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM

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