sprintf, extension of files??

This is a discussion on sprintf, extension of files?? within the C Programming forums, part of the General Programming Boards category; My program handles/creates different type of files: projects, materials... When a new project is created I use this code: void ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    207

    sprintf, extension of files??

    My program handles/creates different type of files: projects, materials...

    When a new project is created I use this code:

    void
    new_project(void)
    {

    char file_name[8];

    FILE *output;

    printf("Write the name of the new project \n");
    scanf("%s", file_name);
    output = fopen(file_name, "w");
    printf("Please enter the characteristics of the new project \n");
    printf("All the values are to be entered in meters, except the weight");
    printf("\nwhich is in kilograms");

    printf("\nLOA: ");
    scanf("%lf", &LOA);
    fprintf(output, "%.2f\n", LOA);
    printf("\nI: ");
    scanf("%lf", &I);
    fprintf(output, "%.2f\n", I);
    printf("\nJ: ");
    scanf("%lf", &J);
    fprintf(output, "%.2f\n", J);
    printf("\nP: ");
    scanf("%lf", &P);
    fprintf(output, "%.2f\n", P);
    printf("\nE: ");
    scanf("%lf", &E);
    fprintf(output, "%.2f\n", E);
    printf("\nDisplacement (in kilograms): ");
    scanf("%lf", &D);
    fprintf(output, "%.2f\n", D);
    printf("\nGM: ");
    scanf("%lf", &GM);
    fprintf(output, "%.2f\n", GM);
    printf("\nH1: ");
    scanf("%lf", &H1);
    fprintf(output, "%.2f\n", H1);
    printf("\nH2: ");
    scanf("%lf", &H2);
    fprintf(output, "%.2f\n", H2);
    printf("\nH3: ");
    scanf("%lf", &H3);
    fprintf(output, "%.2f\n", H3); printf("\nS1: ");
    scanf("%lf", &S1);
    fprintf(output, "%.2f\n", S1);
    printf("\nS2: ");
    scanf("%lf", &S2);
    fprintf(output, "%.2f\n", S2);
    printf("\nS3: ");
    scanf("%lf", &S3);
    fprintf(output, "%.2f\n", S3);

    fclose(output);

    printf("\n\n Press any key to continue");
    getch();
    MENU();

    }



    But I would like to save the file with the extension .pro (for example). And when I create a new materials library I'd use the extension .mat.

    This way, when the user asks for a list of all the existing projects, I'd look for all the files with the proper extension (I don't know how yet).

    I've done a search and I saw that you guys suggest to use functions like:

    sprintf strcpy strcat

    But I don't really know how to apply them. I've looked through the help from the compiler (Borland Turbo C++ 3.0), and I didn't get anything clear.


    Any help guys?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > char file_name[8];
    Make this bigger
    char file_name[PATH_MAX];

    Change this
    scanf("%s", file_name);
    output = fopen(file_name, "w");

    To this
    scanf("%s", file_name);
    strcat( file_name, ".pro" );
    output = fopen(file_name, "w");

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Thanks again Salem.

    As soon as I finish the function I'm working on I'll compile it and see if it works. But anyway, it's going to.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Well, I've made those changes and I have this problem:

    Error TURBOC\CODE\VERS_01.C 100: Constant expression required


    At the beginning of the code I've declared as a global variable:

    int PATH_MAX = 12;



    And the code looks like this:


    void
    new_project(void)
    {

    char file_name[PATH_MAX];

    FILE *output;

    printf("Write the name of the new project \n");
    scanf("%s", file_name);
    strcat( file_name, ".pro" );
    output = fopen(file_name, "w");

    printf("Please enter the characteristics of the new project \n");


    ***********
    SAME AS BEFORE
    ************
    ***********

    }

    What's wrong?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > int PATH_MAX = 12;
    #define PATH_MAX 12

    In addition, its already defined in limits.h (well it is here). You should have a similarly named constant in one of your .h files.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Ok, it works now. I've included #define PATH_MAX 12


    It's not defined in my limits.h.

    thanks again

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    It's working perfect. Now I've tried to solve the second part of my question. The computer search for all the existing projects (.pro files) and the user can choose one of them to be loaded.

    I've done it and it works, but I don't like the results as much as I expected. When the computer prints the list, it does it this way:

    Dora45.pro
    Kripsi.pro
    ......

    And the user has to write the name of the project "with" the extension .pro.

    How could I print the list without the extensions, and let the user to type just the name of the projects as well (with no extensions)?

    This is the function by now:


    void
    old_project(void)

    {
    // Open an existing project


    char file_name[PATH_MAX];
    FILE *input;

    struct ffblk ffblk;
    int done;

    clrscr();
    printf("Directory listing of existing projects \n");
    done = findfirst("*.pro",&ffblk,0);
    while (!done)
    {
    printf(" %s\n", ffblk.ff_name);
    done = findnext(&ffblk);
    }


    printf("\nName of the project you want to load: ");
    scanf("%s", file_name);
    input = fopen(file_name, "r");


    if (( input = fopen(file_name, "r+")) == NULL ) // check it for errors
    {
    printf("There was an error reading from the file!! \n");
    printf("Press any key to continue ");
    getch();
    old_project();

    }

    fscanf(input, "%lf\n", &LOA);
    fscanf(input, "%lf\n", &I);
    fscanf(input, "%lf\n", &J);
    fscanf(input, "%lf\n", &P);
    fscanf(input, "%lf\n", &E);
    fscanf(input, "%lf\n", &D);
    fscanf(input, "%lf\n", &GM);
    fscanf(input, "%lf\n", &H1);
    fscanf(input, "%lf\n", &H2);
    fscanf(input, "%lf\n", &H3);
    fscanf(input, "%lf\n", &S1);
    fscanf(input, "%lf\n", &S2);
    fscanf(input, "%lf\n", &S3);

    fclose(input);





    }

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > printf(" %s\n", ffblk.ff_name);

    Try
    char *p; // declared in a suitable place

    p = strstr( ffblk.ff_name, ".pro" ); // won't match .PRO though
    if ( p != NULL ) *p = '\0';
    printf(" %s\n", ffblk.ff_name);

  9. #9
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    I think it's defined as MAX_PATH in limits.h....

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    This is my limits.h:

    /* limits.h

    Defines implementation specific limits on type values.

    Copyright (c) 1987, 1991 by Borland International
    All Rights Reserved.
    */

    #ifndef __LIMITS_H
    #define __LIMITS_H

    #if !defined( __DEFS_H )
    #include <_defs.h>
    #endif

    #define CHAR_BIT 8

    #if ('\x80' < 0)
    #define CHAR_MAX 127
    #define CHAR_MIN (-128)
    #else
    #define CHAR_MAX 255
    #define CHAR_MIN 0
    #endif

    #define SCHAR_MAX 127
    #define SCHAR_MIN (-128)
    #define UCHAR_MAX 255

    #define SHRT_MAX 0x7FFF
    #define SHRT_MIN ((int)0x8000)
    #define USHRT_MAX 0xFFFFU

    #define INT_MAX 0x7FFF
    #define INT_MIN ((int)0x8000)
    #define UINT_MAX 0xFFFFU

    #define LONG_MAX 0x7FFFFFFFL
    #define LONG_MIN ((long)0x80000000L)
    #define ULONG_MAX 0xFFFFFFFFUL

    #define MB_LEN_MAX 1

    #endif

    ///////////////////////////////////////////////////



    Nothing about PATH, but I've defined it anyway, and it works.

    KEN: could you help me with the "Parameter pass" thread as well?
    Thanks

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    207
    Salem is this what you mean?


    void
    old_project(void)

    {
    // Open an existing project


    char file_name[PATH_MAX];
    FILE *input;

    struct ffblk ffblk;
    int done;
    char *p;

    clrscr();
    printf("Directory listing of existing projects \n");
    done = findfirst("*.pro",&ffblk,0);
    while (!done)
    {
    p = strstr( ffblk.ff_name, ".pro" );
    if ( p!= NULL ) *p = '\0';
    printf(" %s\n", ffblk.ff_name);
    done = findnext(&ffblk);
    }


    printf("\nName of the project you want to load: ");
    scanf("%s", file_name);
    input = fopen(file_name, "r");

    //////////////////////////


    I've compiled this with no error, and it works just as it did before. Still using the extensions.

    Thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  3. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 08:36 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. process all files with certain extension
    By moi in forum C Programming
    Replies: 6
    Last Post: 08-05-2002, 04:10 AM

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