write to variable filename

This is a discussion on write to variable filename within the C Programming forums, part of the General Programming Boards category; Hi I want to be able to write to a file that the user has specified the name of. This ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    190

    Question write to variable filename

    Hi

    I want to be able to write to a file that the user has specified the name of. This is where i am

    Code:
    //the rest of the program
    
    char *name[20], *filename;
            FILE *EulerData;
    
    scanf("%s", name);
    
    filename = ("%s.txt", name);
    
    printf("%s", filename);//just to see if the program has added the .txt
    
    for (i=0;i<100;i++)
    {
    EulerData = fopen(filename, "w");
            fprintf(EulerData, "%f %f %f\n", x[i], y[i], z[i]);
            fclose;
    }
    But it either crashes, or doesnt store.

    Any ideas?

    P.S if you are reading from a text file, how do you check if there is nothing in it or it doesnt exist?

    cheers

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You probably want to use sprintf() to produce a string from the input and the ".txt" extension (and most of the time, it's a good idea to check for an extension first, so that foo.txt doesn't become foo.txt.txt).

    By the way, the fun of C and some of it's obscurer parts of it:
    Code:
    filename = ("%s.txt", name);
    is the same as:
    Code:
    "%s.txt"; 
    filename = name;
    Of course having a text-string in the middle of the code produces absolutely nothing useful (although it is valid).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    As an addition to matsp:
    You probably want fclose(filehandle);. Not fclose;.

    Also, you probably want to do the fopen and fclose outside the loop.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    also your name and filename should be char arrays:

    char name[20], filename[24];

    instead of what you have

    Note that file names could include spaces (in this case %s format is not suitable for entering them) and be a lot longer than 19 characters
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    Thanks guys for help, I have modified the function now to this:

    Code:
    int Euler(t, u, v, x0, y0, z0)
    
        float t, u, v, x0, y0, z0;
        {
            float x[100], y[100], z[100], dxdt_i, dydt_i, dzdt_i,dx, dy, dz, dxdt_new, dydt_new, dzdt_new, dt = 1;
            char name[20], filename[24];
            FILE *EulerData;
            int i, n;
    
    
            x[0] = x0;
            y[0] = y0;
            z[0] = z0;
    
            Sleep(1000);
            printf("\nPlease enter the name of the file to store the data in, (example: lorenz)\n");
            printf("If it doesnt exist, it will be created.\n\n");
            scanf("%s", name);
    
    
             sprintf(filename, "%s.txt",name);
    
    
    
            printf("\nStoring all Data in file '%s'\n", filename);
    
            Sleep(1000);
            printf("Beginning execution of improved Euler...\n\n");
            Sleep(1000);
    
            system("cls");
            for (i = 0; i<101; i++)
            {
    
            dxdt_i = t*(y[i] - x[i]);
            dydt_i = x[i]*(u - z[i]) - y[i];
            dzdt_i = (x[i]*y[i]) - (v*z[i]);
    
            dx = (t*(y[i] - x[i]))*dt;
            dy = (x[i]*(u - z[i]) - y[i])*dt;
            dz = ((x[i]*y[i]) - (v*z[i]))*dt;
    
    
            dxdt_new = t*(y[i] - (x[i] + dx));
            dydt_new = x[i]*(u - z[i]) - (y[i] + dy);
            dzdt_new = (x[i]*y[i]) - (v*(z[i] + dz));
    
            x[i+1] = x[i] +(dt/2)*(dxdt_i + dxdt_new);
            y[i+1] = y[i] +(dt/2)*(dydt_i + dydt_new);
            z[i+1] = z[i] +(dt/2)*(dzdt_i + dzdt_new);
    
    
            //printf("New x = %5.3f New y = %5.3f New z = %5.3f\n", x[i+1], y[i+1], z[i+1]);
            printf("%d\n", i);
    
        }
            EulerData = fopen(filename, "w");
            for (n = 0; n<101; n++)
            {
             printf("%d\n", n);
            fprintf(EulerData, "%f %f %f\n", x[n], y[n], z[n]);
    
    
            }
            fclose(filename);
        }
    I added the lines
    printf("%d\n", i);
    and
    printf("%d\n", n);

    to see if it was actually running the for loops, which it is because i get the values printed to the console, but it crashes AFTER the n's have been printed. This tells me that the loop is running, but there is nothing in the textfile, which i suspect means it cant close the file, and thats why it crashes - why would that be?


    EDIT - I had fclose(filename) not fclose(EulerData)

    in reply to vart, should you never have a * before a character array, for instance in another function i have

    char *option[100], *as[100], *bs[100], *cs[100], *Xos[100], *Yos[100], *Zos[100];//the s at the end of the name tell me it is stored as a string.

    option is waiting for the user to type an option, "exit" etc, and the others are taking data from the user, should they not have the *?
    Last edited by a.mlw.walker; 03-30-2009 at 08:05 AM. Reason: IT WORKS!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    int Euler(t, u, v, x0, y0, z0)
    
        float t, u, v, x0, y0, z0;
    This style of C went out of fashion about 15 years ago.

    If the array is intended to be a string then you should not have a * before it. If you want to store the address of a string in your array, then char * is indeed what you want. It is all about how you are using the array. Often, arrays do not have * in the type declaration, but there are many valid cases where you would do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    OK so how is it done now, i read that method in the sams teach yourself c book.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by a.mlw.walker View Post
    OK so how is it done now, i read that method in the sams teach yourself c book.
    Really?

    Well, any new code in the last ten or so years uses:
    Code:
    int Euler(float t, float u, float v, float x0, float y0, float z0)
    {
    ...
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    Oh OK, i thought you were going to tell me a completely new way of doing it, can you do

    Code:
    int Euler (float t, u, v, x0, y0, z0)
    {
    ...
    }
    ?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need the type on each parameter, alas.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Also beware the dangers of scanf: SourceForge.net: Scanf woes - cpwiki
    Or you can try my little experiment on some better C strings: New string functions
    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.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    1
    Be careful a.mlw.walker, if I am correct, I may be on the same course as you and doing the same program for Computational Techniques? Just be careful about putting your full code up here as people may be able to steal your code!

    That said, I am at the same stage as you, I have one question... e are meant to run the program over the range 0<t<100, alas, the numbers soon become MASSIVE!

    Are you sure there is not a typo is the assigment and instead we should run it up to 0<t<10?

    I have done the calculations in excel and the numbers soon reach somewhere in the order of

    +/- x*10^300 which is more or less as much as a double can take!

    I'm stuck!

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    190
    kryptica it sounds like we are on the same course. I was trying to take the code off but it wont let me. eaybaw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. I/O--Using a variable as a filename
    By jedo in forum C++ Programming
    Replies: 7
    Last Post: 09-06-2005, 10:57 AM
  4. This should be the Cprogramming.com anthem!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-20-2002, 11:01 PM

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