Thread: I Cant input filename and its annoying

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    I Cant input filename and its annoying

    Anybody have any advice here ??

    I would like to read a filename into my programme but it not working with the code below, theres no way i want to type the whole path or any part of the filepath in each time so i am appending the filename that the user inputs to the filepath i have already added to a char buffer.

    when however i print the resulting string to screen it gives me the path without the '\\' between the directories and instead shows me '\' so is this why fopen() is then unable to open the filepath? and why is my string being changed like this? or in the background has it remained the same but clever ole C is printing it with the single backslash to show me the usual filepath format?, if that is the case why is it still failing??

    any help much appreciated



    Code:
    FILE *NumData;
        
        
        char fName[200];
        char fPath[200] = "c:\\documents and settings\\601887741\\My Documents\\Desktop.FS\\Misc\\Programming\\";
       
    
       printf("Enter filename > ");
       scanf("%s", &fName);
       strcat(fPath,fName);
    
       NumData = fopen(fPath, "r");

  2. #2
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by rogster001 View Post
    Anybody have any advice here ??

    I would like to read a filename into my programme but it not working with the code below, theres no way i want to type the whole path or any part of the filepath in each time so i am appending the filename that the user inputs to the filepath i have already added to a char buffer.

    when however i print the resulting string to screen it gives me the path without the '\\' between the directories and instead shows me '\' so is this why fopen() is then unable to open the filepath? and why is my string being changed like this? or in the background has it remained the same but clever ole C is printing it with the single backslash to show me the usual filepath format?, if that is the case why is it still failing??

    any help much appreciated



    Code:
    FILE *NumData;
        
        
        char fName[200];
        char fPath[200] = "c:\\documents and settings\\601887741\\My Documents\\Desktop.FS\\Misc\\Programming\\";
       
    
       printf("Enter filename > ");
       scanf("%s", &fName);
       strcat(fPath,fName);
    
       NumData = fopen(fPath, "r");
    scanf("%s", &fName);
    What is the & operator doing here ? Its an array and its automatically supposed to decompose into an address? Rest everything is fine and i dont see any problem

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The compiler uses the '\' in string literals to signal a control character sequence (such as "\n"). So two of them are used to instruct the compiler to generate one, eg:

    "\\\\\\" -> "\\\"

    So the problem is elsewhere. Can you post a working (eg: compilable) example of the problem?

    >> What is the & operator doing here ? Its an array and its automatically supposed to decompose into an address? Rest everything is fine and i dont see any problem

    Ah, good eye.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    The other problem is that you are blindly reading into your string and catting onto a fixed size buffer without any checking for going over the size of that buffer.

    If the user inputs more text then you can fit into the 200 character buffer + the patch you have already in there, you will be getting undefined behavior.

    you probably want to use fgets and strtol and strncat instead of strcat

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Quote Originally Posted by Sebastiani View Post
    The compiler uses the '\' in string literals to signal a control character sequence (such as "\n"). So two of them are used to instruct the compiler to generate one, eg:

    "\\\\\\" -> "\\\"

    So the problem is elsewhere. Can you post a working (eg: compilable) example of the problem?

    >> What is the & operator doing here ? Its an array and its automatically supposed to decompose into an address? .
    thanks for the advice, i am going to review this and see whats what, i dont understand the second part about &operator? i thought always used this with scanf, meaning 'read value input into address of...' you mean i should just write ("%s", fName) ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        FILE *NumData;
     
        
        char fName[200];
        char fPath[200] = "c:\\myfile.txt";
      
      
       
       printf("Enter filename |> ");
       scanf("%s", &fName);
       strcat(fPath,fName);
       
       NumData = fopen(fPath, "r");  
         
      system("PAUSE");	
      return 0;
    }

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Quote Originally Posted by roaan View Post
    scanf("%s", &fName);
    What is the & operator doing here ? Its an array and its automatically supposed to decompose into an address? Rest everything is fine and i dont see any problem

    Hey good tip,,,it solved it! i got rid of the & realising why that made sense and done, thanks!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You have pass to pass the address of a variable to store the result in. This is done using &. However, arrays are special. Simply typing out its name (without the &) will give you a pointer to the first element which is what you need.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed