Thread: Files!

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    Barbados
    Posts
    14

    Files!

    Good afternoon. I'm new here and i have a few questions.
    1. you request the user to enter the name of the file that you want to write info to, how do you open that file in another function? say the file name is never the same.
    2. if the user enter the file name stocks20121117. the program should be using 4 separate files. is it possible to automatically generate the files? example when the user enters stocks20121117, list20121117, numbers20121117 and calls20121117 should be created.

    thanks in advance !

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    Barbados
    Posts
    14
    Code:
     char fname[128];
        printf("Enter the file name: \n");
        scanf("%123s",fname);
        strcat(fname,".txt");
        FILE *inputf;
    thats what im using to ask the user for the name. i tried reading fname in another function, but its not working.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You need to either pass the filename to the function (and open the file there) or open the file here and pass the FILE pointer to the function.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Location
    Barbados
    Posts
    14
    Code:
    void weekly_prices(FILE*fPtr) 
    {
        system("cls");
        struct cropprices price={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
    char fname[128];
        printf("Enter the file name: \n");
        scanf("%123s",fname);
        strcat(fname,".txt");
        FILE *inputf
    this is the start of one function.

    Code:
    void newOrder(FILE*fPtr) 
    {
        system("cls");
        struct cusInfo cus={0,"","","","",};
        char fname[128];
        printf("Enter the file name: \n");
        scanf("%123s",fname);
        strcat(fname,".txt");
        FILE *inputf;
    fPtr=fopen("fname","r");
    			 if (fPtr==NULL){
    				   printf("Error opening source file\n");
    			}
    
    			fscanf(fPtr,"%.2f\n",price.pumpkinsp);
    thats how i did it, but in the new order function it is telling me that price was not delcared. how do i pass the file name or pointer?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You didn't pass a pointer to cropprices struct, or create a cropprices struct in newOrder(). It only has a cusInfo struct.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Location
    Barbados
    Posts
    14
    and how to do that exactly?
    Code:
    struct cusInfo cus={0,"","","","",0.0,0};
    i assume i cant declare cropprices struct like how i did cusinfo. Cause then that would erase all the data gather from the previous function?

    Sorry for the hassle but im new to C

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by dogla12 View Post
    i assume i cant declare cropprices struct like how i did cusinfo. Cause then that would erase all the data gather from the previous function?
    "price" is local to weekly_prices(). If you declare another struct cropprices called "price" in newOrder(), it's local to that function. Both are independent from each other and only visible in the scope where they are declared.

    Bye, Andreas

  9. #9
    Registered User
    Join Date
    Apr 2011
    Location
    Barbados
    Posts
    14
    what about the file names now?

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by dogla12 View Post
    what about the file names now?
    It's the same. Each "fname" is local to the function where it is declared.

    Every variable in C has a scope.
    All variables declared inside a function are only visible there (they have block scope).
    Any variable declared outside of a function is visible in the whole file (it has file scope).

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary files: copying jpeg or video files
    By cfanatic in forum C Programming
    Replies: 5
    Last Post: 07-19-2012, 08:17 AM
  2. Drag and Drop files/Read and write files
    By ScoutDavid in forum C Programming
    Replies: 2
    Last Post: 01-13-2011, 12:14 PM
  3. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  4. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  5. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM